groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blackd...@apache.org
Subject [4/4] groovy git commit: GROOVY-8352: add a @Generated annotation
Date Mon, 16 Oct 2017 14:21:33 GMT
GROOVY-8352: add a @Generated annotation


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/d6319eb4
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d6319eb4
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d6319eb4

Branch: refs/heads/GROOVY_2_5_X
Commit: d6319eb4b341e34f0ac6acc0c555aacb377515d5
Parents: 49cc205
Author: aalmiray <aalmiray@gmail.com>
Authored: Wed Oct 11 16:34:27 2017 +0200
Committer: Jochen Theodorou <blackdrag@gmx.org>
Committed: Mon Oct 16 16:21:19 2017 +0200

----------------------------------------------------------------------
 src/main/groovy/transform/Generated.java        | 36 +++++++++
 .../org/codehaus/groovy/classgen/Verifier.java  | 22 +++---
 .../groovy/tools/javac/JavaStubGenerator.java   | 10 ++-
 .../transform/GeneratedTransformTest.groovy     | 81 ++++++++++++++++++++
 4 files changed, 136 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/d6319eb4/src/main/groovy/transform/Generated.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/transform/Generated.java b/src/main/groovy/transform/Generated.java
new file mode 100644
index 0000000..f7ea40a
--- /dev/null
+++ b/src/main/groovy/transform/Generated.java
@@ -0,0 +1,36 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.transform;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * The Generated annotation is used to mark members that have been generated.
+ *
+ * @author Andres Almiray
+ * @author Jochen Theodorou
+ * @author Mark Hoffmann
+ */
+@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE, ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Generated {
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/d6319eb4/src/main/org/codehaus/groovy/classgen/Verifier.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/Verifier.java b/src/main/org/codehaus/groovy/classgen/Verifier.java
index b98e784..f1251d7 100644
--- a/src/main/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/org/codehaus/groovy/classgen/Verifier.java
@@ -21,6 +21,7 @@ package org.codehaus.groovy.classgen;
 import groovy.lang.GroovyClassLoader;
 import groovy.lang.GroovyObject;
 import groovy.lang.MetaClass;
+import groovy.transform.Generated;
 import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.ast.*;
 import org.codehaus.groovy.ast.expr.ArgumentListExpression;
@@ -124,6 +125,8 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
             new Parameter(ClassHelper.METACLASS_TYPE, "mc")
     };
 
+    private static final Class GENERATED_ANNOTATION = Generated.class;
+
     private ClassNode classNode;
     private MethodNode methodNode;
 
@@ -384,6 +387,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
     protected void addGroovyObjectInterfaceAndMethods(ClassNode node, final String classInternalName)
{
         if (!node.isDerivedFromGroovyObject()) node.addInterface(ClassHelper.make(GroovyObject.class));
         FieldNode metaClassField = getMetaClassField(node);
+        AnnotationNode generatedAnnotation = new AnnotationNode(ClassHelper.make(GENERATED_ANNOTATION));
 
         if (!node.hasMethod("getMetaClass", Parameter.EMPTY_ARRAY)) {
             metaClassField = setMetaClassFieldIfNotExists(node, metaClassField);
@@ -425,7 +429,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
                             mv.visitInsn(ARETURN);
                         }
                     })
-            );
+            ).addAnnotation(generatedAnnotation);
         }
 
         Parameter[] parameters = new Parameter[]{new Parameter(ClassHelper.METACLASS_TYPE,
"mc")};
@@ -459,7 +463,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
                     ACC_PUBLIC, ClassHelper.VOID_TYPE,
                     SET_METACLASS_PARAMS, ClassNode.EMPTY_ARRAY,
                     setMetaClassCode
-            );
+            ).addAnnotation(generatedAnnotation);
         }
 
         if (!node.hasMethod("invokeMethod", INVOKE_METHOD_PARAMS)) {
@@ -485,7 +489,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
                             mv.visitInsn(ARETURN);
                         }
                     })
-            );
+            ).addAnnotation(generatedAnnotation);
         }
 
         if (!node.hasMethod("getProperty", GET_PROPERTY_PARAMS)) {
@@ -505,7 +509,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
                             mv.visitInsn(ARETURN);
                         }
                     })
-            );
+            ).addAnnotation(generatedAnnotation);
         }
 
         if (!node.hasMethod("setProperty", SET_PROPERTY_PARAMS)) {
@@ -526,7 +530,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
                             mv.visitInsn(RETURN);
                         }
                     })
-            );
+            ).addAnnotation(generatedAnnotation);
         }
     }
 
@@ -535,12 +539,12 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
      * call will either be made to ClassNode.addSyntheticMethod() or ClassNode.addMethod().
