groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [groovy] 01/02: GROOVY-9054: Missing @Generated annotation in @Builder (closes #929)
Date Sun, 12 May 2019 10:53:33 GMT
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 846edff9804840debec266fd28723cad8fddaef9
Author: aalmiray <aalmiray@gmail.com>
AuthorDate: Sun May 12 10:14:06 2019 +0200

    GROOVY-9054: Missing @Generated annotation in @Builder (closes #929)
---
 .../groovy/groovy/transform/builder/DefaultStrategy.java    | 12 ++++++++----
 .../groovy/groovy/transform/builder/ExternalStrategy.java   |  7 +++++--
 .../groovy/transform/builder/InitializerStrategy.java       |  3 ++-
 .../groovy/groovy/transform/builder/SimpleStrategy.java     | 13 +++++++------
 .../org/apache/groovy/ast/tools/AnnotatedNodeUtils.java     |  3 ++-
 5 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/src/main/groovy/groovy/transform/builder/DefaultStrategy.java b/src/main/groovy/groovy/transform/builder/DefaultStrategy.java
index e3cd82a..f92be1e 100644
--- a/src/main/groovy/groovy/transform/builder/DefaultStrategy.java
+++ b/src/main/groovy/groovy/transform/builder/DefaultStrategy.java
@@ -35,6 +35,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
 import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX;
@@ -182,12 +184,13 @@ public class DefaultStrategy extends BuilderASTTransformation.AbstractBuilderStr
         }
         ClassNode buildee = mNode.getDeclaringClass();
         ClassNode builder = createBuilder(anno, buildee);
+        markAsGenerated(buildee, builder);
         createBuilderFactoryMethod(anno, buildee, builder);
         for (Parameter parameter : mNode.getParameters()) {
             builder.addField(createFieldCopy(buildee, parameter));
             builder.addMethod(createBuilderMethodForProp(builder, new PropertyInfo(parameter.getName(),
parameter.getType()), getPrefix(anno)));
         }
-        builder.addMethod(createBuildMethodForMethod(anno, buildee, mNode, mNode.getParameters()));
+        addGeneratedMethod(buildee, createBuildMethodForMethod(anno, buildee, mNode, mNode.getParameters()));
     }
 
     public void buildClass(BuilderASTTransformation transform, ClassNode buildee, AnnotationNode
anno) {
@@ -197,6 +200,7 @@ public class DefaultStrategy extends BuilderASTTransformation.AbstractBuilderStr
         if (!getIncludeExclude(transform, anno, buildee, excludes, includes)) return;
         if (includes.size() == 1 && Undefined.isUndefined(includes.get(0))) includes
= null;
         ClassNode builder = createBuilder(anno, buildee);
+        markAsGenerated(buildee, builder);
         createBuilderFactoryMethod(anno, buildee, builder);
 //        List<FieldNode> fields = getFields(transform, anno, buildee);
         boolean allNames = transform.memberHasValue(anno, "allNames", true);
@@ -206,9 +210,9 @@ public class DefaultStrategy extends BuilderASTTransformation.AbstractBuilderStr
             ClassNode correctedType = getCorrectedType(buildee, pi.getType(), builder);
             String fieldName = pi.getName();
             builder.addField(createFieldCopy(buildee, fieldName, correctedType));
-            builder.addMethod(createBuilderMethodForProp(builder, new PropertyInfo(fieldName,
correctedType), getPrefix(anno)));
+            addGeneratedMethod(builder, createBuilderMethodForProp(builder, new PropertyInfo(fieldName,
correctedType), getPrefix(anno)));
         }
