Search in sources :

Example 46 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project repository-permissions-updater by jenkins-infra.

the class GradleVerifier method getGroupId.

public static String getGroupId(String contents) {
    String res = null;
    AstBuilder astBuilder = new AstBuilder();
    List<ASTNode> nodes = astBuilder.buildFromString(CompilePhase.SEMANTIC_ANALYSIS, false, contents);
    BlockStatement node = (BlockStatement) nodes.get(0);
    for (Statement s : node.getStatements()) {
        Expression e = ((ExpressionStatement) s).getExpression();
        if (e instanceof BinaryExpression) {
            BinaryExpression be = (BinaryExpression) e;
            VariableExpression v = (VariableExpression) be.getLeftExpression();
            if (v.getName().equals("group")) {
                if (be.getRightExpression() instanceof ConstantExpression) {
                    res = be.getRightExpression().getText();
                    break;
                }
            }
        }
    }
    return res;
}
Also used : AstBuilder(org.codehaus.groovy.ast.builder.AstBuilder) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ASTNode(org.codehaus.groovy.ast.ASTNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 47 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project repository-permissions-updater by jenkins-infra.

the class GradleVerifier method verify.

@Override
public void verify(HostingRequest issue, HashSet<VerificationMessage> hostingIssues) throws IOException {
    GitHub github = GitHub.connect();
    forkFrom = issue.getRepositoryUrl();
    forkTo = issue.getNewRepoName();
    if (StringUtils.isNotBlank(forkFrom)) {
        Matcher m = Pattern.compile("(?:https://github\\.com/)?(\\S+)/(\\S+)", CASE_INSENSITIVE).matcher(forkFrom);
        if (m.matches()) {
            String owner = m.group(1);
            String repoName = m.group(2);
            GHRepository repo = github.getRepository(owner + "/" + repoName);
            try {
                GHContent buildGradle = repo.getFileContent("build.gradle");
                if (buildGradle != null) {
                    InputStream input = buildGradle.read();
                    AstBuilder astBuilder = new AstBuilder();
                    List<ASTNode> nodes = astBuilder.buildFromString(CompilePhase.SEMANTIC_ANALYSIS, false, IOUtils.toString(input, Charset.defaultCharset()));
                    BlockStatement node = (BlockStatement) nodes.get(0);
                    for (Statement s : node.getStatements()) {
                        if (s instanceof ExpressionStatement) {
                            Expression e = ((ExpressionStatement) s).getExpression();
                            if (e instanceof MethodCallExpression) {
                                MethodCallExpression mc = (MethodCallExpression) e;
                                if (mc.getMethodAsString().equals("plugins")) {
                                    // make sure we get the correct version of the gradle jpi plugin
                                    checkPluginVersion(((ArgumentListExpression) mc.getArguments()).getExpression(0), hostingIssues);
                                } else if (mc.getMethodAsString().equals("repositories")) {
                                    // verify that any references to repo.jenkins-ci.org are correct
                                    checkRepositories(((ArgumentListExpression) mc.getArguments()).getExpression(0), hostingIssues);
                                } else if (mc.getMethodAsString().equals("jenkinsPlugin")) {
                                    // verify the things that will make it into the pom.xml that is published
                                    checkJenkinsPlugin(((ArgumentListExpression) mc.getArguments()).getExpression(0), hostingIssues);
                                }
                            } else if (e instanceof BinaryExpression) {
                                BinaryExpression be = (BinaryExpression) e;
                                VariableExpression v = (VariableExpression) be.getLeftExpression();
                                if (v.getName().equals("group")) {
                                    checkGroup(be.getRightExpression(), hostingIssues);
                                } else if (v.getName().equals("targetCompatibility")) {
                                    checkTargetCompatibility(be.getRightExpression(), hostingIssues);
                                }
                            }
                        }
                    // There are other possibilities here, but we currently don't take them into account
                    }
                    if (!hasJenkinsVersion) {
                        hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.REQUIRED, MISSING_JENKINS_VERSION));
                    }
                    if (!hasShortName) {
                        hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.REQUIRED, MISSING_SHORTNAME));
                    }
                    if (!hasDisplayName) {
                        hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.REQUIRED, MISSING_DISPLAYNAME));
                    }
                    if (!hasGroup) {
                        hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.REQUIRED, MISSING_GROUP));
                    }
                    if (!hasLicense) {
                        hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.REQUIRED, SPECIFY_LICENSE));
                    }
                } else {
                    hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.WARNING, NO_BUILD_GRADLE_FOUND));
                }
            } catch (GHFileNotFoundException e) {
                hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.WARNING, NO_BUILD_GRADLE_FOUND));
            } catch (CompilationFailedException e) {
                hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.REQUIRED, INVALID_BUILD_GRADLE));
            }
        } else {
            hostingIssues.add(new VerificationMessage(VerificationMessage.Severity.REQUIRED, HostingChecker.INVALID_FORK_FROM, forkFrom));
        }
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) Matcher(java.util.regex.Matcher) GitHub(org.kohsuke.github.GitHub) InputStream(java.io.InputStream) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) GHContent(org.kohsuke.github.GHContent) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) AstBuilder(org.codehaus.groovy.ast.builder.AstBuilder) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ASTNode(org.codehaus.groovy.ast.ASTNode)

Example 48 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.

the class StaticTypesTypeChooser method resolveType.

