Class DynamicClassGenerator


  • public final class DynamicClassGenerator
    extends java.lang.Object
    Dynamically generates new class from abstract class or interface. Resulted class may be used as implementation in guice. Generated class will be tied to class loader of original type. It is safe to to use with dynamic class loaders (like play dev mode).

    Guice will be able to apply aop to generated class methods. Annotations are copied from original class, to let aop mechanisms work even for interfaces. As a result you may forget about class generation and think of abstract class or interface as usual guice bean.

    Abstract methods will not be implemented: abstract method call error will be thrown if you try to call it. All abstract methods must be covered by guice aop.

    It may be used directly in guice module to register interface or abstract class: bind(MyType.class).to(DynamicClassGenerator.generate(MyType.class)). Another option is to use DynamicClassProvider: annotate type with @ProvidedBy(DynamicClassProvider.class) and either rely on JIT (don't register type at all) or simply register type: @{code bind(MyType.class)}. If used with injectors hierarchy or within private modules, use "anchor" dependency to prevent bubbling up for resulted binding (see for example anchor implementation for use with @ProvidedBy annotation GeneratorAnchorModule).

    Don't use scope annotations directly - instead wrap them into ScopeAnnotation, because guice doesn't allow scope definition on abstract types.

    Since:
    10.12.2014
    See Also:
    DynamicClassProvider
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DYNAMIC_CLASS_POSTFIX
      Postfix applied to interface or abstract class name to get generated class name.
      static java.lang.String JAKARTA_INJECT  
      static java.lang.String JAVAX_INJECT  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.Class<T> generate​(java.lang.Class<T> type)
      Shortcut for generate(Class, Class, Class) method to create default scoped classes.
      static <T> java.lang.Class<T> generate​(java.lang.Class<T> type, java.lang.Class<? extends java.lang.annotation.Annotation> scope)
      Shortcut for generate(Class, Class, Class) method to create classes with provided scope (and without extra anchor).
      static <T> java.lang.Class<T> generate​(java.lang.Class<T> type, java.lang.Class<? extends java.lang.annotation.Annotation> scope, java.lang.Class<?> anchor)
      Generates dynamic class, which guice may use as implementation and generate proxy above it, correctly applying aop features.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DYNAMIC_CLASS_POSTFIX

        public static final java.lang.String DYNAMIC_CLASS_POSTFIX
        Postfix applied to interface or abstract class name to get generated class name.
        See Also:
        Constant Field Values
    • Method Detail

      • generate

        public static <T> java.lang.Class<T> generate​(java.lang.Class<T> type)
        Shortcut for generate(Class, Class, Class) method to create default scoped classes.

        Method is thread safe.

        Type Parameters:
        T - type
        Parameters:
        type - interface or abstract class
        Returns:
        implementation class for provided type (will not generate if class already exist)
      • generate

        public static <T> java.lang.Class<T> generate​(java.lang.Class<T> type,
                                                      java.lang.Class<? extends java.lang.annotation.Annotation> scope)
        Shortcut for generate(Class, Class, Class) method to create classes with provided scope (and without extra anchor).

        Method is thread safe.

        Type Parameters:
        T - type
        Parameters:
        type - interface or abstract class
        scope - scope annotation to apply on generated class (may be null for default prototype scope)
        Returns:
        implementation class for provided type (will not generate if class already exist)
      • generate

        public static <T> java.lang.Class<T> generate​(java.lang.Class<T> type,
                                                      java.lang.Class<? extends java.lang.annotation.Annotation> scope,
                                                      java.lang.Class<?> anchor)
        Generates dynamic class, which guice may use as implementation and generate proxy above it, correctly applying aop features.

        New class will inherit type annotations and constructor with annotations (if base class use constructor injection). Also constructor inherits all annotations, including parameters annotations. If anchor is provided then it will be added as last constructor parameter or (when abstract type has no constructor) new constructor added with one parameter (anchor).

        Method is thread safe.

        Type Parameters:
        T - type
        Parameters:
        type - interface or abstract class
        scope - scope annotation to apply on generated class (may be null for default prototype scope)
        anchor - existing binding to depend generated class on (to prevent binding bubbling up to root injector)
        Returns:
        implementation class for provided type (will not generate if class already exist)
        See Also:
        for more details about anchor usage