@SuppressWarnings("unchecked")
    public static <T> Class<T> generate(final Class<T> type,
                                        final Class<? extends java.lang.annotation.Annotation> scope,
                                        final Class<?> anchor) {
        Preconditions.checkNotNull(type, "Original type required");
        Preconditions.checkArgument(type.isInterface() || Modifier.isAbstract(type.getModifiers()),
                "Type must be interface or abstract class, but provided type is not: %s", type.getName());

        final String targetClassName = type.getName() + DYNAMIC_CLASS_POSTFIX;
        final ClassLoader classLoader = type.getClassLoader();

        /*
         * Synchronization is required to avoid double generation and consequent problems.
         * Very unlikely that this method would be called too often and synchronization become bottleneck.
         * Using original class as monitor to allow concurrent generation for different classes.
         */
        synchronized (type) {
            Class<?> targetClass;
            try {
                // will work if class was already generated
                targetClass = classLoader.loadClass(targetClassName);
            } catch (ClassNotFoundException ex) {
                targetClass = generateClass(type, targetClassName, classLoader, scope, anchor);
            }
            return (Class<T>) targetClass;
        }
    }
@ SuppressWarnings ( "unchecked" ) public static < T > Class < T > generate ( final Class < T > type , final Class < ? extends java . lang . annotation . Annotation > scope , final Class < ? > anchor ) { Preconditions . checkNotNull ( type , "Original type required" ) ; Preconditions . checkArgument ( type . isInterface ( ) || Modifier . isAbstract ( type . getModifiers ( ) ) , "Type must be interface or abstract class, but provided type is not: %s" , type . getName ( ) ) ; final String targetClassName = type . getName ( ) + DYNAMIC_CLASS_POSTFIX ; final ClassLoader classLoader = type . getClassLoader ( ) ; synchronized ( type ) { Class < ? > targetClass ; try { targetClass = classLoader . loadClass ( targetClassName ) ; } catch ( ClassNotFoundException ex ) { targetClass = generateClass ( type , targetClassName , classLoader , scope , anchor ) ; } return ( Class < T > ) targetClass ; } }
Generates dynamic class, which guice may use as implementation and generate proxy above it,
correctly applying aop features.
<p>
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).
<p>
Method is thread safe.

@param type   interface or abstract class
@param scope  scope annotation to apply on generated class (may be null for default prototype scope)
@param anchor existing binding to depend generated class on (to prevent binding bubbling up to root injector)
@param <T>    type
@return implementation class for provided type (will not generate if class already exist)
@see ru.vyarus.guice.ext.core.generator.anchor.GeneratorAnchorModule for more details about anchor usage