/** * creates a new CompilationUnit. If you want to add additional * phase operations to the CompilationUnit (for example to inject * additional methods, variables, fields), then you should overwrite * this method. * * @param config the compiler configuration, usually the same as for this class loader * @param source the source containing the initial file to compile, more files may follow during compilation * @return the CompilationUnit */ protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) { return new CompilationUnit(config, source, this); }
/** * Compiles a single File. */ public void compile( File file ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( file ); unit.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(); }
public FileSystemCompiler(CompilerConfiguration configuration, CompilationUnit cu) throws ConfigurationException { if (cu != null) { unit = cu; } else if (configuration.getJointCompilationOptions() != null) { unit = new JavaAwareCompilationUnit(configuration); } else { unit = new CompilationUnit(configuration); } }
/** * Compiles a series of Files. */ public void compile( File[] files ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSources( files ); unit.compile(); }
CompilationUnit unit = new CompilationUnit(); unit.setClassgenCallback(search); unit.addSource(url);
protected CompilationUnit makeCompileUnit(GroovyClassLoader loader) { 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, loader); } else { return new CompilationUnit(configuration, null, loader); } }
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
public long execute() throws Exception { ClassLoader cl = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader().getParent()); GroovyClassLoader gcl = new GroovyClassLoader(cl); CompilationUnit cu = new CompilationUnit(new CompilerConfiguration(), null, gcl, new GroovyClassLoader(this.getClass().getClassLoader())); for (File source : sources) { cu.addSource(source); } long sd = System.nanoTime(); cu.compile(CompilePhase.CLASS_GENERATION.getPhaseNumber()); long dur = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sd); return dur; } }
/** * 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. * <p> * 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); addGeneratedMethod(node, "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 | NoSuchMethodException | InvocationTargetException e) { throw new GroovyBugError(e); } }
@Override protected void setUp() throws Exception { super.setUp(); cu = new CompilationUnit(); cache = new StringSetMap(); cu.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { @Override public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { DependencyTracker dt = new DependencyTracker(source,cache); dt.visitClass(classNode); } }, Phases.CLASS_GENERATION); }
String name = superClass.getName() + "$TraitAdapter"; ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(ClassNode.EMPTY_ARRAY), null); CompilationUnit cu = new CompilationUnit(loader); CompilerConfiguration config = new CompilerConfiguration(); SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config));
public ModuleNode getAST(String source, int untilPhase) { SourceUnit unit = SourceUnit.create("Test", source); CompilationUnit compUnit = new CompilationUnit(); compUnit.addSource(unit); compUnit.compile(untilPhase); return unit.getAST(); }
CompilationUnit compilation = new CompilationUnit(config, null, gcl);
CompilationUnit cu = new CompilationUnit(objectGroovyClassFactory.config); cu.addSource(name, script); try {
/** * Compiles a single File. */ public void compile( File file ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( file ); unit.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(); }
/** * 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(); }
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }