Giter VIP home page Giter VIP logo

jvm-driver's Introduction

ToolFactory JVM Driver Tweet

logo.png

Maven Central with version prefix filter GitHub

Platforms

Supported JVM

GitHub open issues GitHub closed issues

Repository dependents

Extracted from Burningwave Core 8, fully refactored and with an extensible architecture the JVM Driver allows a deep interaction with the JVM without any restrictions.


To include ToolFactory JVM Driver in your projects simply use with Apache Maven:

<dependency>
    <groupId>io.github.toolfactory</groupId>
    <artifactId>jvm-driver</artifactId>
    <version>9.7.1</version>
</dependency>	

Requiring the ToolFactory JVM Driver module

To use ToolFactory JMV Driver as a Java module, add the following to your module-info.java:

//Mandatory if you use the dynamic, hybrid or native driver
requires io.github.toolfactory.narcissus;

//Mandatory if you use the default, dynamic or hybrid driver
requires jdk.unsupported;

requires io.github.toolfactory.jvm;

Overview

There are four kinds of driver:

  • the default driver completely based on Java api
  • the dynamic driver that extends the default driver and uses a JNI function only if a Java based function offered by the default driver cannot be initialized
  • the hybrid driver that extends the default driver and uses some JNI functions only when run on JVM 17 and later
  • the native driver that extends the hybrid driver and uses JNI functions more consistently regardless of the Java version it is running on

By default all JNI methods used by the dynamic, hybrid and native driver are supplied by Narcissus that works on the following system configurations:

  • Windows (x86, x64)
  • Linux (x86, x64)
  • MacOs (x64)

It is also possible to replace the Narcissus JNI engine with another fast JNI engine: the one included in the Burningwave JVM Driver.


Usage

To create a driver instance you should use this code:

io.github.toolfactory.jvm.Driver driver = io.github.toolfactory.jvm.Driver.getNew();

The driver type returned by the method io.github.toolfactory.jvm.Driver.Factory.getNew() is the first driver that can be initialized among the default, hybrid and native drivers respectively.

If you need to create a specific driver type you should use:

  • this code to create a default driver instance:
io.github.toolfactory.jvm.Driver driver = io.github.toolfactory.jvm.Driver.Factory.getNewDefault();
  • this code to create a dynamic driver instance:
io.github.toolfactory.jvm.Driver driver = io.github.toolfactory.jvm.Driver.Factory.getNewDynamic();
  • this code to create an hybrid driver instance:
io.github.toolfactory.jvm.Driver driver = io.github.toolfactory.jvm.Driver.Factory.getNewHybrid();
  • this code to create a native driver instance:
io.github.toolfactory.jvm.Driver driver = io.github.toolfactory.jvm.Driver.Factory.getNewNative();

Each functionality offered by the driver is initialized in deferred way at the first call if the driver is not obtained through the method io.github.toolfactory.jvm.Driver.getNew(). However, it is possible to initialize all of the functionalities at once by calling the method Driver.init().

The methods exposed by the Driver interface are the following:

public <D extends Driver> D init();

public <T> T allocateInstance(Class<?> cls);

// Return a ClassLoaderDelegate or the input itself if the input is a 
// BuiltinClassLoader or null if the JVM version is less than 9
public ClassLoader convertToBuiltinClassLoader(ClassLoader classLoader);

public Class<?> defineHookClass(Class<?> clientClass, byte[] byteCode);

public Class<?> getBuiltinClassLoaderClass();

public Class<?> getClassLoaderDelegateClass();

public Class<?> getClassByName(String className, Boolean initialize, ClassLoader classLoader, Class<?> caller);

public MethodHandles.Lookup getConsulter(Class<?> cls);

public <T> Constructor<T>[] getDeclaredConstructors(Class<T> cls);

public Field[] getDeclaredFields(Class<?> cls);

public Method[] getDeclaredMethods(Class<?> cls);

public <T> T getFieldValue(Object target, Field field);

public Package getPackage(ClassLoader classLoader, String packageName);

public Collection<URL> getResources(String resourceRelativePath, boolean findFirst, ClassLoader... classLoaders);

public Collection<URL> getResources(String resourceRelativePath, boolean findFirst, Collection<ClassLoader> classLoaders);

public <T> T invoke(Object target, Method method, Object[] params);

public boolean isBuiltinClassLoader(ClassLoader classLoader);

public boolean isClassLoaderDelegate(ClassLoader classLoader);

public <T> T newInstance(Constructor<T> ctor, Object[] params);

public CleanableSupplier<Collection<Class<?>>> getLoadedClassesRetriever(ClassLoader classLoader);

public Map<String, ?> retrieveLoadedPackages(ClassLoader classLoader);

