use of groovy.transform.CompileStatic 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);
}
}
Aggregations