org.codehaus.groovy.ast.GroovyCodeVisitor Java Examples

The following examples show how to use org.codehaus.groovy.ast.GroovyCodeVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    receiver.visit(visitor);
    if (visitor instanceof AsmClassGenerator) {
        ClassNode topOperand = controller.getOperandStack().getTopOperand();
        ClassNode type = getType();
        if (ClassHelper.GSTRING_TYPE.equals(topOperand) && ClassHelper.STRING_TYPE.equals(type)) {
            // perform regular type conversion
            controller.getOperandStack().doGroovyCast(type);
            return;
        }
        if (ClassHelper.isPrimitiveType(topOperand) && !ClassHelper.isPrimitiveType(type)) {
            controller.getOperandStack().box();
        } else if (!ClassHelper.isPrimitiveType(topOperand) && ClassHelper.isPrimitiveType(type)) {
            controller.getOperandStack().doGroovyCast(type);
        }
        if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(topOperand, type)) return;
        controller.getMethodVisitor().visitTypeInsn(CHECKCAST, type.isArray()
                ? BytecodeHelper.getTypeDescription(type)
                : BytecodeHelper.getClassInternalName(type.getName()));
        controller.getOperandStack().replace(type);
    }
}
 
Example #2
Source File: CompareIdentityExpression.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
        controller.getTypeChooser().resolveType(rightExpression, controller.getClassNode());
        MethodVisitor mv = controller.getMethodVisitor();
        leftExpression.visit(acg);
        controller.getOperandStack().box();
        rightExpression.visit(acg);
        controller.getOperandStack().box();
        Label l1 = new Label();
        mv.visitJumpInsn(IF_ACMPNE, l1);
        mv.visitInsn(ICONST_1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l2);
        controller.getOperandStack().replace(ClassHelper.boolean_TYPE, 2);
    } else {
        super.visit(visitor);
    }
}
 
Example #3
Source File: AstUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void visitScriptCode(SourceUnit source, GroovyCodeVisitor transformer) {
    source.getAST().getStatementBlock().visit(transformer);
    for (Object method : source.getAST().getMethods()) {
        MethodNode methodNode = (MethodNode) method;
        methodNode.getCode().visit(transformer);
    }
}
 
Example #4
Source File: CompareToNullExpression.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        MethodVisitor mv = controller.getMethodVisitor();
        objectExpression.visit(acg);
        ClassNode top = controller.getOperandStack().getTopOperand();
        if (ClassHelper.isPrimitiveType(top)) {
            controller.getOperandStack().pop();
            mv.visitInsn(equalsNull ? ICONST_0 : ICONST_1);
            controller.getOperandStack().push(ClassHelper.boolean_TYPE);
            return;
        }
        Label zero = new Label();
        mv.visitJumpInsn(equalsNull ? IFNONNULL : IFNULL, zero);
        mv.visitInsn(ICONST_1);
        Label end = new Label();
        mv.visitJumpInsn(GOTO, end);
        mv.visitLabel(zero);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(end);
        controller.getOperandStack().replace(ClassHelper.boolean_TYPE);
    } else {
        super.visit(visitor);
    }
}
 
Example #5
Source File: ConstructorCallTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        acg = (AsmClassGenerator) visitor;
    } else {
        originalCall.visit(visitor);
    }
    super.visit(visitor);
}
 
Example #6
Source File: AstBuilderTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
protected GroovyCodeVisitor getTransformer(ASTNode[] nodes, SourceUnit sourceUnit) {
    // todo : are there other import types that can be specified?
    return new AstBuilderInvocationTrap(
        sourceUnit.getAST().getImports(),
        sourceUnit.getAST().getStarImports(),
        sourceUnit.getSource(),
        sourceUnit
    );
}
 
Example #7
Source File: AstUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void visitScriptCode(SourceUnit source, GroovyCodeVisitor transformer) {
    source.getAST().getStatementBlock().visit(transformer);
    for (Object method : source.getAST().getMethods()) {
        MethodNode methodNode = (MethodNode) method;
        methodNode.getCode().visit(transformer);
    }
}
 
