org.codehaus.groovy.ast.CodeVisitorSupport Java Examples

The following examples show how to use org.codehaus.groovy.ast.CodeVisitorSupport. 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: DataSet.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static void visit(Closure closure, CodeVisitorSupport visitor) {
    if (closure != null) {
        ClassNode classNode = closure.getMetaClass().getClassNode();
        if (classNode == null) {
            throw new GroovyRuntimeException(
                    "DataSet unable to evaluate expression. AST not available for closure: " + closure.getMetaClass().getTheClass().getName() +
                            ". Is the source code on the classpath?");
        }
        List methods = classNode.getDeclaredMethods("doCall");
        if (!methods.isEmpty()) {
            MethodNode method = (MethodNode) methods.get(0);
            if (method != null) {
                Statement statement = method.getCode();
                if (statement != null) {
                    statement.visit(visitor);
                }
            }
        }
    }
}
 
Example #2
Source File: Verifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected void addConstructor(Parameter[] newParams, ConstructorNode ctor, Statement code, ClassNode type) {
    ConstructorNode newConstructor = type.addConstructor(ctor.getModifiers(), newParams, ctor.getExceptions(), code);
    newConstructor.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
    markAsGenerated(type, newConstructor);
    // TODO: Copy annotations, etc.?

    // set anon. inner enclosing method reference
    code.visit(new CodeVisitorSupport() {
        @Override
        public void visitConstructorCallExpression(ConstructorCallExpression call) {
            if (call.isUsingAnonymousInnerClass()) {
                call.getType().setEnclosingMethod(newConstructor);
            }
            super.visitConstructorCallExpression(call);
        }
    });
}
 
Example #3
Source File: EnumCompletionVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private String getUniqueVariableName(final String name, Statement code) {
    if (code == null) return name;
    final Object[] found = new Object[1];
    CodeVisitorSupport cv = new CodeVisitorSupport() {
        public void visitVariableExpression(VariableExpression expression) {
            if (expression.getName().equals(name)) found[0] = Boolean.TRUE;
        }
    };
    code.visit(cv);
    if (found[0] != null) return getUniqueVariableName("_" + name, code);
    return name;
}
 
Example #4
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 #5
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];
}