Class BytecodeGen


  • public final class BytecodeGen
    extends java.lang.Object
    Utility methods for runtime code generation and class loading. We use this stuff for faster 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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String CGLIB_PACKAGE  
      private static 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.
      (package private) static java.lang.ClassLoader GUICE_CLASS_LOADER  
      (package private) static java.lang.String GUICE_INTERNAL_PACKAGE
      ie.
      (package private) static java.util.logging.Logger logger  
    • Constructor Summary

      Constructors 
      Constructor Description
      BytecodeGen()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.ClassLoader canonicalize​(java.lang.ClassLoader classLoader)
      Attempts to canonicalize null references to the system class loader.
      static java.lang.ClassLoader getClassLoader​(java.lang.Class<?> type)
      Returns the class loader to host generated classes for type.
      private static java.lang.ClassLoader getClassLoader​(java.lang.Class<?> type, java.lang.ClassLoader delegate)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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"
      • 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.
    • Constructor Detail

      • BytecodeGen

        public BytecodeGen()
    • 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 for type.
      • getClassLoader

        private static java.lang.ClassLoader getClassLoader​(java.lang.Class<?> type,
                                                            java.lang.ClassLoader delegate)