Package com.google.inject.internal
Class BytecodeGen
- java.lang.Object
-
- com.google.inject.internal.BytecodeGen
-
public final class BytecodeGen extends Object
Utility methods for runtime code generation and class loading. We use this stuff forfaster reflection
,method interceptors
and 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
OSGi
use 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.
- Author:
- mcculls@gmail.com (Stuart McCulloch), jessewilson@google.com (Jesse Wilson)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BytecodeGen.Visibility
The required visibility of a user's class from a Guice-generated class.
-
Constructor Summary
Constructors Constructor Description BytecodeGen()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ClassLoader
getClassLoader(Class<?> type)
Returns the class loader to host generated classes fortype
.static net.sf.cglib.proxy.Enhancer
newEnhancer(Class<?> type, BytecodeGen.Visibility visibility)
static net.sf.cglib.reflect.FastClass
newFastClassForMember(Class<?> type, Member member)
Returns a FastClass proxy for invoking the given member ornull
if access rules disallow it.static net.sf.cglib.reflect.FastClass
newFastClassForMember(Member member)
Returns a FastClass proxy for invoking the given member ornull
if access rules disallow it.
-
-
-
Method Detail
-
getClassLoader
public static ClassLoader getClassLoader(Class<?> type)
Returns the class loader to host generated classes fortype
.
-
newFastClassForMember
public static net.sf.cglib.reflect.FastClass newFastClassForMember(Member member)
Returns a FastClass proxy for invoking the given member ornull
if access rules disallow it.- See Also:
for a full description
-
newFastClassForMember
public static net.sf.cglib.reflect.FastClass newFastClassForMember(Class<?> type, Member member)
Returns a FastClass proxy for invoking the given member ornull
if 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.BridgeClassLoader
which 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
FastClass
as 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
-
newEnhancer
public static net.sf.cglib.proxy.Enhancer newEnhancer(Class<?> type, BytecodeGen.Visibility visibility)
-
-