You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/04/28 06:03:32 UTC

[groovy] 05/05: GROOVY-9095: Default retention policy not implemented for annotation (add test)

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 e4e10a044db92d0558ebd9c0b9d69d071062a5ec
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Apr 28 16:02:12 2019 +1000

    GROOVY-9095: Default retention policy not implemented for annotation (add test)
---
 .../gls/annotations/AnnotationsInfoTest.groovy     | 54 +++++++++++++++++
 .../AnnotationsTestBase.java}                      | 67 ++++++++++++++++------
 .../gls/annotations/HasDefaultClassRetention.java  | 21 +++++++
 .../gls/annotations/HasExplicitClassRetention.java | 25 ++++++++
 src/test/gls/annotations/HasRuntimeRetention.java  | 25 ++++++++
 src/test/gls/annotations/HasSourceRetention.java   | 25 ++++++++
 src/test/gls/generics/GenericsTestBase.java        |  1 +
 7 files changed, 202 insertions(+), 16 deletions(-)

diff --git a/src/test/gls/annotations/AnnotationsInfoTest.groovy b/src/test/gls/annotations/AnnotationsInfoTest.groovy
new file mode 100644
index 0000000..f78023d
--- /dev/null
+++ b/src/test/gls/annotations/AnnotationsInfoTest.groovy
@@ -0,0 +1,54 @@
+/*
+ *  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 gls.annotations
+
+class AnnotationsInfoTest extends AnnotationsTestBase {
+
+    void testClassWithoutParameterExtendsClassWithFixedParameter() {
+        createClassInfo """
+            import gls.annotations.*
+
+            @HasRuntimeRetention
+            class B extends ArrayList<Long> {
+                @HasSourceRetention
+                def fieldS
+                
+                @HasRuntimeRetention
+                def fieldR
+                
+                @HasDefaultClassRetention
+                def methodDC() {}
+                
+                @HasExplicitClassRetention
+                def methodEC() {}
+            }
+        """
+        assert !annotations.any{ it.contains('SourceRetention') }
+        annotations.findAll { it.contains('RuntimeRetention') }.with { annos ->
+            assert annos.size() == 2
+            assert annos.every{ it.contains('visible=true') }
+            assert annos.every{ it.contains('class B') || it.contains('field B#fieldR') }
+        }
+        annotations.findAll { it.contains('ClassRetention') }.with { annos ->
+            assert annos.size() == 2
+            assert annos.every{ it.contains('visible=false') }
+            assert annos.every{ it.contains('method B#methodDC') || it.contains('method B#methodEC') }
+        }
+    }
+}
diff --git a/src/test/gls/generics/GenericsTestBase.java b/src/test/gls/annotations/AnnotationsTestBase.java
similarity index 58%
copy from src/test/gls/generics/GenericsTestBase.java
copy to src/test/gls/annotations/AnnotationsTestBase.java
index 820a330..1fb6d43 100644
--- a/src/test/gls/generics/GenericsTestBase.java
+++ b/src/test/gls/annotations/AnnotationsTestBase.java
@@ -16,7 +16,7 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-package gls.generics;
+package gls.annotations;
 
 import groovy.lang.GroovyClassLoader;
 import groovy.lang.GroovyClassLoader.InnerLoader;
@@ -26,13 +26,15 @@ import org.codehaus.groovy.control.CompilationFailedException;
 import org.codehaus.groovy.control.CompilationUnit;
 import org.codehaus.groovy.control.CompilerConfiguration;
 import org.codehaus.groovy.control.SourceUnit;
+import org.objectweb.asm.*;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.List;
 
-public abstract class GenericsTestBase extends GroovyTestCase {
+public abstract class AnnotationsTestBase extends GroovyTestCase {
     MyLoader loader;
-    HashMap<String, String> signatures;
+    List<String> annotations;
+    String current = "";
 
     private class MyLoader extends GroovyClassLoader {
         MyLoader(ClassLoader classLoader) {
@@ -51,43 +53,76 @@ public abstract class GenericsTestBase extends GroovyTestCase {
 
         protected Class createClass(byte[] code, ClassNode classNode) {
             ClassReader cr = new ClassReader(code);
-            GenericsTester classVisitor = new GenericsTester(new org.objectweb.asm.tree.ClassNode());
+            AnnotationsTester classVisitor = new AnnotationsTester(new org.objectweb.asm.tree.ClassNode());
             cr.accept(classVisitor, ClassWriter.COMPUTE_MAXS);
             return super.createClass(code, classNode);
         }
     }
 
-    private class GenericsTester extends ClassVisitor {
-        GenericsTester(ClassVisitor cv) {
+    private class FieldAnnotationScanner extends FieldVisitor {
+        private final String field;
+
+        FieldAnnotationScanner(String field) {
+            super(CompilerConfiguration.ASM_API_VERSION);
+            this.field = field;
+        }
+
+        @Override
+        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
+            annotations.add("visiting field " + field + ", found annotation: desc=" + desc + ", visible=" + visible);
+            return super.visitAnnotation(desc, visible);
+        }
+    }
+
+    private class MethodAnnotationScanner extends MethodVisitor {
+        private final String method;
+
+        MethodAnnotationScanner(String method) {
+            super(CompilerConfiguration.ASM_API_VERSION);
+            this.method = method;
+        }
+
+        @Override
+        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
+            annotations.add("visiting method " + method + ", found annotation: desc=" + desc + ", visible=" + visible);
+            return super.visitAnnotation(desc, visible);
+        }
+    }
+
+    private class AnnotationsTester extends ClassVisitor {
+        AnnotationsTester(ClassVisitor cv) {
             super(CompilerConfiguration.ASM_API_VERSION, cv);
         }
 
         public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
-            if (signature != null) signatures.put("class", signature);
+            current = name;
+        }
+
+        public AnnotationVisitor visitAnnotation(String desc, boolean visible){
+            annotations.add("visiting class " + current + " found annotation: desc=" + desc + ", visible=" + visible);
+            return super.visitAnnotation(desc,visible);
         }
 
         public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
-            if (signature != null) signatures.put(name, signature);
-            return super.visitField(access, name, desc, signature, value);
+            return new FieldAnnotationScanner(current + "#" + name);
         }
 
         public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
-            if (signature != null) signatures.put(name + desc, signature);
-            return super.visitMethod(access, name, desc, signature, exceptions);
+            return new MethodAnnotationScanner(current + "#" + name);
         }
     }
 
     public void setUp() {
         loader = new MyLoader(this.getClass().getClassLoader());
-        signatures = new HashMap<>();
+        annotations = new ArrayList<>();
     }
 
     public void createClassInfo(String script) {
         loader.parseClass(script);
     }
 
-    public Map<String, String> getSignatures() {
-        return signatures;
+    public List<String> getAnnotations() {
+        return annotations;
     }
 
     protected void shouldNotCompile(String script) {
diff --git a/src/test/gls/annotations/HasDefaultClassRetention.java b/src/test/gls/annotations/HasDefaultClassRetention.java
new file mode 100644
index 0000000..4ff6eee
--- /dev/null
+++ b/src/test/gls/annotations/HasDefaultClassRetention.java
@@ -0,0 +1,21 @@
+/*
+ *  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 gls.annotations;
+
+public @interface HasDefaultClassRetention { }
diff --git a/src/test/gls/annotations/HasExplicitClassRetention.java b/src/test/gls/annotations/HasExplicitClassRetention.java
new file mode 100644
index 0000000..87b1802
--- /dev/null
+++ b/src/test/gls/annotations/HasExplicitClassRetention.java
@@ -0,0 +1,25 @@
+/*
+ *  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 gls.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.CLASS)
+public @interface HasExplicitClassRetention { }
diff --git a/src/test/gls/annotations/HasRuntimeRetention.java b/src/test/gls/annotations/HasRuntimeRetention.java
new file mode 100644
index 0000000..6c0498a
--- /dev/null
+++ b/src/test/gls/annotations/HasRuntimeRetention.java
@@ -0,0 +1,25 @@
+/*
+ *  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 gls.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface HasRuntimeRetention { }
diff --git a/src/test/gls/annotations/HasSourceRetention.java b/src/test/gls/annotations/HasSourceRetention.java
new file mode 100644
index 0000000..8709eed
--- /dev/null
+++ b/src/test/gls/annotations/HasSourceRetention.java
@@ -0,0 +1,25 @@
+/*
+ *  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 gls.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.SOURCE)
+public @interface HasSourceRetention { }
diff --git a/src/test/gls/generics/GenericsTestBase.java b/src/test/gls/generics/GenericsTestBase.java
index 820a330..5b91306 100644
--- a/src/test/gls/generics/GenericsTestBase.java
+++ b/src/test/gls/generics/GenericsTestBase.java
@@ -26,6 +26,7 @@ import org.codehaus.groovy.control.CompilationFailedException;
 import org.codehaus.groovy.control.CompilationUnit;
 import org.codehaus.groovy.control.CompilerConfiguration;
 import org.codehaus.groovy.control.SourceUnit;
+import org.objectweb.asm.*;
 
 import java.util.HashMap;
 import java.util.Map;