Skip to content

About

A driver to allow deep interaction with the JVM without any restrictions

Topics

Resources

Stars

65 stars

Watchers

2 watching

Forks

Latest commit

 

History

2,848 Commits

Folders and files

Repository files navigation

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>10.1.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 with the default JNI engine
requires org.burningwave.jni;

//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, or if the memory-access methods of sun.misc.Unsafe are not available (they can be disabled with --sun-misc-unsafe-memory-access=deny and they will be removed from the JDK, see JEP 471)
  • 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 default driver and uses JNI functions more consistently regardless of the Java version it is running on

All JNI methods used by the dynamic, hybrid and native driver are supplied by a JNI engine. Starting from version 10.0.0 the default JNI engine is the JNI Executor of Burningwave, which took the place of Narcissus: the switch is based on the excellent results it obtained in benchmarks (this one and the Burningwave JVM Driver benchmark). The Burningwave engine works on the following system configurations:

  • Windows (x86, x64, arm64)
  • Linux (x86, x64, arm64)
  • macOS (x64, arm64)

Narcissus is still supported: to use it instead of the Burningwave engine, just add the Narcissus plugin to your dependencies. Narcissus 1.0.14 works on Windows (x64, arm64), Linux (x64, arm64) and macOS (x64, arm64).

On JDK 24 and later the JVM prints a warning when a JNI engine loads its native library, and a future JDK release will block it unless native access is enabled. To enable it, start the JVM with --enable-native-access=ALL-UNNAMED or, on the module path, with the name of the module of the engine (--enable-native-access=org.burningwave.jni or --enable-native-access=io.github.toolfactory.narcissus); for an executable jar you can instead add Enable-Native-Access: ALL-UNNAMED to its manifest.

Prefer the default driver

The default driver only uses Java APIs and it is faster than the drivers that use JNI functions: the JIT compiles its operations like any other Java code, while every operation that the dynamic, hybrid and native drivers delegate to a JNI function pays the cost of a native call. In a JMH benchmark run on JDK 18 the default driver read a field at 41,581 ops/ms against the 16,982 ops/ms of the native driver, and wrote it at 37,480 ops/ms against 19,672 ops/ms.

The JNI functions exist to get around the strong encapsulation of the JDK, not to be faster: use the dynamic, hybrid or native driver only when the default driver cannot be initialized on your JVM.

Driver.Factory.getNew() already works this way, since it returns the first driver that can be initialized among the default, hybrid and native drivers.

Using Narcissus

To make the drivers use Narcissus instead of the Burningwave engine, add the Narcissus plugin:

<dependency>
    <groupId>io.github.toolfactory</groupId>
    <artifactId>jvm-driver-narcissus-plugin</artifactId>
    <version>1.1.1</version>
</dependency>

Nothing else is needed: the plugin contains a jvm-driver.properties file with priority 1 that sets the engine to io.github.toolfactory.jvm.narcissus.NarcissusEngine. The plugin depends on ToolFactory JVM Driver and on Narcissus 1.0.14: to leave out the Burningwave engine, exclude org.burningwave:jni-executor from its dependencies.

On the module path, require the module of the plugin, which in turn requires the modules of ToolFactory JVM Driver and of Narcissus:

requires io.github.toolfactory.jvm.narcissus;

Choosing the JNI engine

The engine is an implementation of the abstract class io.github.toolfactory.jvm.NativeEngine, named by the native-engine.class property of the jvm-driver.properties file with the highest priority that sets it; the jvm-driver.properties files with the same value of priority-of-this-configuration are merged. ToolFactory JVM Driver sets it to io.github.toolfactory.jvm.BurningwaveEngine with priority 0 and the Narcissus plugin to io.github.toolfactory.jvm.narcissus.NarcissusEngine with priority 1. To choose an engine regardless of the plugins in the class path, add to your project a jvm-driver.properties file with a higher priority, for example:

priority-of-this-configuration=2
native-engine.class=io.github.toolfactory.jvm.BurningwaveEngine

It can also name your own engine: a public class that extends io.github.toolfactory.jvm.NativeEngine and has a public constructor without parameters.

If the chosen engine cannot be loaded, the drivers that need it cannot be initialized, because no other engine is tried: the catalog of the functions and NativeEngine.getInstance() throw the InitializeException of the io.github.toolfactory.jvm.function package, which the module io.github.toolfactory.jvm exports.


Usage

To create a driver instance you should use this code:

io.github.toolfactory.jvm.Driver driver = io.github.toolfactory.jvm.Driver.Factory.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.Factory.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);

If you need a complete reflection engine and not only the driver, see Burningwave Reflection: it handles the private and all the other members of an object, gets and sets the properties of Java beans through paths, and uses the JVM Driver only if it is in the class path.

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:

About

A driver to allow deep interaction with the JVM without any restrictions

Topics

Resources

Stars

65 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages