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;
}
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));
}
}
}
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);
}
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;
}
});
}
}
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);
}
Aggregations