You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/07/06 18:27:36 UTC
[groovy] 02/03: refactor duplication
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 9472fd02894201de1bcbf45cfd48c1ee45d2282e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jul 6 12:57:02 2022 -0500
refactor duplication
---
.../codehaus/groovy/control/ResolveVisitor.java | 54 +++++++---------------
1 file changed, 16 insertions(+), 38 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 79e7c4fbec..31ab094b0f 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -19,7 +19,6 @@
package org.codehaus.groovy.control;
import groovy.lang.Tuple2;
-import org.apache.groovy.ast.tools.ExpressionUtils;
import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotationNode;
@@ -44,7 +43,6 @@ import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.ast.expr.CastExpression;
import org.codehaus.groovy.ast.expr.ClassExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression;
-import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
@@ -81,7 +79,7 @@ import java.util.Set;
import java.util.function.Predicate;
import static groovy.lang.Tuple.tuple;
-import static org.codehaus.groovy.ast.ClassHelper.isObjectType;
+import static org.apache.groovy.ast.tools.ExpressionUtils.transformInlineConstants;
import static org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe;
/**
@@ -428,7 +426,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
ClassNode cn = currentClass;
Set<ClassNode> cycleCheck = new HashSet<>();
// GROOVY-4043: for type "X", try "A$X" with each type in the class hierarchy (except for Object)
- for (; cn != null && cycleCheck.add(cn) && !isObjectType(cn); cn = cn.getSuperClass()) {
+ for (; cn != null && cycleCheck.add(cn) && !ClassHelper.isObjectType(cn); cn = cn.getSuperClass()) {
if (setRedirect(type, cn)) return true;
}
@@ -1196,12 +1194,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
}
protected Expression transformAnnotationConstantExpression(final AnnotationConstantExpression ace) {
- AnnotationNode an = (AnnotationNode) ace.getValue();
- ClassNode type = an.getClassNode();
- resolveOrFail(type, " for annotation", an);
- for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
- member.setValue(transform(member.getValue()));
- }
+ visitAnnotation((AnnotationNode) ace.getValue());
return ace;
}
@@ -1233,40 +1226,25 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
@Override
protected void visitAnnotation(final AnnotationNode node) {
resolveOrFail(node.getClassNode(), " for annotation", node, true);
-
- for (Map.Entry<String, Expression> member : node.getMembers().entrySet()) {
- Expression value = transformInlineConstants(transform(member.getValue()));
+ // duplicates part of AnnotationVisitor because we cannot wait until later
+ for (Map.Entry<String, Expression> entry : node.getMembers().entrySet()) {
+ // resolve constant-looking expressions statically
+ // do it now since they get transformed away later
+ Expression value = transform(entry.getValue());
+ value = transformInlineConstants(value);
checkAnnotationMemberValue(value);
- member.setValue(value);
- }
- }
-
- // resolve constant-looking expressions statically (do here as they get transformed away later)
- private static Expression transformInlineConstants(final Expression exp) {
- if (exp instanceof AnnotationConstantExpression) {
- ConstantExpression ce = (ConstantExpression) exp;
- if (ce.getValue() instanceof AnnotationNode) {
- // replicate a little bit of AnnotationVisitor here
- // because we can't wait until later to do this
- AnnotationNode an = (AnnotationNode) ce.getValue();
- for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
- member.setValue(transformInlineConstants(member.getValue()));
- }
- }
- } else {
- return ExpressionUtils.transformInlineConstants(exp);
+ entry.setValue(value);
}
- return exp;
}
- private void checkAnnotationMemberValue(final Expression newValue) {
- if (newValue instanceof PropertyExpression) {
- PropertyExpression pe = (PropertyExpression) newValue;
+ private void checkAnnotationMemberValue(final Expression value) {
+ if (value instanceof PropertyExpression) {
+ PropertyExpression pe = (PropertyExpression) value;
if (!(pe.getObjectExpression() instanceof ClassExpression)) {
addError("unable to find class '" + pe.getText() + "' for annotation attribute constant", pe.getObjectExpression());
}
- } else if (newValue instanceof ListExpression) {
- ListExpression le = (ListExpression) newValue;
+ } else if (value instanceof ListExpression) {
+ ListExpression le = (ListExpression) value;
for (Expression e : le.getExpressions()) {
checkAnnotationMemberValue(e);
}
@@ -1547,7 +1525,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
GenericsType[] parameters = type.redirect().getGenericsTypes();
if (parameters != null && i < parameters.length) {
ClassNode implicitBound = parameters[i].getType();
- if (!isObjectType(implicitBound))
+ if (!ClassHelper.isObjectType(implicitBound))
argument.getType().setRedirect(implicitBound);
}
}