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/03/22 17:33:29 UTC
[groovy] 01/01: minor refactor
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch minor-refactor
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 080789f5f9eeb8630e9e6c337f5ff2acac5b89cd
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Mar 22 12:33:17 2022 -0500
minor refactor
---
.../org/codehaus/groovy/ast/AnnotatedNode.java | 10 +++-
.../org/codehaus/groovy/ast/AnnotationNode.java | 4 +-
.../java/org/codehaus/groovy/ast/ImportNode.java | 59 +++++++---------------
.../org/codehaus/groovy/classgen/Verifier.java | 22 ++++----
.../codehaus/groovy/control/CompilationUnit.java | 9 ++--
5 files changed, 44 insertions(+), 60 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java b/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java
index 10c12db..ed3d4bd 100644
--- a/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java
@@ -37,9 +37,9 @@ public class AnnotatedNode extends ASTNode implements GroovydocHolder<AnnotatedN
return annotations;
}
- public List<AnnotationNode> getAnnotations(ClassNode type) {
+ public List<AnnotationNode> getAnnotations(final ClassNode type) {
List<AnnotationNode> ret = new ArrayList<>(annotations.size());
- for (AnnotationNode node : annotations) {
+ for (AnnotationNode node : getAnnotations()) {
if (type.equals(node.getClassNode())) {
ret.add(node);
}
@@ -47,6 +47,12 @@ public class AnnotatedNode extends ASTNode implements GroovydocHolder<AnnotatedN
return ret;
}
+ public AnnotationNode addAnnotation(final ClassNode type) {
+ AnnotationNode node = new AnnotationNode(type);
+ addAnnotation(node);
+ return node;
+ }
+
public void addAnnotation(AnnotationNode annotation) {
if (annotation != null) {
if (annotations == Collections.EMPTY_LIST) {
diff --git a/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java b/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
index da27918..5a32327 100644
--- a/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
@@ -25,6 +25,8 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
+import static java.util.Objects.requireNonNull;
+
/**
* Represents an annotation which can be attached to interfaces, classes, methods, fields, parameters, and other places.
*/
@@ -50,7 +52,7 @@ public class AnnotationNode extends ASTNode {
private int allowedTargets = ALL_TARGETS;
public AnnotationNode(ClassNode classNode) {
- this.classNode = classNode;
+ this.classNode = requireNonNull(classNode);
}
public ClassNode getClassNode() {
diff --git a/src/main/java/org/codehaus/groovy/ast/ImportNode.java b/src/main/java/org/codehaus/groovy/ast/ImportNode.java
index d72bc23..83ede6b 100644
--- a/src/main/java/org/codehaus/groovy/ast/ImportNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ImportNode.java
@@ -18,7 +18,7 @@
*/
package org.codehaus.groovy.ast;
-import static java.util.Objects.requireNonNull;
+import java.util.Objects;
/**
* Represents an import statement.
@@ -31,7 +31,7 @@ public class ImportNode extends AnnotatedNode {
private final String packageName;
private final boolean isStar;
private final boolean isStatic;
- private int hashCode;
+ private transient int hashCode;
/**
* An import of a single type, i.e. {@code import pack.Type} or {@code import pack.Type as Alias}
@@ -40,7 +40,7 @@ public class ImportNode extends AnnotatedNode {
* @param alias optional alias
*/
public ImportNode(final ClassNode type, final String alias) {
- this.type = requireNonNull(type);
+ this.type = Objects.requireNonNull(type);
this.alias = alias;
this.isStar = false;
this.isStatic = false;
@@ -58,7 +58,7 @@ public class ImportNode extends AnnotatedNode {
this.alias = null;
this.isStar = true;
this.isStatic = false;
- this.packageName = requireNonNull(packageName);
+ this.packageName = Objects.requireNonNull(packageName);
this.fieldName = null;
}
@@ -68,7 +68,7 @@ public class ImportNode extends AnnotatedNode {
* @param type the type reference
*/
public ImportNode(final ClassNode type) {
- this.type = requireNonNull(type);
+ this.type = Objects.requireNonNull(type);
this.alias = null;
this.isStar = true;
this.isStatic = true;
@@ -84,12 +84,12 @@ public class ImportNode extends AnnotatedNode {
* @param alias optional alias
*/
public ImportNode(final ClassNode type, final String fieldName, final String alias) {
- this.type = requireNonNull(type);
+ this.type = Objects.requireNonNull(type);
this.alias = alias;
this.isStar = false;
this.isStatic = true;
this.packageName = null;
- this.fieldName = requireNonNull(fieldName);
+ this.fieldName = Objects.requireNonNull(fieldName);
}
/**
@@ -145,53 +145,32 @@ public class ImportNode extends AnnotatedNode {
}
public void setType(final ClassNode type) {
- this.type = type;
+ this.type = Objects.requireNonNull(type);
hashCode = 0;
}
@Override
- public boolean equals(Object o) {
- if (!(o instanceof ImportNode))
- return false;
- ImportNode imp = (ImportNode) o;
- if ((type == null) != (imp.type == null))
- return false;
- if (type != null && !type.equals(imp.type))
- return false;
- if ((alias == null) != (imp.alias == null))
- return false;
- if (alias != null && !alias.equals(imp.alias))
- return false;
- if ((fieldName == null) != (imp.fieldName == null))
- return false;
- if (fieldName != null && !fieldName.equals(imp.fieldName))
- return false;
- if ((packageName == null) != (imp.packageName == null))
+ public boolean equals(Object that) {
+ if (that == this) return true;
+ if (!(that instanceof ImportNode)) return false;
+
+ ImportNode node = (ImportNode) that;
+ if (!Objects.equals(type, node.type))
return false;
- if (packageName != null && !packageName.equals(imp.packageName))
+ if (!Objects.equals(alias, node.alias))
return false;
- if (isStar != imp.isStar)
+ if (!Objects.equals(fieldName, node.fieldName))
return false;
- if (isStatic != imp.isStatic)
+ if (!Objects.equals(packageName, node.packageName))
return false;
- return true;
+ return (isStar == node.isStar && isStatic == node.isStatic);
}
@Override
public int hashCode() {
int result = hashCode;
if (result == 0) {
- if (type != null)
- result = 31 * result + type.hashCode();
- if (alias != null)
- result = 31 * result + alias.hashCode();
- if (fieldName != null)
- result = 31 * result + fieldName.hashCode();
- if (packageName != null)
- result = 31 * result + packageName.hashCode();
- result = 31 * result +Boolean.hashCode(isStar);
- result = 31 * result +Boolean.hashCode(isStatic);
- hashCode = result;
+ hashCode = Objects.hash(type, alias, fieldName, packageName, isStar, isStatic);
}
return result;
}
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 271a20e..78db90a 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -154,9 +154,9 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
public static final String INITIAL_EXPRESSION = "INITIAL_EXPRESSION";
public static final String DEFAULT_PARAMETER_GENERATED = "DEFAULT_PARAMETER_GENERATED";
- private static final Class<?> GENERATED_ANNOTATION = Generated.class;
- private static final Class<?> INTERNAL_ANNOTATION = Internal.class;
- private static final Class<?> TRANSIENT_ANNOTATION = Transient.class;
+ private static final ClassNode GENERATED_ANNOTATION = ClassHelper.make(Generated.class);
+ private static final ClassNode INTERNAL_ANNOTATION = ClassHelper.make(Internal .class);
+ private static final ClassNode TRANSIENT_ANNOTATION = ClassHelper.make(Transient.class);
// NOTE: timeStamp constants shouldn't belong to Verifier but kept here for binary compatibility
public static final String __TIMESTAMP = "__timeStamp";
@@ -519,11 +519,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
if (!node.isDerivedFromGroovyObject()) node.addInterface(ClassHelper.GROOVY_OBJECT_TYPE);
FieldNode metaClassField = getMetaClassField(node);
- boolean shouldAnnotate = classNode.getModule().getContext() != null;
- AnnotationNode generatedAnnotation = shouldAnnotate ? new AnnotationNode(ClassHelper.make(GENERATED_ANNOTATION)) : null;
- AnnotationNode internalAnnotation = shouldAnnotate ? new AnnotationNode(ClassHelper.make(INTERNAL_ANNOTATION)) : null;
- AnnotationNode transientAnnotation = shouldAnnotate ? new AnnotationNode(ClassHelper.make(TRANSIENT_ANNOTATION)) : null;
-
+ boolean shouldAnnotate = (classNode.getModule().getContext() != null);
if (!node.hasMethod("getMetaClass", Parameter.EMPTY_ARRAY)) {
metaClassField = setMetaClassFieldIfNotExists(node, metaClassField);
Statement getMetaClassCode = new BytecodeSequence(new BytecodeInstruction() {
@@ -563,9 +559,9 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
MethodNode methodNode = addMethod(node, !shouldAnnotate, "getMetaClass", ACC_PUBLIC,
ClassHelper.METACLASS_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getMetaClassCode);
if (shouldAnnotate) {
- methodNode.addAnnotation(generatedAnnotation);
- methodNode.addAnnotation(internalAnnotation);
- methodNode.addAnnotation(transientAnnotation);
+ methodNode.addAnnotation(GENERATED_ANNOTATION);
+ methodNode.addAnnotation(INTERNAL_ANNOTATION );
+ methodNode.addAnnotation(TRANSIENT_ANNOTATION);
}
}
@@ -593,8 +589,8 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
MethodNode methodNode = addMethod(node, !shouldAnnotate, "setMetaClass", ACC_PUBLIC,
ClassHelper.VOID_TYPE, SET_METACLASS_PARAMS, ClassNode.EMPTY_ARRAY, setMetaClassCode);
if (shouldAnnotate) {
- methodNode.addAnnotation(generatedAnnotation);
- methodNode.addAnnotation(internalAnnotation);
+ methodNode.addAnnotation(GENERATED_ANNOTATION);
+ methodNode.addAnnotation(INTERNAL_ANNOTATION);
}
}
}
diff --git a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
index 0780a21..24e81cd 100644
--- a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
+++ b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
@@ -239,6 +239,11 @@ public class CompilationUnit extends ProcessingUnit {
}, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
+ AnnotationCollectorTransform.ClassChanger xformer = new AnnotationCollectorTransform.ClassChanger();
+ xformer.transformClass(classNode);
+ }, Phases.SEMANTIC_ANALYSIS);
+
+ addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
TraitComposer.doExtendTraits(classNode, source, this);
}, Phases.CANONICALIZATION);
@@ -295,10 +300,6 @@ public class CompilationUnit extends ProcessingUnit {
}
});
- addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
- AnnotationCollectorTransform.ClassChanger xformer = new AnnotationCollectorTransform.ClassChanger();
- xformer.transformClass(classNode);
- }, Phases.SEMANTIC_ANALYSIS);
ASTTransformationVisitor.addPhaseOperations(this);
// post-transform operations: