Search in sources :

Example 6 with GeneratorContext

use of org.codehaus.groovy.classgen.GeneratorContext in project groovy by apache.

the class CompilationUnit method addPhaseOperations.

private void addPhaseOperations() {
    addPhaseOperation(SourceUnit::parse, Phases.PARSING);
    addPhaseOperation(source -> {
        source.convert();
        // add module to compile unit
        getAST().addModule(source.getAST());
        Optional.ofNullable(getProgressCallback()).ifPresent(callback -> callback.call(source, getPhase()));
    }, Phases.CONVERSION);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        GroovyClassVisitor visitor = new EnumVisitor(this, source);
        visitor.visitClass(classNode);
    }, Phases.CONVERSION);
    addPhaseOperation(source -> {
        for (ClassNode classNode : source.getAST().getClasses()) {
            resolveVisitor.setClassNodeResolver(classNodeResolver);
            resolveVisitor.startResolving(classNode, source);
        }
    }, Phases.SEMANTIC_ANALYSIS);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        GroovyClassVisitor visitor = new StaticImportVisitor(classNode, source);
        visitor.visitClass(classNode);
    }, Phases.SEMANTIC_ANALYSIS);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        GroovyClassVisitor visitor = new InnerClassVisitor(this, source);
        visitor.visitClass(classNode);
    }, Phases.SEMANTIC_ANALYSIS);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        if (!classNode.isSynthetic()) {
            GroovyClassVisitor visitor = new GenericsVisitor(source);
            visitor.visitClass(classNode);
        }
    }, Phases.SEMANTIC_ANALYSIS);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        TraitComposer.doExtendTraits(classNode, source, this);
    }, Phases.CANONICALIZATION);
    addPhaseOperation(source -> {
        List<ClassNode> classes = source.getAST().getClasses();
        for (ClassNode node : classes) {
            CompileUnit cu = node.getCompileUnit();
            for (Iterator<String> it = cu.getClassesToCompile().keySet().iterator(); it.hasNext(); ) {
                String name = it.next();
                StringBuilder message = new StringBuilder();
                message.append("Compilation incomplete: expected to find the class ").append(name).append(" in ").append(source.getName());
                if (classes.isEmpty()) {
                    message.append(", but the file seems not to contain any classes");
                } else {
                    message.append(", but the file contains the classes: ");
                    boolean first = true;
                    for (ClassNode cn : classes) {
                        if (first) {
                            first = false;
                        } else {
                            message.append(", ");
                        }
                        message.append(cn.getName());
                    }
                }
                getErrorCollector().addErrorAndContinue(Message.create(message.toString(), this));
                it.remove();
            }
        }
    }, Phases.CANONICALIZATION);
    addPhaseOperation(classgen, Phases.CLASS_GENERATION);
    addPhaseOperation(groovyClass -> {
        String name = groovyClass.getName().replace('.', File.separatorChar) + ".class";
        File path = new File(getConfiguration().getTargetDirectory(), name);
        // ensure the path is ready for the file
        File directory = path.getParentFile();
        if (directory != null && !directory.exists()) {
            directory.mkdirs();
        }
        // create the file and write out the data
        try (FileOutputStream stream = new FileOutputStream(path)) {
            stream.write(groovyClass.getBytes());
        } catch (IOException e) {
            getErrorCollector().addError(Message.create(e.getMessage(), this));
        }
    });
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        AnnotationCollectorTransform.ClassChanger xformer = new AnnotationCollectorTransform.ClassChanger();
        xformer.transformClass(classNode);
    }, Phases.SEMANTIC_ANALYSIS);
    ASTTransformationVisitor.addPhaseOperations(this);
    // post-transform operations:
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        StaticVerifier verifier = new StaticVerifier();
        verifier.visitClass(classNode, source);
    }, Phases.SEMANTIC_ANALYSIS);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        GroovyClassVisitor visitor = new InnerClassCompletionVisitor(this, source);
        visitor.visitClass(classNode);
        visitor = new EnumCompletionVisitor(this, source);
        visitor.visitClass(classNode);
    }, Phases.CANONICALIZATION);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        Object callback = classNode.getNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK);
        if (callback instanceof IPrimaryClassNodeOperation) {
            ((IPrimaryClassNodeOperation) callback).call(source, context, classNode);
            classNode.removeNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK);
        }
    }, Phases.INSTRUCTION_SELECTION);
    addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        // TODO: Can this be moved into org.codehaus.groovy.transform.sc.transformers.VariableExpressionTransformer?
        GroovyClassVisitor visitor = new ClassCodeExpressionTransformer() {

            @Override
            protected SourceUnit getSourceUnit() {
                return source;
            }

            @Override
            public Expression transform(final Expression expression) {
                if (expression instanceof VariableExpression) {
                    // check for "switch(enumType) { case CONST: ... }"
                    ClassNode enumType = expression.getNodeMetaData(SWITCH_CONDITION_EXPRESSION_TYPE);
                    if (enumType != null) {
                        // replace "CONST" variable expression with "EnumType.CONST" property expression
                        Expression propertyExpression = propX(classX(enumType), expression.getText());
                        setSourcePosition(propertyExpression, expression);
                        return propertyExpression;
                    }
                }
                return expression;
            }
        };
        visitor.visitClass(classNode);
    }, Phases.INSTRUCTION_SELECTION);
}
Also used : GroovyClassVisitor(org.codehaus.groovy.ast.GroovyClassVisitor) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) InnerClassVisitor(org.codehaus.groovy.classgen.InnerClassVisitor) AnnotationCollectorTransform(org.codehaus.groovy.transform.AnnotationCollectorTransform) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) EnumVisitor(org.codehaus.groovy.classgen.EnumVisitor) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) IOException(java.io.IOException) InnerClassCompletionVisitor(org.codehaus.groovy.classgen.InnerClassCompletionVisitor) EnumCompletionVisitor(org.codehaus.groovy.classgen.EnumCompletionVisitor) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) Expression(org.codehaus.groovy.ast.expr.Expression) FileOutputStream(java.io.FileOutputStream) CompileUnit(org.codehaus.groovy.ast.CompileUnit) File(java.io.File)

