Interface ReflectiveAccess
public interface ReflectiveAccess
This interface is used to register classes, methods, and fields for use with the
java.lang.reflect API at run time, and for serialization at run time. An instance of this
interface is acquired via Feature.AfterRegistrationAccess.getReflectiveAccess().
All methods in ReflectiveAccess require a AccessCondition as their first
parameter. A class and its members will be accessible at run-time only if the specified condition
is satisfied.
How to use
ReflectiveAccess should only be used during Feature.afterRegistration(Feature.AfterRegistrationAccess). Attempts
to register metadata in any other phase of the Feature API will result in an error.
Example:
@Override
public void afterRegistration(AfterRegistrationAccess access) {
ReflectionAccess reflection = access.getReflectionAccess();
AccessCondition condition = AccessCondition.typeReached(ConditionType.class);
reflection.register(condition, Foo.class, Bar.class);
reflection.register(AccessCondition.unconditional(), Foo.class.getMethod("method"));
reflection.registerForUnsafeAllocation(condition, Foo.class);
Class<?> proxyClass = reflection.registerProxy(condition, Interface1.class, Interface2.class);
reflection.registerForSerialization(AccessCondition.unconditional(), proxyClass);
}
- Since:
- 25.0.1
-
Method Summary
Modifier and TypeMethodDescriptionvoidregister(AccessCondition condition, Class<?>... classes) Registers the provided classes for reflection at run time, if theconditionis satisfied.voidregister(AccessCondition condition, Executable... executables) Registers the providedexecutables(methods and constructors) for reflective invocation at run time, if theconditionis satisfied.voidregister(AccessCondition condition, Field... fields) Registers the providedfieldsfor reflective access at run time, if theconditionis satisfied.voidregisterForSerialization(AccessCondition condition, Class<?>... classes) Registers the provided classes for serialization at run time, if theconditionis satisfied.voidregisterForUnsafeAllocation(AccessCondition condition, Class<?>... classes) Registers the providedclassesfor unsafe allocation at run time, if theconditionis satisfied.Class<?> registerProxy(AccessCondition condition, Class<?>... interfaces) Registers aProxyclass in the system classloader that implements the specifiedinterfaces, if theconditionis satisfied.
-
Method Details
-
register
Registers the provided classes for reflection at run time, if theconditionis satisfied. This means all reflection methods defined by theClass(exceptClass.arrayType()) are accessible at run time for those classes.If a class is not registered for reflection at run time, the following methods will throw
ClassNotFoundException:- Since:
- 25.0.1
-
register
Registers the providedexecutables(methods and constructors) for reflective invocation at run time, if theconditionis satisfied. This method also registers the declaring classes of the provided executables for reflection at run time. The executables will be invocable at run time viaMethod.invoke(java.lang.Object, java.lang.Object...).- Since:
- 25.0.1
-
register
Registers the providedfieldsfor reflective access at run time, if theconditionis satisfied. This method also registers the declaring classes of the provided fields for reflection at run time.The fields will be accessible at run time via following methods:
Field.get(Object)Field.set(Object, Object)Field.getByte(Object)Field.setByte(Object, byte)Field.getChar(Object)Field.setChar(Object, char)Field.getShort(Object)Field.setShort(Object, short)Field.getInt(Object)Field.setInt(Object, int)Field.getLong(Object)Field.setLong(Object, long)Field.getFloat(Object)Field.setFloat(Object, float)Field.getDouble(Object)Field.setDouble(Object, double)
- Since:
- 25.0.1
-
registerForSerialization
Registers the provided classes for serialization at run time, if theconditionis satisfied. This method also registers the provided classes for reflection at run time.The following methods require a type to be registered for serialization:
- Since:
- 25.0.1
-
registerProxy
Registers aProxyclass in the system classloader that implements the specifiedinterfaces, if theconditionis satisfied. The proxy class is fully specified by the interfaces it implements, and proxy instances matching that specification can be created at run time. The returned proxy class can be used in registration for serialization at run time.NOTE: The order of the interfaces provided in the
interfacesparameter is significant; different orderings will produce distinct proxy classes.Example:
Class<?> proxyClass = reflection.registerProxy(AccessCondition.unconditional(), Interface1.class, Interface2.class); reflection.registerForSerialization(AccessCondition.unconditional(), proxyClass);- Returns:
- Proxy class defined by the provided interfaces, or
nullif no such proxy class can be created with the given interfaces - Since:
- 25.0.1
-
registerForUnsafeAllocation
Registers the providedclassesfor unsafe allocation at run time, if theconditionis satisfied. Unsafe allocation can happen viaUnsafe.allocateInstance(Class)or from native code viaAllocObject(jClass).- Since:
- 25.0.1
-