Java源码示例:org.codehaus.groovy.ast.ClassCodeVisitorSupport

示例1
@Override
public void call(final SourceUnit source) throws CompilationFailedException {
    // currently we only look in script code; could extend this to build script classes
    AstUtils.visitScriptCode(source, new ClassCodeVisitorSupport() {
        @Override
        protected SourceUnit getSourceUnit() {
            return source;
        }

        @Override
        protected void visitStatement(Statement statement) {
            if (statement.getStatementLabel() != null) {
                String message = String.format("Statement labels may not be used in build scripts.%nIn case you tried to configure a property named '%s', replace ':' with '=' or ' ', otherwise it will not have the desired effect.",
                        statement.getStatementLabel());
                addError(message, statement);
            }
        }
    });
}
 
示例2
@Override
public void call(final SourceUnit source) throws CompilationFailedException {
    // currently we only look in script code; could extend this to build script classes
    AstUtils.visitScriptCode(source, new ClassCodeVisitorSupport() {
        @Override
        protected SourceUnit getSourceUnit() {
            return source;
        }

        @Override
        protected void visitStatement(Statement statement) {
            if (statement.getStatementLabel() != null) {
                String message = String.format("Statement labels may not be used in build scripts.%nIn case you tried to configure a property named '%s', replace ':' with '=' or ' ', otherwise it will not have the desired effect.",
                        statement.getStatementLabel());
                addError(message, statement);
            }
        }
    });
}
 
示例3
@Override
public void call(final SourceUnit source) throws CompilationFailedException {
    final List<Statement> logStats = Lists.newArrayList();

    // currently we only look in script code; could extend this to build script classes
    AstUtils.visitScriptCode(source, new ClassCodeVisitorSupport() {
        @Override
        protected SourceUnit getSourceUnit() {
            return source;
        }

        @Override
        protected void visitStatement(Statement statement) {
            if (statement.getStatementLabel() != null) {
                // Because we aren't failing the build, the script will be cached and this transformer won't run the next time.
                // In order to make the deprecation warning stick, we have to weave the call to StatementLabelsDeprecationLogger
                // into the build script.
                String label = statement.getStatementLabel();
                String sample = source.getSample(statement.getLineNumber(), statement.getColumnNumber(), null);
                Expression logExpr = new StaticMethodCallExpression(ClassHelper.makeWithoutCaching(StatementLabelsDeprecationLogger.class), "log",
                        new ArgumentListExpression(new ConstantExpression(label), new ConstantExpression(sample)));
                logStats.add(new ExpressionStatement(logExpr));
            }
        }
    });

    source.getAST().getStatementBlock().addStatements(logStats);
}
 
示例4
private static void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
    markAsGenerated(cNode, constructorNode);
    cNode.addConstructor(constructorNode);
    // GROOVY-5814: Immutable is not compatible with @CompileStatic
    Parameter argsParam = null;
    for (Parameter p : constructorNode.getParameters()) {
        if ("args".equals(p.getName())) {
            argsParam = p;
            break;
        }
    }
    if (argsParam != null) {
        final Parameter arg = argsParam;
        ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() {
            @Override
            protected SourceUnit getSourceUnit() {
                return cNode.getModule().getContext();
            }

            @Override
            public void visitVariableExpression(final VariableExpression expression) {
                super.visitVariableExpression(expression);
                if ("args".equals(expression.getName())) {
                    expression.setAccessedVariable(arg);
                }
            }
        };
        variableExpressionFix.visitConstructor(constructorNode);
    }
}
 
示例5
private void process(ASTNode[] nodes, final ClassCodeVisitorSupport visitor) {
    candidate = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode())) return;

    if (candidate instanceof ClassNode) {
        processClass((ClassNode) candidate, visitor);
    } else if (candidate instanceof MethodNode) {
        processConstructorOrMethod((MethodNode) candidate, visitor);
    } else if (candidate instanceof FieldNode) {
        processField((FieldNode) candidate, visitor);
    } else if (candidate instanceof DeclarationExpression) {
        processLocalVariable((DeclarationExpression) candidate, visitor);
    }
}
 
