Class JavaDispatcher<T>
- Type Parameters:
T- The resolved type.
- All Implemented Interfaces:
PrivilegedAction<T>
A dispatcher for creating a proxy that invokes methods of a type that is possibly unknown on the current VM. Dispatchers do not use any of Byte Buddy's regular infrastructure, to avoid bootstrapping issues as these dispatchers are used by Byte Buddy itself.
By default, this dispatcher uses the Java Proxy for creating dispatchers. By setting net.bytebuddy.generate to
true, Byte Buddy can generate proxies manually as byte code to mostly avoid reflection and boxing of arguments as arrays.
If a security manager is active, the net.bytebuddy.createJavaDispatcher runtime permission is required. Any dispatching
will be executed from a separate class loader and an unnamed module but with the ProtectionDomain of
the JavaDispatcher class. It is not permitted to invoke methods of the java.security.AccessController class or
to resolve a java.lang.invoke.MethodHandle$Lookup.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceIndicates that the method is supposed to return an array of the proxied type.static @interfaceIndicates that a method is supposed to return a default value if a method or type could not be resolved.private static classAnInvokerthat uses Byte Buddy's invocation context to use if dynamic class loading is not supported, for example on Android, and that do not use secured contexts, where this security measure is obsolete to begin with.protected static interfaceA dispatcher for handling a proxied method.protected static classA class loader for loading synthetic classes for implementing aJavaDispatcher.static @interfaceIndicates that a method is supposed to perform an instance check.private static classA privileged action for creating anInvoker.static @interfaceIndicates that a proxied method is a constructor.static @interfaceIndicates that a proxied method is static.static @interfaceIndicates a proxied type's name.protected static classAn invocation handler that invokes given dispatchers. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ClassLoaderThe class loader to resolve the proxied type from ornullif the bootstrap loader should be used.private final booleantrueif a proxy class should be manually generated.private static final booleanIftrue, dispatcher classes will be generated natively and not by using aProxy.static final StringA property to determine, that iftrue, dispatcher classes will be generated natively and not by using aProxy.private static final InvokerContains an invoker that makes sure that reflective dispatchers make invocations from an isolatedClassLoaderand not from within Byte Buddy's context.The proxy type.private static final JavaDispatcher.DynamicClassLoader.ResolverA resolver to assure that a type's package and module are exported to the created class loader. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedJavaDispatcher(Class<T> proxy, ClassLoader classLoader, boolean generate) Creates a new dispatcher. -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T> TdoPrivileged(PrivilegedAction<T> action) A proxy forjava.security.AccessController#doPrivilegedthat is activated if available.static <T> PrivilegedAction<T> Resolves an action for creating a dispatcher for the provided type where the proxied type is resolved from the bootstrap loader.protected static <T> PrivilegedAction<T> of(Class<T> type, ClassLoader classLoader) Resolves an action for creating a dispatcher for the provided type.protected static <T> PrivilegedAction<T> of(Class<T> type, ClassLoader classLoader, boolean generate) Resolves an action for creating a dispatcher for the provided type.run()
-
Field Details
-
GENERATE_PROPERTY
A property to determine, that iftrue, dispatcher classes will be generated natively and not by using aProxy.- See Also:
-
GENERATE
private static final boolean GENERATEIftrue, dispatcher classes will be generated natively and not by using aProxy. -
RESOLVER
A resolver to assure that a type's package and module are exported to the created class loader. This should normally always be the case, but if another library is shading Byte Buddy or otherwise manipulates the module graph, this might become necessary. -
INVOKER
Contains an invoker that makes sure that reflective dispatchers make invocations from an isolatedClassLoaderand not from within Byte Buddy's context. This way, no privilege context can be leaked by accident. -
proxy
The proxy type. -
classLoader
The class loader to resolve the proxied type from ornullif the bootstrap loader should be used. -
generate
private final boolean generatetrueif a proxy class should be manually generated.
-
-
Constructor Details
-
JavaDispatcher
Creates a new dispatcher.- Parameters:
proxy- The proxy type.classLoader- The class loader to resolve the proxied type from ornullif the bootstrap loader should be used.generate-trueif a proxy class should be manually generated.
-
-
Method Details
-
doPrivileged
A proxy forjava.security.AccessController#doPrivilegedthat is activated if available.- Type Parameters:
T- The type of the action's resolved value.- Parameters:
action- The action to execute from a privileged context.- Returns:
- The action's resolved value.
-
of
Resolves an action for creating a dispatcher for the provided type where the proxied type is resolved from the bootstrap loader.- Type Parameters:
T- The resolved type.- Parameters:
type- The type for which a dispatcher should be resolved.- Returns:
- An action for creating an appropriate dispatcher.
-
of
Resolves an action for creating a dispatcher for the provided type.- Type Parameters:
T- The resolved type.- Parameters:
type- The type for which a dispatcher should be resolved.classLoader- The class loader to resolve the proxied type from.- Returns:
- An action for creating an appropriate dispatcher.
-
of
protected static <T> PrivilegedAction<T> of(Class<T> type, @MaybeNull ClassLoader classLoader, boolean generate) Resolves an action for creating a dispatcher for the provided type.- Type Parameters:
T- The resolved type.- Parameters:
type- The type for which a dispatcher should be resolved.classLoader- The class loader to resolve the proxied type from.generate-trueif a proxy class should be manually generated.- Returns:
- An action for creating an appropriate dispatcher.
-
run
- Specified by:
runin interfacePrivilegedAction<T>
-