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)}
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)
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)
New class will inherit type annotations and constructor with annotations (if base class use constructor injection). Also constructor inherits all annotations, including parameters annotations.
T - typetype - interface or abstract class