public void setAccessible(AccessibleObject object, boolean flag);

public void setFieldValue(Object target, Field field, Object value);

public void stop(Thread thread);

public <T> T throwException(String message, Object... placeHolderReplacements);

public <T> T throwException(Throwable exception);

In the test folder is also present a little utility class named io.github.toolfactory.util.Reflection that can be copied into your project and that can be instantiated through the factory methods exposed by the inner static class io.github.toolfactory.util.Reflection.Factory:

public static Reflection getNew();

public static Reflection getNewWithDefaultDriver();

public static Reflection getNewWithDynamicDriver();

public static Reflection getNewWithHybridDriver();

public static Reflection getNewWithNativeDriver();

public static Reflection getNewWith(Driver driver);

The methods exposed by the io.github.toolfactory.util.Reflection component are the following:

public Driver getDriver();

public Collection<Method> getDeclaredMethods(Class<?> cls);

public Collection<Method> getAllMethods(Class<?> cls);

public <T> T getFieldValue(Object target, Field field);

public void setFieldValue(Object target, Field field, Object value);

public Field getDeclaredField(Class<?> cls, String name);

public Collection<Field> getDeclaredFields(Class<?> cls);

public Collection<Field> getAllFields(Class<?> cls);

public Collection<Constructor<?>> getDeclaredConstructors(Class<?> cls);

public Collection<Constructor<?>> getAllConstructors(Class<?> cls);

Compilation requirements

A JDK version 9 or higher is required to compile the project.

Ask for assistance

For assistance you can:

jvm-driver's People

Contributors

dependabot[bot] avatar grmt avatar lukehutch avatar ptziegler avatar roberto-gentili avatar web-flow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jvm-driver's Issues

Reporting Vulnerability

Hello!

I hope you are doing well!

We are a security research team. Our tool automatically detected a vulnerability in this repository. We want to disclose it responsibly. GitHub has a feature called Private vulnerability reporting, which enables security research to privately disclose a vulnerability. Unfortunately, it is not enabled for this repository.

Can you enable it, so that we can report it?

Thanks in advance!

PS: you can read about how to enable private vulnerability reporting here: https://docs.github.com/en/code-security/security-advisories/repository-security-advisories/configuring-private-vulnerability-reporting-for-a-repository

OSGi Metadata contain outdated version numbers

The current version (9.4.3) exports its packages with version 8.4.9. If I want to use this library in an OSGi environment, I then get the following error:

[ERROR] Cannot resolve project dependencies:
[ERROR]   Software being installed: org.eclipse.wb.core 1.12.0.qualifier
[ERROR]   Missing requirement: org.burningwave.core 12.62.7 requires 'java.package; io.github.toolfactory.jvm 9.4.3' but it could not be found
[ERROR]   Cannot satisfy dependency: org.eclipse.wb.core 1.12.0.qualifier depends on: osgi.bundle; org.burningwave.core 12.62.7

Not compatible with Java 22

Caused by: io.github.toolfactory.jvm.Driver$InitializeException: Could not initialize NativeDriver
	at io.github.toolfactory.jvm.DriverAbst.init(DriverAbst.java:177)
	at top.outlands.foundation.boot.JVMDriverHolder.<clinit>(JVMDriverHolder.java:15)
	... 12 more
Caused by: io.github.toolfactory.jvm.util.ObjectProvider$BuildingException: Exception occurred while retrieving the implementation of class io.github.toolfactory.jvm.function.catalog.ConstructorInvokeFunction (jvm architecture: x64, jvm version: 22, jvm vendor: Azul Systems, Inc.)
	at io.github.toolfactory.jvm.util.ObjectProvider.getOrBuildObject(ObjectProvider.java:95)
	at io.github.toolfactory.jvm.DriverAbst.getOrBuildConstructorInvoker(DriverAbst.java:411)
	at io.github.toolfactory.jvm.DriverAbst.init(DriverAbst.java:136)
	... 13 more
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(Unknown Source)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Unknown Source)
	at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
	at io.github.toolfactory.jvm.util.ObjectProvider.getOrBuildObjectInternal(ObjectProvider.java:144)
	at io.github.toolfactory.jvm.util.ObjectProvider.getOrBuildObject(ObjectProvider.java:92)
	... 15 more
Caused by: java.lang.ClassNotFoundException: jdk.internal.reflect.NativeConstructorAccessorImpl
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
	at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Unknown Source)
	at java.base/java.lang.Class.forName(Unknown Source)
	at io.github.toolfactory.jvm.function.catalog.ConstructorInvokeFunction$ForJava9.<init>(ConstructorInvokeFunction.java:70)
	... 20 more

NativeConstructorAccessorImpl is gone in Java 22

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.