Search in sources :

Example 6 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit in project spring-boot by spring-projects.

the class GroovyCompiler method compile.

/**
	 * Compile the specified Groovy sources, applying any
	 * {@link CompilerAutoConfiguration}s. All classes defined in the sources will be
	 * returned from this method.
	 * @param sources the sources to compile
	 * @return compiled classes
	 * @throws CompilationFailedException in case of compilation failures
	 * @throws IOException in case of I/O errors
	 * @throws CompilationFailedException in case of compilation errors
	 */
public Class<?>[] compile(String... sources) throws CompilationFailedException, IOException {
    this.loader.clearCache();
    List<Class<?>> classes = new ArrayList<>();
    CompilerConfiguration configuration = this.loader.getConfiguration();
    CompilationUnit compilationUnit = new CompilationUnit(configuration, null, this.loader);
    ClassCollector collector = this.loader.createCollector(compilationUnit, null);
    compilationUnit.setClassgenCallback(collector);
    for (String source : sources) {
        List<String> paths = ResourceUtils.getUrls(source, this.loader);
        for (String path : paths) {
            compilationUnit.addSource(new URL(path));
        }
    }
    addAstTransformations(compilationUnit);
    compilationUnit.compile(Phases.CLASS_GENERATION);
    for (Object loadedClass : collector.getLoadedClasses()) {
        classes.add((Class<?>) loadedClass);
    }
    ClassNode mainClassNode = MainClass.get(compilationUnit);
    Class<?> mainClass = null;
    for (Class<?> loadedClass : classes) {
        if (mainClassNode.getName().equals(loadedClass.getName())) {
            mainClass = loadedClass;
        }
    }
    if (mainClass != null) {
        classes.remove(mainClass);
        classes.add(0, mainClass);
    }
    return classes.toArray(new Class<?>[classes.size()]);
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) ClassCollector(groovy.lang.GroovyClassLoader.ClassCollector) ArrayList(java.util.ArrayList) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) URL(java.net.URL)

Example 7 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit 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 8 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit in project groovy by apache.

the class Groovyc method makeCompileUnit.

protected CompilationUnit makeCompileUnit() {
    Map<String, Object> options = configuration.getJointCompilationOptions();
    if (options != null) {
        if (keepStubs) {
            options.put("keepStubs", Boolean.TRUE);
        }
        if (stubDir != null) {
            options.put("stubDir", stubDir);
        } else {
            try {
                File tempStubDir = DefaultGroovyStaticMethods.createTempDir(null, "groovy-generated-", "-java-source");
                temporaryFiles.add(tempStubDir);
                options.put("stubDir", tempStubDir);
            } catch (IOException ioe) {
                throw new BuildException(ioe);
            }
        }
        return new JavaAwareCompilationUnit(configuration, buildClassLoaderFor());
    } else {
        return new CompilationUnit(configuration, null, buildClassLoaderFor());
    }
}
Also used : JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 9 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit in project groovy by apache.

the class Compiler method compile.

/**
    *  Compiles a series of Files.
    */
public void compile(File[] files) throws CompilationFailedException {
    CompilationUnit unit = new CompilationUnit(configuration);
    unit.addSources(files);
    unit.compile();
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit)

Example 10 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit in project groovy by apache.

the class Compiler method compile.

/**
    *  Compiles a series of Files from file names.
    */
public void compile(String[] files) throws CompilationFailedException {
    CompilationUnit unit = new CompilationUnit(configuration);
    unit.addSources(files);
    unit.compile();
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit)

Aggregations

CompilationUnit (org.codehaus.groovy.control.CompilationUnit)31 ClassNode (org.codehaus.groovy.ast.ClassNode)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 SourceUnit (org.codehaus.groovy.control.SourceUnit)8 File (java.io.File)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyClass (org.codehaus.groovy.tools.GroovyClass)5 ArrayList (java.util.ArrayList)4 BuildException (org.apache.tools.ant.BuildException)4 URL (java.net.URL)3 List (java.util.List)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 JavaAwareCompilationUnit (org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit)3 GroovyObject (groovy.lang.GroovyObject)2 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PrivilegedActionException (java.security.PrivilegedActionException)2 LinkedHashSet (java.util.LinkedHashSet)2 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2