Search in sources :

Example 6 with GroovyClass

use of org.codehaus.groovy.tools.GroovyClass in project groovy by apache.

the class ProxyGeneratorAdapter method adjustSuperClass.

private Class adjustSuperClass(Class superClass, final Class[] interfaces) {
    boolean isSuperClassAnInterface = superClass.isInterface();
    if (!isSuperClassAnInterface) {
        return superClass;
    }
    Class result = Object.class;
    Set<ClassNode> traits = new LinkedHashSet<ClassNode>();
    // check if it's a trait
    collectTraits(superClass, traits);
    if (interfaces != null) {
        for (Class anInterface : interfaces) {
            collectTraits(anInterface, traits);
        }
    }
    if (!traits.isEmpty()) {
        String name = superClass.getName() + "$TraitAdapter";
        ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(new ClassNode[traits.size()]), null);
        CompilationUnit cu = new CompilationUnit(loader);
        CompilerConfiguration config = new CompilerConfiguration();
        SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config));
        cu.addSource(su);
        cu.compile(Phases.CONVERSION);
        su.getAST().addClass(cn);
        cu.compile(Phases.CLASS_GENERATION);
        @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
        for (GroovyClass groovyClass : classes) {
            if (groovyClass.getName().equals(name)) {
                return loader.defineClass(name, groovyClass.getBytes());
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyClass(org.codehaus.groovy.tools.GroovyClass) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) SourceUnit(org.codehaus.groovy.control.SourceUnit) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyClass(org.codehaus.groovy.tools.GroovyClass) GroovyObject(groovy.lang.GroovyObject) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

GroovyClass (org.codehaus.groovy.tools.GroovyClass)6 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)5 ClassNode (org.codehaus.groovy.ast.ClassNode)4 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 GroovyObject (groovy.lang.GroovyObject)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 LinkedHashSet (java.util.LinkedHashSet)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)2 ErrorCollector (org.codehaus.groovy.control.ErrorCollector)2 SourceUnit (org.codehaus.groovy.control.SourceUnit)2 File (java.io.File)1 LinkedList (java.util.LinkedList)1 MethodNode (org.codehaus.groovy.ast.MethodNode)1 ModuleNode (org.codehaus.groovy.ast.ModuleNode)1