Example 7 with GeneratorContext

use of org.codehaus.groovy.classgen.GeneratorContext in project groovy by apache.

the class TraitASTTransformation method registerASTTransformations.

private void registerASTTransformations(final ClassNode helper) {
    ASTTransformationVisitor.addNewPhaseOperation(compilationUnit, sourceUnit, helper);
    // perform an additional operation which has to be done *after* static type checking
    compilationUnit.addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        if (classNode == helper) {
            GroovyClassVisitor visitor = new PostTypeCheckingExpressionReplacer(source);
            visitor.visitClass(helper);
        }
    }, CompilePhase.INSTRUCTION_SELECTION.getPhaseNumber());
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyClassVisitor(org.codehaus.groovy.ast.GroovyClassVisitor) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext)

Example 8 with GeneratorContext

use of org.codehaus.groovy.classgen.GeneratorContext in project groovy by apache.

the class TraitASTTransformation method registerASTTranformations.

private void registerASTTranformations(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());
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) ASTTransformationCollectorCodeVisitor(org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor)

Example 9 with GeneratorContext

use of org.codehaus.groovy.classgen.GeneratorContext in project groovy-core by groovy.

the class DependencyTest method setUp.

@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);
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext)

Example 10 with GeneratorContext

use of org.codehaus.groovy.classgen.GeneratorContext 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)

Aggregations

GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)11 ClassNode (org.codehaus.groovy.ast.ClassNode)10 SourceUnit (org.codehaus.groovy.control.SourceUnit)6 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)4 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)4 GroovyClassVisitor (org.codehaus.groovy.ast.GroovyClassVisitor)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 Script (groovy.lang.Script)2 CompileStatic (groovy.transform.CompileStatic)2 File (java.io.File)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 CompilationCustomizer (org.codehaus.groovy.control.customizers.CompilationCustomizer)2 ASTTransformationCollectorCodeVisitor (org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor)2 Binding (groovy.lang.Binding)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyShell (groovy.lang.GroovyShell)1 InheritConstructors (groovy.transform.InheritConstructors)1 FileOutputStream (java.io.FileOutputStream)1