Package com.google.inject.internal
Class BytecodeGen
- java.lang.Object
-
- com.google.inject.internal.BytecodeGen
-
public final class BytecodeGen extends java.lang.ObjectUtility methods for runtime code generation and class loading. We use this stuff forfaster reflection,method interceptorsand to proxy circular dependencies.When loading classes, we need to be careful of:
- Memory leaks. Generated classes need to be garbage collected in long-lived applications. Once an injector and any instances it created can be garbage collected, the corresponding generated classes should be collectable.
- Visibility. Containers like
OSGiuse class loader boundaries to enforce modularity at runtime.
For each generated class, there's multiple class loaders involved:
- The related class's class loader. Every generated class services exactly one user-supplied class. This class loader must be used to access members with protected and package visibility.
- Guice's class loader.
- Our bridge class loader. This is a child of the user's class loader. It selectively delegates to either the user's class loader (for user classes) or the Guice class loader (for internal classes that are used by the generated classes). This class loader that owns the classes generated by Guice.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classBytecodeGen.BridgeClassLoaderLoader for Guice-generated classes.private static classBytecodeGen.SystemBridgeHolderstatic classBytecodeGen.VisibilityThe required visibility of a user's class from a Guice-generated class.
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.lang.StringCGLIB_PACKAGEeither "net.sf.cglib", or "com.google.inject.internal.cglib"private static com.google.common.cache.LoadingCache<java.lang.ClassLoader,java.lang.ClassLoader>CLASS_LOADER_CACHEWeak cache of bridge class loaders that make the Guice implementation classes visible to various code-generated proxies of client classes.(package private) static net.sf.cglib.core.NamingPolicyENHANCER_NAMING_POLICY(package private) static net.sf.cglib.core.NamingPolicyFASTCLASS_NAMING_POLICY(package private) static java.lang.ClassLoaderGUICE_CLASS_LOADER(package private) static java.lang.StringGUICE_INTERNAL_PACKAGEie.(package private) static java.util.logging.Loggerlogger
-
Constructor Summary
Constructors Constructor Description BytecodeGen()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.lang.ClassLoadercanonicalize(java.lang.ClassLoader classLoader)Attempts to canonicalize null references to the system class loader.static java.lang.ClassLoadergetClassLoader(java.lang.Class<?> type)Returns the class loader to host generated classes fortype.private static java.lang.ClassLoadergetClassLoader(java.lang.Class<?> type, java.lang.ClassLoader delegate)private static booleanhasSameVersionOfCglib(java.lang.ClassLoader classLoader)Returns true if the types classloader has the same version of cglib that BytecodeGen has.private static booleanisPubliclyCallable(java.lang.reflect.Member member)Returns true if the member can be called by a fast class generated in a different classloader.static net.sf.cglib.proxy.EnhancernewEnhancer(java.lang.Class<?> type, BytecodeGen.Visibility visibility)static net.sf.cglib.reflect.FastClassnewFastClassForMember(java.lang.Class<?> type, java.lang.reflect.Member member)Returns a FastClass proxy for invoking the given member ornullif access rules disallow it.static net.sf.cglib.reflect.FastClassnewFastClassForMember(java.lang.reflect.Member member)Returns a FastClass proxy for invoking the given member ornullif access rules disallow it.
-
-
-
Field Detail
-
logger
static final java.util.logging.Logger logger
-
GUICE_CLASS_LOADER
static final java.lang.ClassLoader GUICE_CLASS_LOADER
-
GUICE_INTERNAL_PACKAGE
static final java.lang.String GUICE_INTERNAL_PACKAGE
ie. "com.google.inject.internal"
-
CGLIB_PACKAGE
static final java.lang.String CGLIB_PACKAGE
either "net.sf.cglib", or "com.google.inject.internal.cglib"
-
FASTCLASS_NAMING_POLICY
static final net.sf.cglib.core.NamingPolicy FASTCLASS_NAMING_POLICY
-
ENHANCER_NAMING_POLICY
static final net.sf.cglib.core.NamingPolicy ENHANCER_NAMING_POLICY
-
CLASS_LOADER_CACHE
private static final com.google.common.cache.LoadingCache<java.lang.ClassLoader,java.lang.ClassLoader> CLASS_LOADER_CACHE
Weak cache of bridge class loaders that make the Guice implementation classes visible to various code-generated proxies of client classes.
-
-
Method Detail
-
canonicalize
private static java.lang.ClassLoader canonicalize(java.lang.ClassLoader classLoader)
Attempts to canonicalize null references to the system class loader. May return null if for some reason the system loader is unavailable.
-
getClassLoader
public static java.lang.ClassLoader getClassLoader(java.lang.Class<?> type)
Returns the class loader to host generated classes fortype.
-
getClassLoader
private static java.lang.ClassLoader getClassLoader(java.lang.Class<?> type, java.lang.ClassLoader delegate)
-
newFastClassForMember
public static net.sf.cglib.reflect.FastClass newFastClassForMember(java.lang.reflect.Member member)
Returns a FastClass proxy for invoking the given member ornullif access rules disallow it.- See Also:
for a full description
-
newFastClassForMember
public static net.sf.cglib.reflect.FastClass newFastClassForMember(java.lang.Class<?> type, java.lang.reflect.Member member)Returns a FastClass proxy for invoking the given member ornullif access rules disallow it.FastClass works by generating a type in the same package as the target
type. This may or may not work depending on the access level of the class/member. It breaks down into the following cases depending on accessibility:- Public: This always works since we can generate the type into the
BytecodeGen.BridgeClassLoaderwhich ensures there are no versioning issues. - Package private and Protected: This works as long as:
- We can generate into the same classloader as the type. This is not possible for JDK types which use the 'bootstrap' loader.
- The classloader of the type has the same version of
FastClassas we do. This may be violated when running in OSGI bundles.
- Private: This never works.
- Public: This always works since we can generate the type into the
-
hasSameVersionOfCglib
private static boolean hasSameVersionOfCglib(java.lang.ClassLoader classLoader)
Returns true if the types classloader has the same version of cglib that BytecodeGen has. This only returns false in strange OSGI situations, but it prevents us from using FastClass for non public members.
-
isPubliclyCallable
private static boolean isPubliclyCallable(java.lang.reflect.Member member)
Returns true if the member can be called by a fast class generated in a different classloader.
-
newEnhancer
public static net.sf.cglib.proxy.Enhancer newEnhancer(java.lang.Class<?> type, BytecodeGen.Visibility visibility)
-
-