If a non-synthetic method
      * is to be added the ACC_SYNTHETIC modifier is removed if it has been accidentally supplied.
      */
-    protected void addMethod(ClassNode node, boolean shouldBeSynthetic, String name, int
modifiers, ClassNode returnType, Parameter[] parameters,
-                             ClassNode[] exceptions, Statement code) {
+    protected MethodNode addMethod(ClassNode node, boolean shouldBeSynthetic, String name,
int modifiers, ClassNode returnType, Parameter[] parameters,
+                                   ClassNode[] exceptions, Statement code) {
         if (shouldBeSynthetic) {
-            node.addSyntheticMethod(name, modifiers, returnType, parameters, exceptions,
code);
+            return node.addSyntheticMethod(name, modifiers, returnType, parameters, exceptions,
code);
         } else {
-            node.addMethod(name, modifiers & ~ACC_SYNTHETIC, returnType, parameters,
exceptions, code);
+            return node.addMethod(name, modifiers & ~ACC_SYNTHETIC, returnType, parameters,
exceptions, code);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/d6319eb4/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index b672793..89d45ae 100644
--- a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -155,8 +155,8 @@ public class JavaStubGenerator {
                     doAddMethod(method);
                 }
                 protected void addReturnIfNeeded(MethodNode node) {}
-                protected void addMethod(ClassNode node, boolean shouldBeSynthetic, String
name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions,
Statement code) {
-                    doAddMethod(new MethodNode(name, modifiers, returnType, parameters, exceptions,
code));
+                protected MethodNode addMethod(ClassNode node, boolean shouldBeSynthetic,
String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions,
Statement code) {
+                    return doAddMethod(new MethodNode(name, modifiers, returnType, parameters,
exceptions, code));
                 }
 
                 protected void addConstructor(Parameter[] newParams, ConstructorNode ctor,
Statement code, ClassNode node) {
@@ -184,13 +184,15 @@ public class JavaStubGenerator {
                     }
                 }
 
-                private void doAddMethod(MethodNode method) {
+                private MethodNode doAddMethod(MethodNode method) {
                     String sig = method.getTypeDescriptor();
 
-                    if (propertyMethodsWithSigs.containsKey(sig)) return;
+                    if (propertyMethodsWithSigs.containsKey(sig)) return method;
 
                     propertyMethods.add(method);
                     propertyMethodsWithSigs.put(sig, method);
+
+                    return method;
                 }
 
                 @Override

http://git-wip-us.apache.org/repos/asf/groovy/blob/d6319eb4/src/test/org/codehaus/groovy/transform/GeneratedTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/GeneratedTransformTest.groovy b/src/test/org/codehaus/groovy/transform/GeneratedTransformTest.groovy
new file mode 100644
index 0000000..34e9f0e
--- /dev/null
+++ b/src/test/org/codehaus/groovy/transform/GeneratedTransformTest.groovy
@@ -0,0 +1,81 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.transform
+
+import org.junit.After
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.TestName
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+import static org.junit.Assume.assumeTrue
+
+/**
+ * Tests for the @Generated annotation.
+ */
+@RunWith(JUnit4)
+class GeneratedTransformTest extends GroovyShellTestCase {
+    @Rule public TestName nameRule = new TestName()
+
+    @Before
+    void setUp() {
+        super.setUp()
+        // check java version requirements
+        def v = System.getProperty("java.specification.version")
+        assert v
+        assumeTrue('Test requires jre8+', nameRule.methodName.endsWith('_vm8').implies(new
BigDecimal(v) >= 1.8))
+    }
+
+    @After
+    void tearDown() {
+        super.tearDown()
+    }
+
+    @Test
+    void testDefaultGroovyMethodsAreAnnotatedWithGenerated() {
+        def person = evaluate('''
+            class Person {
+                String name
+            }
+            new Person()
+        ''')
+
+        GroovyObject.declaredMethods.each { m ->
+            def method = person.class.declaredMethods.find { it.name == m.name }
+            assert method.annotations*.annotationType().name == ['groovy.transform.Generated']
+        }
+    }
+
+    @Test
+    void testOverridenDefaultGroovyMethodsAreNotAnnotatedWithGenerated() {
+        def person = evaluate('''
+            class Person {
+                String name
+                
+                def invokeMethod(String name, args) { }
+            }
+            new Person()
+        ''')
+
+        def method = person.class.declaredMethods.find { it.name == 'invokeMethod' }
+        assert !('groovy.transform.Generated' in method.annotations*.annotationType().name)
+    }
+}
\ No newline at end of file


Mime
View raw message