示例6
private void processClass(ClassNode cNode, final ClassCodeVisitorSupport visitor) {
    if (!isEnabled(cNode)) return;
    if (cNode.isInterface()) {
        addError("Error processing interface '" + cNode.getName() +
                "'. " + MY_TYPE_NAME + " only allowed for classes.", cNode);
        return;
    }

    for (ConstructorNode cn : cNode.getDeclaredConstructors()) {
        if (hasNoExplicitAutoFinal(cn)) {
            processConstructorOrMethod(cn, visitor);
        }
    }

    for (MethodNode mn : cNode.getAllDeclaredMethods()) {
        if (hasNoExplicitAutoFinal(mn)) {
            processConstructorOrMethod(mn, visitor);
        }
    }

    Iterator<InnerClassNode> it = cNode.getInnerClasses();
    while (it.hasNext()) {
        InnerClassNode in = it.next();
        if (in.getAnnotations(MY_TYPE).isEmpty()) {
            processClass(in, visitor);
        }
    }

    visitor.visitClass(cNode);
}
 
示例7
private void processConstructorOrMethod(MethodNode mNode, ClassCodeVisitorSupport visitor) {
    if (!isEnabled(mNode)) return;
    if (mNode.isSynthetic()) return;
    Parameter[] origParams = mNode.getParameters();
    for (Parameter p : origParams) {
        p.setModifiers(p.getModifiers() | Modifier.FINAL);
    }
    visitor.visitMethod(mNode);
}
 
示例8
@Override
public void call(final SourceUnit source) throws CompilationFailedException {
    final List<Statement> logStats = Lists.newArrayList();

    // currently we only look in script code; could extend this to build script classes
    AstUtils.visitScriptCode(source, new ClassCodeVisitorSupport() {
        @Override
        protected SourceUnit getSourceUnit() {
            return source;
        }

        @Override
        protected void visitStatement(Statement statement) {
            if (statement.getStatementLabel() != null) {
                // Because we aren't failing the build, the script will be cached and this transformer won't run the next time.
                // In order to make the deprecation warning stick, we have to weave the call to StatementLabelsDeprecationLogger
                // into the build script.
                String label = statement.getStatementLabel();
                String sample = source.getSample(statement.getLineNumber(), statement.getColumnNumber(), null);
                Expression logExpr = new StaticMethodCallExpression(ClassHelper.makeWithoutCaching(StatementLabelsDeprecationLogger.class), "log",
                        new ArgumentListExpression(new ConstantExpression(label), new ConstantExpression(sample)));
                logStats.add(new ExpressionStatement(logExpr));
            }
        }
    });

    source.getAST().getStatementBlock().addStatements(logStats);
}
 
示例9
public static ListExpression buildSubstitutions(final SourceUnit source, final ASTNode expr) {
    final ListExpression listExpression = new ListExpression();

    ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {
        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }

        @Override
        public void visitClass(final ClassNode node) {
            super.visitClass(node);
            Iterator<InnerClassNode> it = node.getInnerClasses();
            while (it.hasNext()) {
                InnerClassNode next = it.next();
                visitClass(next);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            super.visitMethodCallExpression(call);

            if (DOLLAR_VALUE.equals(call.getMethodAsString())) {
                ClosureExpression substitutionClosureExpression = getClosureArgument(source, call);

                if (substitutionClosureExpression == null) {
                    return;
                }

                Statement code = substitutionClosureExpression.getCode();
                if (code instanceof BlockStatement) {
                    ((BlockStatement) code).setVariableScope(null);
                }

                listExpression.addExpression(substitutionClosureExpression);
            }
        }
    };
    if (expr instanceof ClassNode) {
        visitor.visitClass((ClassNode) expr);
    } else {
        expr.visit(visitor);
    }
    return listExpression;
}
 
示例10
public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    final ClassCodeVisitorSupport visitor = createVisitor();
    process(nodes, visitor);
}
 
示例11
private void processLocalVariable(DeclarationExpression de, ClassCodeVisitorSupport visitor) {
    if (!isEnabled(de)) return;
    if (de.getRightExpression() instanceof ClosureExpression) {
        visitor.visitDeclarationExpression(de);
    }
}
 
示例12
private void processField(FieldNode fNode, ClassCodeVisitorSupport visitor) {
    if (!isEnabled(fNode)) return;
    if (fNode.hasInitialExpression() && fNode.getInitialExpression() instanceof ClosureExpression) {
        visitor.visitField(fNode);
    }
}
 
示例13
public boolean existsProperty(final PropertyExpression pexp, final boolean checkForReadOnly, final ClassCodeVisitorSupport visitor) {
    return typeCheckingVisitor.existsProperty(pexp, checkForReadOnly, visitor);
}