/** * Sets the compile phase up to which compilation should proceed. Defaults to * CompilePhase.CONVERSION (the phase in which the AST is first constructed). * * @param phase the compile phase up to which compilation should proceed * @throws IllegalArgumentException if a compile phase before * CompilePhase.CONVERSION is specified */ public void setCompilePhase(CompilePhase phase) { if (phase.getPhaseNumber() < CompilePhase.CONVERSION.getPhaseNumber()) throw new IllegalArgumentException("AST is only available from phase CONVERSION onwards"); compilePhase = phase; }
public static void addPhaseOperations(final CompilationUnit compilationUnit) { final ASTTransformationsContext context = compilationUnit.getASTTransformationsContext(); addGlobalTransforms(context); compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(source, compilationUnit.getTransformLoader()); collector.visitClass(classNode); } }, Phases.SEMANTIC_ANALYSIS); for (CompilePhase phase : CompilePhase.values()) { final ASTTransformationVisitor visitor = new ASTTransformationVisitor(phase, context); switch (phase) { case INITIALIZATION: case PARSING: case CONVERSION: // with transform detection alone these phases are inaccessible, so don't add it break; default: compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { visitor.source = source; visitor.visitClass(classNode); } }, phase.getPhaseNumber()); break; } } }
public Object postCompleteNode(final FactoryBuilderSupport factory, final Object parent, final Object node) { if (node instanceof Map) { Map map = (Map) node; ProxyGeneratorAdapter adapter = new ProxyGeneratorAdapter( map, map.containsKey("superClass")?(Class)map.get("superClass"):CompilationCustomizer.class, map.containsKey("interfaces")?(Class[])map.get("interfaces"):null, this.getClass().getClassLoader(), false, null ); Object phase = map.get("phase"); if (!(phase instanceof CompilePhase)) { phase = CompilePhase.valueOf(phase.toString()); } return adapter.proxy(map, phase); } return node; } }
/** * Returns the CompilePhase for the given integer phase number. * @param phaseNumber * the phase number * @return * the CompilePhase or null if not found */ public static CompilePhase fromPhaseNumber(int phaseNumber) { for (CompilePhase phase : values()) { if (phase.phaseNumber == phaseNumber) { return phase; } } return null; } }
private Map<CompilePhase, Map<Class<? extends ASTTransformation>, Set<ASTNode>>> getTransformInstances() { if(transformInstances == null){ transformInstances = new EnumMap<CompilePhase, Map<Class <? extends ASTTransformation>, Set<ASTNode>>>(CompilePhase.class); for (CompilePhase phase : CompilePhase.values()) { transformInstances.put(phase, new LinkedHashMap<Class <? extends ASTTransformation>, Set<ASTNode>>()); } } return transformInstances; }
@Override protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) { CompilationUnit unit = super.createCompilationUnit(config, source); unit.addPhaseOperation(new CompilationUnit.SourceUnitOperation() { @Override public void call(SourceUnit source) throws CompilationFailedException { module = source.getAST(); throw new AstSuccessfullyCaptured(); } }, compilePhase.getPhaseNumber()); return unit; } }
public static void addPhaseOperations(final CompilationUnit compilationUnit) { addGlobalTransforms(compilationUnit); compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(source, compilationUnit.getTransformLoader()); collector.visitClass(classNode); } }, Phases.SEMANTIC_ANALYSIS); for (CompilePhase phase : CompilePhase.values()) { final ASTTransformationVisitor visitor = new ASTTransformationVisitor(phase); switch (phase) { case INITIALIZATION: case PARSING: case CONVERSION: // with transform detection alone these phases are inaccessible, so don't add it break; default: compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { visitor.source = source; visitor.visitClass(classNode); } }, phase.getPhaseNumber()); break; } } }
/** * Returns the CompilePhase for the given integer phase number. * @param phaseNumber * the phase number * @return * the CompilePhase or null if not found */ public static CompilePhase fromPhaseNumber(int phaseNumber) { for (CompilePhase phase : values()) { if (phase.phaseNumber == phaseNumber) { return phase; } } return null; } }
private CompilePhase extractCompilePhaseFrom(final AnnotationNode annotationNode) { String compilePhaseAsString = A.UTIL.NODE.get(annotationNode, String.class); return CompilePhase.valueOf(compilePhaseAsString); } }
private void verifyCompilePhase(AnnotationNode annotation, Class<?> klass) { GroovyASTTransformation transformationClass = klass.getAnnotation(GroovyASTTransformation.class); if (transformationClass != null) { CompilePhase specifiedCompilePhase = transformationClass.phase(); if (specifiedCompilePhase.getPhaseNumber() < CompilePhase.SEMANTIC_ANALYSIS.getPhaseNumber()) { source.getErrorCollector().addError( new SimpleMessage( annotation.getClassNode().getName() + " is defined to be run in compile phase " + specifiedCompilePhase + ". Local AST transformations must run in " + CompilePhase.SEMANTIC_ANALYSIS + " or later!", source)); } } else { source.getErrorCollector().addError( new SimpleMessage("AST transformation implementation classes must be annotated with " + GroovyASTTransformation.class.getName() + ". " + klass.getName() + " lacks this annotation.", source)); } }
public static void addPhaseOperations(final CompilationUnit compilationUnit) { addGlobalTransforms(compilationUnit); compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(source, compilationUnit.getTransformLoader()); collector.visitClass(classNode); } }, Phases.SEMANTIC_ANALYSIS); for (CompilePhase phase : CompilePhase.values()) { final ASTTransformationVisitor visitor = new ASTTransformationVisitor(phase); switch (phase) { case INITIALIZATION: case PARSING: case CONVERSION: // with transform detection alone these phases are inaccessible, so don't add it break; default: compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { visitor.source = source; visitor.visitClass(classNode); } }, phase.getPhaseNumber()); break; } } }
private void getTransformInstancesLazy() { transformInstances = new EnumMap<CompilePhase, Map<Class <? extends ASTTransformation>, Set<ASTNode>>>(CompilePhase.class); for (CompilePhase phase : CompilePhase.values()) { transformInstances.put(phase, new HashMap<Class <? extends ASTTransformation>, Set<ASTNode>>()); } }
private CompilePhase extractCompilePhaseFrom(final AnnotationNode annotationNode) { final String value = A.UTIL.NODE.getStringValue(annotationNode); final String phaseAsString = value .replace(PHASE_GROOVY, BLANK) .replace(PHASE_PFIX, BLANK) .replace(PHASE_PFIX_SHORT, BLANK); final CompilePhase compilePhase = CompilePhase.valueOf(phaseAsString); return compilePhase; } }
private void applyCompilationCustomizers(CompilerConfiguration configuration) { // apply configuration customizers if any if (configuration != null) { final List<CompilationCustomizer> customizers = configuration.getCompilationCustomizers(); for (CompilationCustomizer customizer : customizers) { if (customizer instanceof CompilationUnitAware) { ((CompilationUnitAware) customizer).setCompilationUnit(this); } addPhaseOperation(customizer, customizer.getPhase().getPhaseNumber()); } } }
public static void addPhaseOperations(final CompilationUnit compilationUnit) { addGlobalTransforms(compilationUnit); compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(source, compilationUnit.getTransformLoader()); collector.visitClass(classNode); } }, Phases.SEMANTIC_ANALYSIS); for (CompilePhase phase : CompilePhase.values()) { final ASTTransformationVisitor visitor = new ASTTransformationVisitor(phase); switch (phase) { case INITIALIZATION: case PARSING: case CONVERSION: // with transform detection alone these phases are inaccessible, so don't add it break; default: compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { visitor.source = source; visitor.visitClass(classNode); } }, phase.getPhaseNumber()); break; } } }
private Map<CompilePhase, Map<Class<? extends ASTTransformation>, Set<ASTNode>>> getTransformInstances() { if(transformInstances == null){ transformInstances = new EnumMap<CompilePhase, Map<Class <? extends ASTTransformation>, Set<ASTNode>>>(CompilePhase.class); for (CompilePhase phase : CompilePhase.values()) { transformInstances.put(phase, new HashMap<Class <? extends ASTTransformation>, Set<ASTNode>>()); } } return transformInstances; }
compilationUnit.addPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber()); } else { compilationUnit.addNewPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber());
private void getTransformInstancesLazy() { transformInstances = new EnumMap<CompilePhase, Map<Class <? extends ASTTransformation>, Set<ASTNode>>>(CompilePhase.class); for (CompilePhase phase : CompilePhase.values()) { transformInstances.put(phase, new HashMap<Class <? extends ASTTransformation>, Set<ASTNode>>()); } }
private void registerASTTransformations(final ClassNode helper) { ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor( unit, compilationUnit.getTransformLoader() ); collector.visitClass(helper); // Perform an additional phase which has to be done *after* type checking compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { @Override public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException { if (classNode==helper) { PostTypeCheckingExpressionReplacer replacer = new PostTypeCheckingExpressionReplacer(source); replacer.visitClass(helper); } } }, CompilePhase.INSTRUCTION_SELECTION.getPhaseNumber()); }
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; } }