Search in sources :

Example 1 with GroovyClass

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

the class StaticTypeCheckingSupport method evaluateExpression.

/**
     * A helper method that can be used to evaluate expressions as found in annotation
     * parameters. For example, it will evaluate a constant, be it referenced directly as
     * an integer or as a reference to a field.
     *
     * If this method throws an exception, then the expression cannot be evaluated on its own.
     *
     * @param expr the expression to be evaluated
     * @param config the compiler configuration
     * @return the result of the expression
     */
public static Object evaluateExpression(Expression expr, CompilerConfiguration config) {
    String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$');
    ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, OBJECT_TYPE);
    ReturnStatement code = new ReturnStatement(expr);
    node.addMethod(new MethodNode("eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code));
    CompilerConfiguration copyConf = new CompilerConfiguration(config);
    CompilationUnit cu = new CompilationUnit(copyConf);
    cu.addClassNode(node);
    cu.compile(Phases.CLASS_GENERATION);
    @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
    Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes());
    try {
        return aClass.getMethod("eval").invoke(null);
    } catch (IllegalAccessException e) {
        throw new GroovyBugError(e);
    } catch (InvocationTargetException e) {
        throw new GroovyBugError(e);
    } catch (NoSuchMethodException e) {
        throw new GroovyBugError(e);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyClass(org.codehaus.groovy.tools.GroovyClass) GroovyBugError(org.codehaus.groovy.GroovyBugError) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodNode(org.codehaus.groovy.ast.MethodNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) GroovyClass(org.codehaus.groovy.tools.GroovyClass)

Example 2 with GroovyClass

use of org.codehaus.groovy.tools.GroovyClass in project webpieces by deanhiller.

the class GroovyToJavaClassCompiler method compileImpl.

private void compileImpl(GroovyClassLoader groovyCl, ScriptOutputImpl scriptCode) {
    CompilationUnit compileUnit = new CompilationUnit();
    compileUnit.addSource(scriptCode.getFullClassName(), scriptCode.getScriptSourceCode());
    compileUnit.compile(Phases.CLASS_GENERATION);
    compileUnit.setClassLoader(groovyCl);
    for (Object compileClass : compileUnit.getClasses()) {
        GroovyClass groovyClass = (GroovyClass) compileClass;
        callbacks.compiledGroovyClass(groovyCl, groovyClass);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) GroovyClass(org.codehaus.groovy.tools.GroovyClass)

Example 3 with GroovyClass

use of org.codehaus.groovy.tools.GroovyClass in project groovy-core by groovy.

the class StaticTypeCheckingSupport method evaluateExpression.

/**
     * A helper method that can be used to evaluate expressions as found in annotation
     * parameters. For example, it will evaluate a constant, be it referenced directly as
     * an integer or as a reference to a field.
     *
     * If this method throws an exception, then the expression cannot be evaluated on its own.
     *
     * @param expr the expression to be evaluated
     * @param config the compiler configuration
     * @return the result of the expression
     */
public static Object evaluateExpression(Expression expr, CompilerConfiguration config) {
    String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$');
    ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    ReturnStatement code = new ReturnStatement(expr);
    node.addMethod(new MethodNode("eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code));
    CompilerConfiguration copyConf = new CompilerConfiguration(config);
    CompilationUnit cu = new CompilationUnit(copyConf);
    cu.addClassNode(node);
    cu.compile(Phases.CLASS_GENERATION);
    @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
    Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes());
    try {
        return aClass.getMethod("eval").invoke(null);
    } catch (IllegalAccessException e) {
        throw new GroovyBugError(e);
    } catch (InvocationTargetException e) {
        throw new GroovyBugError(e);
    } catch (NoSuchMethodException e) {
        throw new GroovyBugError(e);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) GroovyClass(org.codehaus.groovy.tools.GroovyClass) GroovyBugError(org.codehaus.groovy.GroovyBugError) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyClass(org.codehaus.groovy.tools.GroovyClass)

Example 4 with GroovyClass

use of org.codehaus.groovy.tools.GroovyClass in project groovy-core by groovy.

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)

Example 5 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