Search in sources :

Example 1 with CompilationCustomizer

use of org.codehaus.groovy.control.customizers.CompilationCustomizer in project ratpack by ratpack.

the class GroovySnippetExecuter method execute.

@Override
public void execute(TestCodeSnippet snippet) throws Exception {
    CompilerConfiguration config = new CompilerConfiguration();
    config.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (compileStatic) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
        }
    });
    ClassLoader classLoader = new URLClassLoader(new URL[] {}, getClass().getClassLoader());
    GroovyShell groovyShell = new GroovyShell(classLoader, new Binding(), config);
    List<String> importsAndSnippet = extractImports(snippet.getSnippet());
    String imports = importsAndSnippet.get(0);
    String snippetMinusImports = fixture.transform(importsAndSnippet.get(1));
    String fullSnippet = imports + fixture.pre() + snippetMinusImports + fixture.post();
    Script script;
    try {
        script = groovyShell.parse(fullSnippet, snippet.getClassName());
    } catch (MultipleCompilationErrorsException e) {
        Message error = e.getErrorCollector().getError(0);
        if (error instanceof SyntaxErrorMessage) {
            //noinspection ThrowableResultOfMethodCallIgnored
            System.out.println(snippet.getSnippet());
            throw new CompileException(e, ((SyntaxErrorMessage) error).getCause().getLine());
        } else {
            throw e;
        }
    }
    ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(groovyShell.getClassLoader());
        fixture.around(script::run);
    } finally {
        Thread.currentThread().setContextClassLoader(previousContextClassLoader);
    }
}
Also used : Binding(groovy.lang.Binding) ClassNode(org.codehaus.groovy.ast.ClassNode) Script(groovy.lang.Script) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) GroovyShell(groovy.lang.GroovyShell) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Example 2 with CompilationCustomizer

use of org.codehaus.groovy.control.customizers.CompilationCustomizer in project ratpack by ratpack.

the class ScriptEngine method createClassLoader.

private GroovyClassLoader createClassLoader(final Path scriptPath) {
    final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    if (!scriptBaseClass.equals(Script.class)) {
        compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
    }
    compilerConfiguration.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (staticCompile) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
            classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
            if (scriptPath != null) {
                AnnotationNode scriptPathAnnotation = new AnnotationNode(new ClassNode(ScriptPath.class));
                scriptPathAnnotation.addMember("value", new ConstantExpression(scriptPath.toUri().toString()));
                classNode.addAnnotation(scriptPathAnnotation);
            }
        }
    });
    return new GroovyClassLoader(parentLoader, compilerConfiguration) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
            return new CompilationUnit(config, source, this) {

                {
                    verifier = new Verifier() {

                        @Override
                        public void visitClass(ClassNode node) {
                            if (node.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
                                AnnotationNode lineNumberAnnotation = new AnnotationNode(LINE_NUMBER_CLASS_NODE);
                                lineNumberAnnotation.addMember("value", new ConstantExpression(node.getLineNumber(), true));
                                node.addAnnotation(lineNumberAnnotation);
                            }
                            super.visitClass(node);
                        }
                    };
                }
            };
        }
    };
}
Also used : Script(groovy.lang.Script) ClassNode(org.codehaus.groovy.ast.ClassNode) InheritConstructors(groovy.transform.InheritConstructors) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) CodeSource(java.security.CodeSource) Verifier(org.codehaus.groovy.classgen.Verifier) GroovyClassLoader(groovy.lang.GroovyClassLoader) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Aggregations

Script (groovy.lang.Script)2 CompileStatic (groovy.transform.CompileStatic)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)2 CompilationCustomizer (org.codehaus.groovy.control.customizers.CompilationCustomizer)2 Binding (groovy.lang.Binding)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyShell (groovy.lang.GroovyShell)1 InheritConstructors (groovy.transform.InheritConstructors)1 URLClassLoader (java.net.URLClassLoader)1 CodeSource (java.security.CodeSource)1 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)1 Verifier (org.codehaus.groovy.classgen.Verifier)1 Message (org.codehaus.groovy.control.messages.Message)1 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)1