@Override
public ClassNode resolveType(final Expression exp, final ClassNode current) {
    // see GROOVY-9344, GROOVY-9607
    ASTNode target = getTarget(exp);
    ClassNode inferredType = target.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
    if (inferredType == null) {
        inferredType = target.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    }
    if (inferredType != null && !isPrimitiveVoid(inferredType)) {
        return inferredType;
    }
    // AsmClassGenerator may create "this" expressions that the type checker knows nothing about
    if (target instanceof VariableExpression && ((VariableExpression) target).isThisExpression()) {
        return current;
    }
    return super.resolveType(exp, current);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ASTNode(org.codehaus.groovy.ast.ASTNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 49 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.

the class Verifier method visitConstructor.

@Override
public void visitConstructor(final ConstructorNode node) {
    Statement stmt = node.getCode();
    if (stmt != null) {
        stmt.visit(new VerifierCodeVisitor(getClassNode()));
        // check for uninitialized-this references
        stmt.visit(new CodeVisitorSupport() {

            @Override
            public void visitClosureExpression(final ClosureExpression ce) {
                boolean oldInClosure = inClosure;
                inClosure = true;
                super.visitClosureExpression(ce);
                inClosure = oldInClosure;
            }

            @Override
            public void visitConstructorCallExpression(final ConstructorCallExpression cce) {
                boolean oldIsSpecialConstructorCall = inSpecialConstructorCall;
                inSpecialConstructorCall |= cce.isSpecialCall();
                super.visitConstructorCallExpression(cce);
                inSpecialConstructorCall = oldIsSpecialConstructorCall;
            }

            @Override
            public void visitMethodCallExpression(final MethodCallExpression mce) {
                if (inSpecialConstructorCall && isThisObjectExpression(mce)) {
                    MethodNode methodTarget = mce.getMethodTarget();
                    if (methodTarget == null || !(methodTarget.isStatic() || classNode.getOuterClasses().contains(methodTarget.getDeclaringClass()))) {
                        if (!mce.isImplicitThis()) {
                            throw newVariableError(mce.getObjectExpression().getText(), mce.getObjectExpression());
                        } else {
                            throw newVariableError(mce.getMethodAsString(), mce.getMethod());
                        }
                    }
                    mce.getMethod().visit(this);
                    mce.getArguments().visit(this);
                } else {
                    super.visitMethodCallExpression(mce);
                }
            }

            @Override
            public void visitVariableExpression(final VariableExpression ve) {
                // before this/super ctor call completes, only params and static or outer members are accessible
                if (inSpecialConstructorCall && (ve.isThisExpression() || ve.isSuperExpression() || isNonStaticMemberAccess(ve))) {
                    // TODO: context for default argument
                    throw newVariableError(ve.getName(), ve.getLineNumber() > 0 ? ve : node);
                }
            }

            // 
            private boolean inClosure, inSpecialConstructorCall;

            private boolean isNonStaticMemberAccess(final VariableExpression ve) {
                Variable variable = ve.getAccessedVariable();
                return !inClosure && variable != null && !isStatic(variable.getModifiers()) && !(variable instanceof DynamicVariable) && !(variable instanceof Parameter);
            }

            private boolean isThisObjectExpression(final MethodCallExpression mce) {
                if (mce.isImplicitThis()) {
                    return true;
                } else if (mce.getObjectExpression() instanceof VariableExpression) {
                    VariableExpression var = (VariableExpression) mce.getObjectExpression();
                    return var.isThisExpression() || var.isSuperExpression();
                } else {
                    return false;
                }
            }

            private GroovyRuntimeException newVariableError(final String name, final ASTNode node) {
                RuntimeParserException rpe = new RuntimeParserException("Cannot reference '" + name + "' before supertype constructor has been called. Possible causes:\n" + "You attempted to access an instance field, method, or property.\n" + "You attempted to construct a non-static inner class.", node);
                rpe.setModule(getClassNode().getModule());
                return rpe;
            }
        });
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CodeVisitorSupport(org.codehaus.groovy.ast.CodeVisitorSupport) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ASTNode(org.codehaus.groovy.ast.ASTNode) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException)

Example 50 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project spring-boot by spring-projects.

the class AnnotatedNodeASTTransformation method visit.

@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    this.sourceUnit = source;
    List<AnnotationNode> annotationNodes = new ArrayList<>();
    ClassVisitor classVisitor = new ClassVisitor(source, annotationNodes);
    for (ASTNode node : nodes) {
        if (node instanceof ModuleNode) {
            ModuleNode module = (ModuleNode) node;
            visitAnnotatedNode(module.getPackage(), annotationNodes);
            for (ImportNode importNode : module.getImports()) {
                visitAnnotatedNode(importNode, annotationNodes);
            }
            for (ImportNode importNode : module.getStarImports()) {
                visitAnnotatedNode(importNode, annotationNodes);
            }
            module.getStaticImports().forEach((name, importNode) -> visitAnnotatedNode(importNode, annotationNodes));
            module.getStaticStarImports().forEach((name, importNode) -> visitAnnotatedNode(importNode, annotationNodes));
            for (ClassNode classNode : module.getClasses()) {
                visitAnnotatedNode(classNode, annotationNodes);
                classNode.visitContents(classVisitor);
            }
        }
    }
    processAnnotationNodes(annotationNodes);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList) ASTNode(org.codehaus.groovy.ast.ASTNode) ImportNode(org.codehaus.groovy.ast.ImportNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Aggregations

ASTNode (org.codehaus.groovy.ast.ASTNode)53 MethodNode (org.codehaus.groovy.ast.MethodNode)22 ClassNode (org.codehaus.groovy.ast.ClassNode)20 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)19 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)19 Expression (org.codehaus.groovy.ast.expr.Expression)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)15 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)13 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)13 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)13 Statement (org.codehaus.groovy.ast.stmt.Statement)13 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)11 Parameter (org.codehaus.groovy.ast.Parameter)11 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)11 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)10 ArrayList (java.util.ArrayList)9 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)9 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)9 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)7