Search in sources :

Example 1 with CompilePhase

use of org.codehaus.groovy.control.CompilePhase in project groovy by apache.

the class ASTTransformationCollectorCodeVisitor method verifyAndAddTransform.

@SuppressWarnings("unchecked")
private void verifyAndAddTransform(final AnnotationNode annotation, final Class<?> transformClass) {
    if (!ASTTransformation.class.isAssignableFrom(transformClass)) {
        String error = "Not an ASTTransformation: " + transformClass.getName() + " declared by " + annotation.getClassNode().getName();
        source.getErrorCollector().addError(new SimpleMessage(error, source));
    }
    GroovyASTTransformation transformationClass = transformClass.getAnnotation(GroovyASTTransformation.class);
    if (transformationClass == null) {
        String error = "AST transformation implementation classes must be annotated with " + GroovyASTTransformation.class.getName() + ". " + transformClass.getName() + " lacks this annotation.";
        source.getErrorCollector().addError(new SimpleMessage(error, source));
    }
    CompilePhase specifiedCompilePhase = transformationClass.phase();
    if (specifiedCompilePhase.getPhaseNumber() < CompilePhase.SEMANTIC_ANALYSIS.getPhaseNumber()) {
        String error = annotation.getClassNode().getName() + " is defined to be run in compile phase " + specifiedCompilePhase + ". Local AST transformations must run in SEMANTIC_ANALYSIS or later!";
        source.getErrorCollector().addError(new SimpleMessage(error, source));
    }
    if (!Traits.isTrait(classNode) || transformClass == TraitASTTransformation.class || transformClass == SealedASTTransformation.class) {
        classNode.addTransform((Class<? extends ASTTransformation>) transformClass, annotation);
    }
}
Also used : CompilePhase(org.codehaus.groovy.control.CompilePhase) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) TraitASTTransformation(org.codehaus.groovy.transform.trait.TraitASTTransformation)

Example 2 with CompilePhase

use of org.codehaus.groovy.control.CompilePhase in project groovy by apache.

the class InlinedASTCustomizerFactory method postCompleteNode.

@Override
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;
}
Also used : CompilePhase(org.codehaus.groovy.control.CompilePhase) ProxyGeneratorAdapter(org.codehaus.groovy.runtime.ProxyGeneratorAdapter) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with CompilePhase

use of org.codehaus.groovy.control.CompilePhase in project groovy by apache.

the class ASTTransformationVisitor method addPhaseOperations.

public static void addPhaseOperations(final CompilationUnit compilationUnit) {
    ASTTransformationsContext context = compilationUnit.getASTTransformationsContext();
    addGlobalTransforms(context);
    compilationUnit.addPhaseOperation((final SourceUnit source, final GeneratorContext ignore, final ClassNode classNode) -> {
        GroovyClassVisitor visitor = new ASTTransformationCollectorCodeVisitor(source, compilationUnit.getTransformLoader());
        visitor.visitClass(classNode);
    }, Phases.SEMANTIC_ANALYSIS);
    for (CompilePhase phase : CompilePhase.values()) {
        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((final SourceUnit source, final GeneratorContext ignore, final ClassNode classNode) -> {
                    ASTTransformationVisitor visitor = new ASTTransformationVisitor(phase, context);
                    visitor.source = source;
                    visitor.visitClass(classNode);
                }, phase.getPhaseNumber());
                break;
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ASTTransformationsContext(org.codehaus.groovy.control.ASTTransformationsContext) CompilePhase(org.codehaus.groovy.control.CompilePhase) GroovyClassVisitor(org.codehaus.groovy.ast.GroovyClassVisitor) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext)

Example 4 with CompilePhase

use of org.codehaus.groovy.control.CompilePhase in project groovy-core by groovy.

the class InlinedASTCustomizerFactory method postCompleteNode.

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;
}
Also used : CompilePhase(org.codehaus.groovy.control.CompilePhase) ProxyGeneratorAdapter(org.codehaus.groovy.runtime.ProxyGeneratorAdapter) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CompilePhase (org.codehaus.groovy.control.CompilePhase)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CompilationCustomizer (org.codehaus.groovy.control.customizers.CompilationCustomizer)2 ProxyGeneratorAdapter (org.codehaus.groovy.runtime.ProxyGeneratorAdapter)2 ClassNode (org.codehaus.groovy.ast.ClassNode)1 GroovyClassVisitor (org.codehaus.groovy.ast.GroovyClassVisitor)1 GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)1 ASTTransformationsContext (org.codehaus.groovy.control.ASTTransformationsContext)1 SourceUnit (org.codehaus.groovy.control.SourceUnit)1 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)1 TraitASTTransformation (org.codehaus.groovy.transform.trait.TraitASTTransformation)1