Skip to content
Permalink
Browse files
minor refactor
  • Loading branch information
eric-milles authored and paulk-asert committed Mar 23, 2022
1 parent 9b105b8 commit cfdc3bcf3501cf693385f25d1bcbeb8c2d8ad968
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 60 deletions.
@@ -37,16 +37,22 @@ public List<AnnotationNode> getAnnotations() {
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);
}
}
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) {
@@ -25,6 +25,8 @@
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() {
@@ -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.&#160;{@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 ImportNode(final String packageName) {
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 ImportNode(final String packageName) {
* @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 ImportNode(final ClassNode type) {
* @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 ClassNode getType() {
}

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;
}
@@ -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 @@ protected void addGroovyObjectInterfaceAndMethods(final ClassNode node, final St
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 void visit(final MethodVisitor mv) {
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 void visit(final MethodVisitor mv) {
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);
}
}
}
@@ -238,6 +238,11 @@ private void addPhaseOperations() {
}
}, 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 @@ private void addPhaseOperations() {
}
});

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:

0 comments on commit cfdc3bc

Please sign in to comment.