Search in sources :

Example 1 with ASTTransformationCollectorCodeVisitor

use of org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor in project groovy by apache.

the class TraitComposer method doExtendTraits.

/**
     * Given a class node, if this class node implements a trait, then generate all the appropriate
     * code which delegates calls to the trait. It is safe to call this method on a class node which
     * does not implement a trait.
     * @param cNode a class node
     * @param unit the source unit
     */
public static void doExtendTraits(final ClassNode cNode, final SourceUnit unit, final CompilationUnit cu) {
    if (cNode.isInterface())
        return;
    boolean isItselfTrait = Traits.isTrait(cNode);
    SuperCallTraitTransformer superCallTransformer = new SuperCallTraitTransformer(unit);
    if (isItselfTrait) {
        checkTraitAllowed(cNode, unit);
        return;
    }
    if (!cNode.getNameWithoutPackage().endsWith(Traits.TRAIT_HELPER)) {
        List<ClassNode> traits = findTraits(cNode);
        for (ClassNode trait : traits) {
            TraitHelpersTuple helpers = Traits.findHelpers(trait);
            applyTrait(trait, cNode, helpers);
            superCallTransformer.visitClass(cNode);
            if (unit != null) {
                ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(unit, cu.getTransformLoader());
                collector.visitClass(cNode);
            }
        }
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) ASTTransformationCollectorCodeVisitor(org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor)

Example 2 with ASTTransformationCollectorCodeVisitor

use of org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor in project groovy-core by groovy.

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 3 with ASTTransformationCollectorCodeVisitor

use of org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor in project groovy-core by groovy.

the class TraitComposer method doExtendTraits.

/**
     * Given a class node, if this class node implements a trait, then generate all the appropriate
     * code which delegates calls to the trait. It is safe to call this method on a class node which
     * does not implement a trait.
     * @param cNode a class node
     * @param unit the source unit
     */
public static void doExtendTraits(final ClassNode cNode, final SourceUnit unit, final CompilationUnit cu) {
    if (cNode.isInterface())
        return;
    boolean isItselfTrait = Traits.isTrait(cNode);
    SuperCallTraitTransformer superCallTransformer = new SuperCallTraitTransformer(unit);
    if (isItselfTrait) {
        checkTraitAllowed(cNode, unit);
        return;
    }
    if (!cNode.getNameWithoutPackage().endsWith(Traits.TRAIT_HELPER)) {
        List<ClassNode> traits = findTraits(cNode);
        for (ClassNode trait : traits) {
            TraitHelpersTuple helpers = Traits.findHelpers(trait);
            applyTrait(trait, cNode, helpers);
            superCallTransformer.visitClass(cNode);
            if (unit != null) {
                ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(unit, cu.getTransformLoader());
                collector.visitClass(cNode);
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ASTTransformationCollectorCodeVisitor(org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor)

Example 4 with ASTTransformationCollectorCodeVisitor

use of org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor 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)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)4 ASTTransformationCollectorCodeVisitor (org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor)4 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)3 GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)2 SourceUnit (org.codehaus.groovy.control.SourceUnit)2