groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [groovy] 05/07: GROOVY-9052: Missing @Generated annotation on @Vetoable
Date Fri, 22 Mar 2019 01:41:30 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit f8e382b97cf6f8e437d660b46e34b578dcf60969
Author: Paul King <paulk@asert.com.au>
AuthorDate: Fri Mar 22 10:15:24 2019 +1000

    GROOVY-9052: Missing @Generated annotation on @Vetoable
---
 .../groovy/beans/VetoableASTTransformation.java    | 25 ++++++++++---------
 ...bleTest.groovy => VetoableTransformTest.groovy} | 29 ++++++++++++++++++++--
 2 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/src/main/groovy/groovy/beans/VetoableASTTransformation.java b/src/main/groovy/groovy/beans/VetoableASTTransformation.java
index 44e1da7..14eb7ac 100644
--- a/src/main/groovy/groovy/beans/VetoableASTTransformation.java
+++ b/src/main/groovy/groovy/beans/VetoableASTTransformation.java
@@ -35,7 +35,6 @@ import org.codehaus.groovy.control.CompilePhase;
 import org.codehaus.groovy.control.SourceUnit;
 import org.codehaus.groovy.control.messages.SimpleMessage;
 import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
-import org.codehaus.groovy.runtime.MetaClassHelper;
 import org.codehaus.groovy.syntax.SyntaxException;
 import org.codehaus.groovy.transform.GroovyASTTransformation;
 import org.objectweb.asm.Opcodes;
@@ -44,6 +43,7 @@ import java.beans.PropertyVetoException;
 import java.beans.VetoableChangeListener;
 import java.beans.VetoableChangeSupport;
 
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
@@ -58,6 +58,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
+import static org.codehaus.groovy.runtime.MetaClassHelper.capitalize;
 
 /**
  * Handles generation of code for the {@code @Vetoable} annotation, and {@code @Bindable}
@@ -166,8 +167,8 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
      * Wrap an existing setter.
      */
     private static void wrapSetterMethod(ClassNode classNode, boolean bindable, String propertyName)
{
-        String getterName = "get" + MetaClassHelper.capitalize(propertyName);
-        MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
+        String getterName = "get" + capitalize(propertyName);
+        MethodNode setter = classNode.getSetterMethod("set" + capitalize(propertyName));
 
         if (setter != null) {
             // Get the existing code block
@@ -208,7 +209,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
         if (needsVetoableChangeSupport(declaringClass, source)) {
             addVetoableChangeSupport(declaringClass);
         }
-        String setterName = "set" + MetaClassHelper.capitalize(propertyNode.getName());
+        String setterName = "set" + capitalize(propertyNode.getName());
         if (declaringClass.getMethods(setterName).isEmpty()) {
             Expression fieldExpression = fieldX(propertyNode.getField());
             BlockStatement setterBlock = new BlockStatement();
@@ -318,7 +319,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
                 setterBlock);
         setter.setSynthetic(true);
         // add it to the class
-        declaringClass.addMethod(setter);
+        addGeneratedMethod(declaringClass, setter);
     }
 
     /**
@@ -352,7 +353,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
         // void addVetoableChangeListener(listener) {
         //     this$vetoableChangeSupport.addVetoableChangeListener(listener)
         //  }
-        declaringClass.addMethod(
+        addGeneratedMethod(declaringClass,
                 new MethodNode(
                         "addVetoableChangeListener",
                         ACC_PUBLIC,
@@ -365,7 +366,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
         // void addVetoableChangeListener(name, listener) {
         //     this$vetoableChangeSupport.addVetoableChangeListener(name, listener)
         //  }
-        declaringClass.addMethod(
+        addGeneratedMethod(declaringClass,
                 new MethodNode(
                         "addVetoableChangeListener",
                         ACC_PUBLIC,
@@ -378,7 +379,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
         // boolean removeVetoableChangeListener(listener) {
         //    return this$vetoableChangeSupport.removeVetoableChangeListener(listener);
         // }
-        declaringClass.addMethod(
+        addGeneratedMethod(declaringClass,
                 new MethodNode(
                         "removeVetoableChangeListener",
                         ACC_PUBLIC,
@@ -388,7 +389,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
                         stmt(callX(fieldX(vcsField), "removeVetoableChangeListener", args(varX("listener",
vclClassNode))))));
 
         // add method: void removeVetoableChangeListener(name, listener)
-        declaringClass.addMethod(
+        addGeneratedMethod(declaringClass,
                 new MethodNode(
                         "removeVetoableChangeListener",
                         ACC_PUBLIC,
@@ -403,7 +404,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
         // {
         //     this$vetoableChangeSupport.fireVetoableChange(name, oldValue, newValue)
         //  }
-        declaringClass.addMethod(
+        addGeneratedMethod(declaringClass,
                 new MethodNode(
                         "fireVetoableChange",
                         ACC_PUBLIC,
@@ -416,7 +417,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
         // VetoableChangeListener[] getVetoableChangeListeners() {
         //   return this$vetoableChangeSupport.getVetoableChangeListeners
         // }
-        declaringClass.addMethod(
+        addGeneratedMethod(declaringClass,
                 new MethodNode(
                         "getVetoableChangeListeners",
                         ACC_PUBLIC,
@@ -429,7 +430,7 @@ public class VetoableASTTransformation extends BindableASTTransformation
{
         // VetoableChangeListener[] getVetoableChangeListeners(String name) {
         //   return this$vetoableChangeSupport.getVetoableChangeListeners(name)
         // }
-        declaringClass.addMethod(
+        addGeneratedMethod(declaringClass,
                 new MethodNode(
                         "getVetoableChangeListeners",
                         ACC_PUBLIC,
diff --git a/src/test/groovy/beans/VetoableTest.groovy b/src/test/groovy/beans/VetoableTransformTest.groovy
similarity index 93%
rename from src/test/groovy/beans/VetoableTest.groovy
rename to src/test/groovy/beans/VetoableTransformTest.groovy
index 2a8519a..3fce996 100644
--- a/src/test/groovy/beans/VetoableTest.groovy
+++ b/src/test/groovy/beans/VetoableTransformTest.groovy
@@ -20,7 +20,7 @@ package groovy.beans
 
 import org.codehaus.groovy.control.CompilationFailedException
 
-class VetoableTest extends GroovyTestCase {
+class VetoableTransformTest extends GroovyShellTestCase {
 
     void testSimpleConstrainedProperty() {
         GroovyShell shell = new GroovyShell()
@@ -456,4 +456,29 @@ class VetoableTest extends GroovyTestCase {
             assert new MyBean()
         """
     }
-}
\ No newline at end of file
+
+    void testVetoableGeneratedMethodsAreAnnotatedWithGenerated_GROOVY9052() {
+        def person = evaluate('''
+            @groovy.beans.Vetoable
+            class Person {
+                String firstName
+
+                void setFirstName(String fn) {
+                    this.firstName = fn.toUpperCase()
+                }
+
+                def zipCode
+             }
+             new Person()
+        ''')
+
+        person.class.declaredMethods.each { m ->
+            if (m.name.contains('VetoableChange') || m.name in ['setZipCode']) {
+                assert m.annotations*.annotationType().name.contains('groovy.transform.Generated')
+            } else if (m.name in ['setFirstName']) {
+                // wrapped methods should not be marked since they contain non-generated
logic
+                assert !m.annotations*.annotationType().name.contains('groovy.transform.Generated')
+            }
+        }
+    }
+}


Mime
View raw message