Class JavaDispatcher<T>
- java.lang.Object
-
- net.bytebuddy.utility.dispatcher.JavaDispatcher<T>
-
- Type Parameters:
T
- The resolved type.
- All Implemented Interfaces:
java.security.PrivilegedAction<T>
@Enhance public class JavaDispatcher<T> extends java.lang.Object implements java.security.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 settingnet.bytebuddy.generate
totrue
, 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 theJavaDispatcher
class. It is not permitted to invoke methods of thejava.security.AccessController
class or to resolve ajava.lang.invoke.MethodHandle$Lookup
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
JavaDispatcher.Container
Indicates that the method is supposed to return an array of the proxied type.static interface
JavaDispatcher.Defaults
Indicates that a method is supposed to return a default value if a method or type could not be resolved.private static class
JavaDispatcher.DirectInvoker
AnInvoker
that 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 interface
JavaDispatcher.Dispatcher
A dispatcher for handling a proxied method.protected static class
JavaDispatcher.DynamicClassLoader
A class loader for loading synthetic classes for implementing aJavaDispatcher
.static interface
JavaDispatcher.Instance
Indicates that a method is supposed to perform an instance check.private static class
JavaDispatcher.InvokerCreationAction
A privileged action for creating anInvoker
.static interface
JavaDispatcher.IsConstructor
Indicates that a proxied method is a constructor.static interface
JavaDispatcher.IsStatic
Indicates that a proxied method is static.static interface
JavaDispatcher.Proxied
Indicates a proxied type's name.protected static class
JavaDispatcher.ProxiedInvocationHandler
An invocation handler that invokes given dispatchers.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ClassLoader
classLoader
The class loader to resolve the proxied type from ornull
if the bootstrap loader should be used.private boolean
generate
true
if a proxy class should be manually generated.private static boolean
GENERATE
Iftrue
, dispatcher classes will be generated natively and not by using aProxy
.static java.lang.String
GENERATE_PROPERTY
A property to determine, that iftrue
, dispatcher classes will be generated natively and not by using aProxy
.private static Invoker
INVOKER
Contains an invoker that makes sure that reflective dispatchers make invocations from an isolatedClassLoader
and not from within Byte Buddy's context.private java.lang.Class<T>
proxy
The proxy type.private static JavaDispatcher.DynamicClassLoader.Resolver
RESOLVER
A resolver to assure that a type's package and module are exported to the created class loader.
-
Constructor Summary
Constructors Modifier Constructor Description protected
JavaDispatcher(java.lang.Class<T> proxy, java.lang.ClassLoader classLoader, boolean generate)
Creates a new dispatcher.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static <T> T
doPrivileged(java.security.PrivilegedAction<T> action)
A proxy forjava.security.AccessController#doPrivileged
that is activated if available.static <T> java.security.PrivilegedAction<T>
of(java.lang.Class<T> type)
Resolves an action for creating a dispatcher for the provided type where the proxied type is resolved from the bootstrap loader.static <T> java.security.PrivilegedAction<T>
of(java.lang.Class<T> type, java.lang.ClassLoader classLoader)
Resolves an action for creating a dispatcher for the provided type.protected static <T> java.security.PrivilegedAction<T>
of(java.lang.Class<T> type, java.lang.ClassLoader classLoader, boolean generate)
Resolves an action for creating a dispatcher for the provided type.T
run()
-
-
-
Field Detail
-
GENERATE_PROPERTY
public static final java.lang.String GENERATE_PROPERTY
A property to determine, that iftrue
, dispatcher classes will be generated natively and not by using aProxy
.- See Also:
- Constant Field Values
-
GENERATE
private static final boolean GENERATE
Iftrue
, dispatcher classes will be generated natively and not by using aProxy
.
-
RESOLVER
private static final JavaDispatcher.DynamicClassLoader.Resolver 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
private static final Invoker INVOKER
Contains an invoker that makes sure that reflective dispatchers make invocations from an isolatedClassLoader
and not from within Byte Buddy's context. This way, no privilege context can be leaked by accident.
-
proxy
private final java.lang.Class<T> proxy
The proxy type.
-
classLoader
@MaybeNull @ValueHandling(REVERSE_NULLABILITY) private final java.lang.ClassLoader classLoader
The class loader to resolve the proxied type from ornull
if the bootstrap loader should be used.
-
generate
private final boolean generate
true
if a proxy class should be manually generated.
-
-
Constructor Detail
-
JavaDispatcher
protected JavaDispatcher(java.lang.Class<T> proxy, @MaybeNull java.lang.ClassLoader classLoader, boolean generate)
Creates a new dispatcher.- Parameters:
proxy
- The proxy type.classLoader
- The class loader to resolve the proxied type from ornull
if the bootstrap loader should be used.generate
-true
if a proxy class should be manually generated.
-
-
Method Detail
-
doPrivileged
@Enhance private static <T> T doPrivileged(java.security.PrivilegedAction<T> action)
A proxy forjava.security.AccessController#doPrivileged
that 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
public static <T> java.security.PrivilegedAction<T> of(java.lang.Class<T> type)
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
public static <T> java.security.PrivilegedAction<T> of(java.lang.Class<T> type, @MaybeNull java.lang.ClassLoader classLoader)
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> java.security.PrivilegedAction<T> of(java.lang.Class<T> type, @MaybeNull java.lang.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
-true
if a proxy class should be manually generated.- Returns:
- An action for creating an appropriate dispatcher.
-
-