ToolFactory JVM Driver
A driver to allow 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>4.0.0</version>
</dependency>
Overview
There are two kinds of driver:
- the default driver completely based on Java api
- 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
All JNI methods used by the native and the hybrid driver are supplied by narcissus that works on the following system configurations:
- Windows (x86, x64)
- Linux (x86, x64)
- MacOs (x64)
Usage
To create a default driver instance you should use this code:
io.github.toolfactory.jvm.Driver driver = new io.github.toolfactory.jvm.DefaultDriver();
To create a hybrid driver instance you should use this code:
io.github.toolfactory.jvm.Driver driver = new io.github.toolfactory.jvm.HybridDriver();
To create a native driver instance you should use this code:
io.github.toolfactory.jvm.Driver driver = new io.github.toolfactory.jvm.NativeDriver();
The methods exposed by the Driver interface are the following:
public Class<?> defineHookClass(Class<?> clientClass, byte[] byteCode);
public Class<?> getBuiltinClassLoaderClass();
public Class<?> getClassLoaderDelegateClass();
public MethodHandles.Lookup getConsulter(Class<?> cls);
public <T> Constructor<T>[] getDeclaredConstructors(Class<T> cls);
public Field getDeclaredField(Class<?> cls, String name);
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 <T> T invoke(Method method, Object target, Object[] params);
public boolean isBuiltinClassLoader(ClassLoader classLoader);
public boolean isClassLoaderDelegate(ClassLoader classLoader);
public <T> T newInstance(Constructor<T> ctor, Object[] params);
public Collection<Class<?>> retrieveLoadedClasses(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 <T> T throwException(Object exceptionOrMessage, Object... placeHolderReplacements);
Compilation requirements
A JDK version 9 or higher is required to compile the project.
Ask for assistance
For assistance you can:
- open a discussion here on GitHub
- report a bug here on GitHub
- ask on Stack Overflow