-        builder.addMethod(createBuildMethod(anno, buildee, props));
+        addGeneratedMethod(builder, createBuildMethod(anno, buildee, props));
     }
 
     private static ClassNode getCorrectedType(ClassNode buildee, ClassNode fieldType, ClassNode
declaringClass) {
@@ -219,7 +223,7 @@ public class DefaultStrategy extends BuilderASTTransformation.AbstractBuilderStr
 
     private static void createBuilderFactoryMethod(AnnotationNode anno, ClassNode buildee,
ClassNode builder) {
         buildee.getModule().addClass(builder);
-        buildee.addMethod(createBuilderMethod(anno, builder));
+        addGeneratedMethod(buildee, createBuilderMethod(anno, builder));
     }
 
     private static ClassNode createBuilder(AnnotationNode anno, ClassNode buildee) {
diff --git a/src/main/groovy/groovy/transform/builder/ExternalStrategy.java b/src/main/groovy/groovy/transform/builder/ExternalStrategy.java
index fafc4fe..22e1953 100644
--- a/src/main/groovy/groovy/transform/builder/ExternalStrategy.java
+++ b/src/main/groovy/groovy/transform/builder/ExternalStrategy.java
@@ -31,6 +31,8 @@ import org.codehaus.groovy.transform.BuilderASTTransformation;
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
@@ -103,6 +105,7 @@ public class ExternalStrategy extends BuilderASTTransformation.AbstractBuilderSt
             transform.addError("Error during " + MY_TYPE_NAME + " processing: 'forClass'
must be specified for " + getClass().getName(), anno);
             return;
         }
+        markAsGenerated(buildee, builder);
         List<String> excludes = new ArrayList<String>();
         List<String> includes = new ArrayList<String>();
         includes.add(Undefined.STRING);
@@ -121,9 +124,9 @@ public class ExternalStrategy extends BuilderASTTransformation.AbstractBuilderSt
         }
         for (PropertyInfo prop : props) {
             builder.addField(createFieldCopy(builder, prop));
-            builder.addMethod(createBuilderMethodForField(builder, prop, prefix));
+            addGeneratedMethod(builder, createBuilderMethodForField(builder, prop, prefix));
         }
-        builder.addMethod(createBuildMethod(transform, anno, buildee, props));
+        addGeneratedMethod(builder, createBuildMethod(transform, anno, buildee, props));
     }
 
     private static MethodNode createBuildMethod(BuilderASTTransformation transform, AnnotationNode
anno, ClassNode sourceClass, List<PropertyInfo> fields) {
diff --git a/src/main/groovy/groovy/transform/builder/InitializerStrategy.java b/src/main/groovy/groovy/transform/builder/InitializerStrategy.java
index cedd884..8c32051 100644
--- a/src/main/groovy/groovy/transform/builder/InitializerStrategy.java
+++ b/src/main/groovy/groovy/transform/builder/InitializerStrategy.java
@@ -42,6 +42,7 @@ import java.util.List;
 import java.util.Map;
 
 import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
 import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX;
@@ -303,7 +304,7 @@ public class InitializerStrategy extends BuilderASTTransformation.AbstractBuilde
 
     // no rename so delete and add
     private static void renameMethod(ClassNode buildee, MethodNode mNode, String newName)
{
-        buildee.addMethod(newName, mNode.getModifiers(), mNode.getReturnType(), mNode.getParameters(),
mNode.getExceptions(), mNode.getCode());
+        addGeneratedMethod(buildee, newName, mNode.getModifiers(), mNode.getReturnType(),
mNode.getParameters(), mNode.getExceptions(), mNode.getCode());
         buildee.removeMethod(mNode);
     }
 
diff --git a/src/main/groovy/groovy/transform/builder/SimpleStrategy.java b/src/main/groovy/groovy/transform/builder/SimpleStrategy.java
index b01493e..f1857fa 100644
--- a/src/main/groovy/groovy/transform/builder/SimpleStrategy.java
+++ b/src/main/groovy/groovy/transform/builder/SimpleStrategy.java
@@ -31,6 +31,7 @@ import org.objectweb.asm.Opcodes;
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
@@ -113,12 +114,12 @@ public class SimpleStrategy extends BuilderASTTransformation.AbstractBuilderStra
             if (!AbstractASTTransformation.shouldSkipUndefinedAware(fieldName, excludes,
includes, allNames)) {
                 String methodName = getSetterName(prefix, fieldName);
                 Parameter parameter = param(field.getType(), fieldName);
-                buildee.addMethod(methodName, Opcodes.ACC_PUBLIC, newClass(buildee), params(parameter),
NO_EXCEPTIONS, block(
-                                stmt(useSetters && !field.isFinal()
-                                                ? callThisX(getSetterName("set", fieldName),
varX(parameter))
-                                                : assignX(fieldX(field), varX(parameter))
-                                ),
-                                returnS(varX("this")))
+                addGeneratedMethod(buildee, methodName, Opcodes.ACC_PUBLIC, newClass(buildee),
params(parameter), NO_EXCEPTIONS, block(
+                    stmt(useSetters && !field.isFinal()
+                        ? callThisX(getSetterName("set", fieldName), varX(parameter))
+                        : assignX(fieldX(field), varX(parameter))
+                    ),
+                    returnS(varX("this")))
                 );
             }
         }
diff --git a/src/main/java/org/apache/groovy/ast/tools/AnnotatedNodeUtils.java b/src/main/java/org/apache/groovy/ast/tools/AnnotatedNodeUtils.java
index 7d22bfd..9e4b2c8 100644
--- a/src/main/java/org/apache/groovy/ast/tools/AnnotatedNodeUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/AnnotatedNodeUtils.java
@@ -35,11 +35,12 @@ public class AnnotatedNodeUtils {
     private AnnotatedNodeUtils() {
     }
 
-    public static void markAsGenerated(ClassNode containingClass, AnnotatedNode nodeToMark)
{
+    public static <T extends AnnotatedNode> T markAsGenerated(ClassNode containingClass,
T nodeToMark) {
         boolean shouldAnnotate = containingClass.getModule() != null && containingClass.getModule().getContext()
!= null;
         if (shouldAnnotate && !hasAnnotation(nodeToMark, GENERATED_TYPE)) {
             nodeToMark.addAnnotation(new AnnotationNode(GENERATED_TYPE));
         }
+        return nodeToMark;
     }
 
     public static boolean hasAnnotation(AnnotatedNode node, ClassNode annotation) {


Mime
View raw message