public final class DynamicClassGenerator
extends java.lang.Object
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.
DynamicClassProvider| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DYNAMIC_CLASS_POSTFIX
Postfix applied to interface or abstract class name to get generated class name.
|
| Modifier and Type | Method and 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.
|
public static final java.lang.String DYNAMIC_CLASS_POSTFIX
public static <T> java.lang.Class<T> generate(java.lang.Class<T> type)
generate(Class, Class, Class) method to create default scoped classes.
Method is thread safe.
T - typetype - interface or abstract classpublic static <T> java.lang.Class<T> generate(java.lang.Class<T> type,
java.lang.Class<? extends java.lang.annotation.Annotation> scope)
generate(Class, Class, Class) method to create classes with provided scope
(and without extra anchor).
Method is thread safe.
T - typetype - interface or abstract classscope - scope annotation to apply on generated class (may be null for default prototype scope)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)
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.
T - typetype - interface or abstract classscope - 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)for more details about anchor usage