Example #8
Source File: TemporaryVariableExpression.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        if (variable==null) {
            AsmClassGenerator acg = (AsmClassGenerator) visitor;
            WriterController controller = acg.getController();
            variable = new ExpressionAsVariableSlot(controller, expression);
        }
        variable.visit(visitor);
    } else {
        expression.visit(visitor);
    }
}
 
Example #9
Source File: AstUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void visitScriptCode(SourceUnit source, GroovyCodeVisitor transformer) {
    source.getAST().getStatementBlock().visit(transformer);
    for (Object method : source.getAST().getMethods()) {
        MethodNode methodNode = (MethodNode) method;
        methodNode.getCode().visit(transformer);
    }
}
 
Example #10
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void checkReturnInObjectInitializer(List<Statement> init) {
    GroovyCodeVisitor visitor = new CodeVisitorSupport() {
        @Override
        public void visitClosureExpression(ClosureExpression expression) {
            // return is OK in closures in object initializers
        }
        @Override
        public void visitReturnStatement(ReturnStatement statement) {
            throw new RuntimeParserException("'return' is not allowed in object initializer", statement);
        }
    };
    for (Statement stmt : init) {
        stmt.visit(visitor);
    }
}
 
Example #11
Source File: AstUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void visitScriptCode(SourceUnit source, GroovyCodeVisitor transformer) {
    source.getAST().getStatementBlock().visit(transformer);
    for (Object method : source.getAST().getMethods()) {
        MethodNode methodNode = (MethodNode) method;
        methodNode.getCode().visit(transformer);
    }
}
 
Example #12
Source File: StaticTypesLambdaWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean isAccessingInstanceMembersOfEnclosingClass(final MethodNode syntheticLambdaMethodNode) {
    boolean[] result = new boolean[1];

    GroovyCodeVisitor visitor = new CodeVisitorSupport() {
        @Override
        public void visitVariableExpression(final VariableExpression expression) {
            if (expression.isThisExpression()) {
                result[0] = true;
            }
        }
    };
    syntheticLambdaMethodNode.getCode().visit(visitor);

    return result[0];
}
 
Example #13
Source File: StaticPropertyAccessHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    super.visit(visitor);
    if (visitor instanceof AsmClassGenerator) {
        // ignore the return of the call
        ((AsmClassGenerator) visitor).getController().getOperandStack().pop();
    }
}
 
Example #14
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator generator = (AsmClassGenerator) visitor;
        controller = generator.getController();
    }
    super.visit(visitor);
}
 
Example #15
Source File: AnnotationConstantExpression.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    AnnotationNode node = (AnnotationNode) getValue();
    Map<String, Expression> attrs = node.getMembers();
    for (Expression expr : attrs.values()) {
        expr.visit(visitor);
    }
    super.visit(visitor);
}
 
Example #16
Source File: StaticPropertyAccessHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    super.visit(visitor);
    if (visitor instanceof AsmClassGenerator) {
        tmp.remove(((AsmClassGenerator) visitor).getController());
    }
}
 
Example #17
Source File: PropertyExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(final GroovyCodeVisitor visitor) {
    visitor.visitPropertyExpression(this);
}
 
Example #18
Source File: ClassExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitClassExpression(this);
}
 
Example #19
Source File: UnaryPlusExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitUnaryPlusExpression(this);
}
 
Example #20
Source File: SpreadMapExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitSpreadMapExpression(this);
}
 
Example #21
Source File: ClosureExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitClosureExpression(this);
}
 
Example #22
Source File: SpreadExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitSpreadExpression(this);
}
 
Example #23
Source File: VariableExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitVariableExpression(this);
}
 
Example #24
Source File: TupleExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(final GroovyCodeVisitor visitor) {
    visitor.visitTupleExpression(this);
}
 
Example #25
Source File: AttributeExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitAttributeExpression(this);
}
 
Example #26
Source File: PrefixExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitPrefixExpression(this);
}
 
Example #27
Source File: ElvisOperatorExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitShortTernaryExpression(this);
}
 
Example #28
Source File: PostfixExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitPostfixExpression(this);
}
 
Example #29
Source File: BitwiseNegationExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    visitor.visitBitwiseNegationExpression(this);
}
 
Example #30
Source File: ListOfExpressionsExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    for (Expression expression : expressions) {
        expression.visit(visitor);
    }
}