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()]);
}
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);
}
}
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());
}
}
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();
}
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();
}
Aggregations