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:27 UTC

[groovy] branch master updated (7b5da6a -> e4e10a0)

This is an automated email from the ASF dual-hosted git repository.

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


    from 7b5da6a  minor refactor: remove javadoc warnings (fix typo)
     new 96c3364  GROOVY-9095: Default retention policy not implemented for annotation (also covers GROOVY-9096)
     new 840884f  checkstyle warnings
     new 08cde8b  fix codenarc warnings
     new 3bfe082  minor refactor: formatting and tidy up
     new e4e10a0  GROOVY-9095: Default retention policy not implemented for annotation (add test)

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovy/transform/AnnotationCollectorMode.java  |   1 -
 .../org/codehaus/groovy/ast/AnnotationNode.java    |  59 +++++----
 .../codehaus/groovy/classgen/ExtendedVerifier.java |  33 +++--
 .../codehaus/groovy/control/ResolveVisitor.java    |   4 +-
 src/test/gls/annotations/AnnotationTest.groovy     |   2 +-
 .../gls/annotations/AnnotationsInfoTest.groovy     |  54 ++++++++
 src/test/gls/annotations/AnnotationsTestBase.java  | 136 +++++++++++++++++++++
 .../annotations/HasDefaultClassRetention.java}     |   4 +-
 ...otation.java => HasExplicitClassRetention.java} |   6 +-
 ...avaAnnotation.java => HasRuntimeRetention.java} |   4 +-
 ...JavaAnnotation.java => HasSourceRetention.java} |   6 +-
 src/test/gls/generics/GenericsTestBase.java        |  60 ++++-----
 src/test/groovy/CompileOrderTest.groovy            |   6 +-
 .../groovy/classgen/GenericsGenTest.groovy         |   2 +-
 .../codehaus/groovy/classgen/InterfaceTest.groovy  |   2 +-
 15 files changed, 291 insertions(+), 88 deletions(-)
 create mode 100644 src/test/gls/annotations/AnnotationsInfoTest.groovy
 create mode 100644 src/test/gls/annotations/AnnotationsTestBase.java
 copy src/test/{groovy/bugs/Groovy4104B.java => gls/annotations/HasDefaultClassRetention.java} (91%)
 copy src/test/gls/annotations/{JavaAnnotation.java => HasExplicitClassRetention.java} (89%)
 copy src/test/gls/annotations/{JavaAnnotation.java => HasRuntimeRetention.java} (92%)
 copy src/test/gls/annotations/{JavaAnnotation.java => HasSourceRetention.java} (89%)


[groovy] 01/05: GROOVY-9095: Default retention policy not implemented for annotation (also covers GROOVY-9096)

Posted by pa...@apache.org.
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 96c3364b18613354fc62f75420d54fb2b668b0f0
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Apr 26 22:19:20 2019 +1000

    GROOVY-9095: Default retention policy not implemented for annotation (also covers GROOVY-9096)
---
 .../groovy/transform/AnnotationCollectorMode.java  |  1 -
 .../org/codehaus/groovy/ast/AnnotationNode.java    | 59 ++++++++++++----------
 .../codehaus/groovy/classgen/ExtendedVerifier.java | 33 ++++++++----
 .../codehaus/groovy/control/ResolveVisitor.java    |  4 +-
 src/test/gls/annotations/AnnotationTest.groovy     |  2 +-
 5 files changed, 59 insertions(+), 40 deletions(-)

diff --git a/src/main/groovy/groovy/transform/AnnotationCollectorMode.java b/src/main/groovy/groovy/transform/AnnotationCollectorMode.java
index 308d0c0..75b78bf 100644
--- a/src/main/groovy/groovy/transform/AnnotationCollectorMode.java
+++ b/src/main/groovy/groovy/transform/AnnotationCollectorMode.java
@@ -21,7 +21,6 @@
 package groovy.transform;
 
 public enum AnnotationCollectorMode {
-    // TODO should we support @Repeatable from Java 8?
     /**
      * Annotations from the annotation collection will always be inserted. After all transforms have been run, it will
      * be an error if multiple annotations (excluding those with SOURCE retention) exist.
diff --git a/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java b/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
index c493299..55543da 100644
--- a/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
@@ -26,7 +26,7 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 
 /**
- * Represents an annotation which can be attached to interfaces, classes, methods and fields.
+ * Represents an annotation which can be attached to interfaces, classes, methods, fields, parameters, and other places.
  */
 public class AnnotationNode extends ASTNode {
     public static final int CONSTRUCTOR_TARGET = 1 << 1;
@@ -45,7 +45,7 @@ public class AnnotationNode extends ASTNode {
 
     private final ClassNode classNode;
     private Map<String, Expression> members;
-    private boolean runtimeRetention= false, sourceRetention= false, classRetention = false;
+    private boolean runtimeRetention = false, sourceRetention = false, /* explicit */ classRetention = false;
     private int allowedTargets = ALL_TARGETS;
 
     public AnnotationNode(ClassNode classNode) {
@@ -62,7 +62,7 @@ public class AnnotationNode extends ASTNode {
         }
         return members;
     }
-    
+
     public Expression getMember(String name) {
         if (members == null) {
             return null;
@@ -91,62 +91,67 @@ public class AnnotationNode extends ASTNode {
         assertMembers();
         members.put(name, value);
     }
-    
+
     public boolean isBuiltIn(){
         return false;
     }
 
     /**
-     * Flag corresponding to <code>RetentionPolicy</code>.
-     * @return <tt>true</tt> if the annotation should be visible at runtime, 
-     *      <tt>false</tt> otherwise
+     * Flag corresponding to <code>RetentionPolicy.RUNTIME</code>.
+     * @return <tt>true</tt> if the annotation should be visible at runtime,
+     *         <tt>false</tt> otherwise
      */
     public boolean hasRuntimeRetention() {
         return this.runtimeRetention;
     }
 
     /**
-     * Sets the internal flag of this annotation runtime retention policy.
-     * If the current annotation has 
-     * <code>RetentionPolicy.RUNTIME</code> or if <tt>false</tt>
-     * if the <code>RetentionPolicy.CLASS</code>.
+     * Sets the internal flag if the current annotation has
+     * <code>RetentionPolicy.SOURCE</code>.
+     *
      * @param flag if <tt>true</tt> then current annotation is marked as having
-     *     <code>RetentionPolicy.RUNTIME</code>. If <tt>false</tt> then
-     *     the annotation has <code>RetentionPolicy.CLASS</code>.
+     *     <code>RetentionPolicy.RUNTIME</code>.
      */
     public void setRuntimeRetention(boolean flag) {
         this.runtimeRetention = flag;
     }
-    
+
     /**
      * Flag corresponding to <code>RetentionPolicy.SOURCE</code>.
-     * @return <tt>true</tt> if the annotation is only allowed in sources 
-     *      <tt>false</tt> otherwise
+     * @return <tt>true</tt> if the annotation is only allowed in sources
+     *         <tt>false</tt> otherwise
      */
     public boolean hasSourceRetention() {
-        if (!runtimeRetention && !classRetention) return true;
         return this.sourceRetention;
     }
 
-    /** Sets the internal flag if the current annotation has 
-     * <code>RetentionPolicy.SOURCE</code>.
-     */ 
+    /**
+     * Sets the internal flag if the current annotation has <code>RetentionPolicy.SOURCE</code>.
+     *
+     * @param flag if <tt>true</tt> then current annotation is marked as having
+     *     <code>RetentionPolicy.SOURCE</code>.
+     */
     public void setSourceRetention(boolean flag) {
         this.sourceRetention = flag;
     }
 
     /**
      * Flag corresponding to <code>RetentionPolicy.CLASS</code>.
-     * @return <tt>true</tt> if the annotation is recorded by the compiler,
-     *                       but not visible at runtime     *
-      *        <tt>false</tt> otherwise
+     * This is the default when no <code>RetentionPolicy</code> annotations are present.
+     *
+     * @return <tt>true</tt> if the annotation is written in the bytecode, but not visible at runtime
+     *         <tt>false</tt> otherwise
      */
     public boolean hasClassRetention() {
+        if (!runtimeRetention && !sourceRetention) return true;
         return this.classRetention;
     }
 
-    /** Sets the internal flag if the current annotation has
-     * <code>RetentionPolicy.CLASS</code>.
+    /**
+     * Sets the internal flag if the current annotation has an explicit <code>RetentionPolicy.CLASS</code>.
+     *
+     * @param flag if <tt>true</tt> then current annotation is marked as having
+     *     <code>RetentionPolicy.CLASS</code>.
      */
     public void setClassRetention(boolean flag) {
         this.classRetention = flag;
@@ -155,11 +160,11 @@ public class AnnotationNode extends ASTNode {
     public void setAllowedTargets(int bitmap) {
         this.allowedTargets = bitmap;
     }
-    
+
     public boolean isTargetAllowed(int target) {
         return (this.allowedTargets & target) == target;
     }
-    
+
     public static String targetToName(int target) {
         switch(target) {
             case TYPE_TARGET:
diff --git a/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java b/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
index 96849ac..58a5bc3 100644
--- a/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
@@ -47,6 +47,8 @@ import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
 import org.codehaus.groovy.syntax.SyntaxException;
 import org.objectweb.asm.Opcodes;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -152,17 +154,17 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
             addError("Annotations are not supported in the current runtime. " + JVM_ERROR_MESSAGE, node);
             return;
         }
-        Map<String, List<AnnotationNode>> runtimeAnnotations = new LinkedHashMap<String, List<AnnotationNode>>();
+        Map<String, List<AnnotationNode>> nonSourceAnnotations = new LinkedHashMap<String, List<AnnotationNode>>();
         for (AnnotationNode unvisited : node.getAnnotations()) {
             AnnotationNode visited = visitAnnotation(unvisited);
             String name = visited.getClassNode().getName();
-            if (visited.hasRuntimeRetention()) {
-                List<AnnotationNode> seen = runtimeAnnotations.get(name);
+            if (!visited.hasSourceRetention()) {
+                List<AnnotationNode> seen = nonSourceAnnotations.get(name);
                 if (seen == null) {
                     seen = new ArrayList<AnnotationNode>();
                 }
                 seen.add(visited);
-                runtimeAnnotations.put(name, seen);
+                nonSourceAnnotations.put(name, seen);
             }
             boolean isTargetAnnotation = name.equals("java.lang.annotation.Target");
 
@@ -175,7 +177,7 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
             visitDeprecation(node, visited);
             visitOverride(node, visited);
         }
-        checkForDuplicateAnnotations(node, runtimeAnnotations);
+        checkForDuplicateAnnotations(node, nonSourceAnnotations);
     }
 
     private void checkForDuplicateAnnotations(AnnotatedNode node, Map<String, List<AnnotationNode>> runtimeAnnotations) {
@@ -199,7 +201,20 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
                 }
                 if (repeatable != null) {
                     AnnotationNode collector = new AnnotationNode(repeatable);
-                    collector.setRuntimeRetention(true); // checked earlier
+                    if (repeatable.isResolved()) {
+                        Class repeatableType = repeatable.getTypeClass();
+                        Retention retAnn = (Retention) repeatableType.getAnnotation(Retention.class);
+                        collector.setRuntimeRetention(retAnn != null && retAnn.value().equals(RetentionPolicy.RUNTIME));
+                    } else if (repeatable.redirect() != null) {
+                        for (AnnotationNode annotationNode : repeatable.redirect().getAnnotations()) {
+                            if (!annotationNode.getClassNode().getName().equals("java.lang.annotation.Retention"))
+                                continue;
+                            String value = annotationNode.getMember("value").getText();
+                            collector.setRuntimeRetention(value.equals(RetentionPolicy.RUNTIME.name()) ||
+                                    value.equals(RetentionPolicy.class.getName() + "." + RetentionPolicy.RUNTIME.name()));
+                        }
+                    }
+
                     List<Expression> annos = new ArrayList<Expression>();
                     for (AnnotationNode an : next.getValue()) {
                         annos.add(new AnnotationConstantExpression(an));
@@ -281,7 +296,7 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
             }
             ClassNode superClass = next.getUnresolvedSuperClass();
             if (superClass != null) {
-                next =  correctToGenericsSpecRecurse(updatedGenericsSpec, superClass);
+                next = correctToGenericsSpecRecurse(updatedGenericsSpec, superClass);
             } else {
                 next = null;
             }
@@ -290,7 +305,7 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
     }
 
     private static MethodNode getDeclaredMethodCorrected(Map genericsSpec, MethodNode mn, ClassNode correctedNext) {
-        for (MethodNode orig :  correctedNext.getDeclaredMethods(mn.getName())) {
+        for (MethodNode orig : correctedNext.getDeclaredMethods(mn.getName())) {
             MethodNode method = correctToGenericsSpec(genericsSpec, orig);
             if (ParameterUtils.parametersEqual(method.getParameters(), mn.getParameters())) {
                 return method;
@@ -338,4 +353,4 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
     public void visitGenericType(GenericsType genericsType) {
 
     }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index aeca559..69a0a9c 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -1364,8 +1364,8 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
             if (annType.isResolved()) {
                 Class annTypeClass = annType.getTypeClass();
                 Retention retAnn = (Retention) annTypeClass.getAnnotation(Retention.class);
-                if (retAnn != null && retAnn.value().equals(RetentionPolicy.RUNTIME) && !isRepeatable(annTypeClass)) {
-                    // remember runtime/non-repeatable annos (auto collecting of Repeatable annotations is handled elsewhere)
+                if (retAnn != null && !retAnn.value().equals(RetentionPolicy.SOURCE) && !isRepeatable(annTypeClass)) {
+                    // remember non-source/non-repeatable annos (auto collecting of Repeatable annotations is handled elsewhere)
                     AnnotationNode anyPrevAnnNode = tmpAnnotations.put(annTypeClass.getName(), an);
                     if (anyPrevAnnNode != null) {
                         addError("Cannot specify duplicate annotation on the same member : " + annType.getName(), an);
diff --git a/src/test/gls/annotations/AnnotationTest.groovy b/src/test/gls/annotations/AnnotationTest.groovy
index 818e337..01a4c50 100644
--- a/src/test/gls/annotations/AnnotationTest.groovy
+++ b/src/test/gls/annotations/AnnotationTest.groovy
@@ -278,7 +278,7 @@ class AnnotationTest extends CompilableTestSupport {
         assertScript """
             import java.lang.annotation.*
 
-            // a random annnotation type
+            // a random annotation type
             @Retention(RetentionPolicy.RUNTIME)
             @interface MyAnnotation {
                 String stringValue()


[groovy] 02/05: checkstyle warnings

Posted by pa...@apache.org.
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 840884f3483cb15738c3ccd581be0f003f2e3988
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Apr 27 23:51:58 2019 +1000

    checkstyle warnings
---
 src/test/groovy/CompileOrderTest.groovy | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/test/groovy/CompileOrderTest.groovy b/src/test/groovy/CompileOrderTest.groovy
index 8a89f9d..2fa008c 100644
--- a/src/test/groovy/CompileOrderTest.groovy
+++ b/src/test/groovy/CompileOrderTest.groovy
@@ -19,10 +19,10 @@
 package groovy
 
 class CompileOrderTest extends GroovyTestCase {
-   public void testCompileOrder() {
+   void testCompileOrder() {
       def interfaceFile = File.createTempFile("TestOrderInterface", ".groovy", new File("target"))
       def concreteFile = File.createTempFile("TestOrderConcrete", ".groovy", new File("target"))
-      def cl = new GroovyClassLoader(this.class.classLoader);
+      def cl = new GroovyClassLoader(this.class.classLoader)
       def currentDir = concreteFile.parentFile.absolutePath
       cl.addClasspath(currentDir)
       cl.shouldRecompile = true
@@ -48,7 +48,7 @@ class CompileOrderTest extends GroovyTestCase {
       }
    }
 
-    public void testCompileFileURI() {
+    void testCompileFileURI() {
         def interfaceFile = File.createTempFile("TestURLOrderInterface", ".groovy", new File("target"))
         def concreteFile = File.createTempFile("TestURLOrderConcrete", ".groovy", new File("target"))
 


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

Posted by pa...@apache.org.
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;


[groovy] 04/05: minor refactor: formatting and tidy up

Posted by pa...@apache.org.
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 3bfe08232e55aee575a68fc1f4865b0112153b47
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Apr 28 09:32:08 2019 +1000

    minor refactor: formatting and tidy up
---
 src/test/gls/generics/GenericsTestBase.java | 61 ++++++++++++++---------------
 1 file changed, 30 insertions(+), 31 deletions(-)

diff --git a/src/test/gls/generics/GenericsTestBase.java b/src/test/gls/generics/GenericsTestBase.java
index 779c77b..820a330 100644
--- a/src/test/gls/generics/GenericsTestBase.java
+++ b/src/test/gls/generics/GenericsTestBase.java
@@ -26,77 +26,76 @@ 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;
 
 public abstract class GenericsTestBase extends GroovyTestCase {
     MyLoader loader;
-    HashMap signatures;
-    
-    private class MyLoader extends GroovyClassLoader{
-        public MyLoader(ClassLoader classLoader) {
+    HashMap<String, String> signatures;
+
+    private class MyLoader extends GroovyClassLoader {
+        MyLoader(ClassLoader classLoader) {
             super(classLoader);
         }
 
-        protected ClassCollector createCollector(CompilationUnit unit,SourceUnit su) {
+        protected ClassCollector createCollector(CompilationUnit unit, SourceUnit su) {
             return new MyCollector(new InnerLoader(this), unit, su);
         }
     }
-    private class MyCollector extends GroovyClassLoader.ClassCollector{
 
-        public MyCollector(InnerLoader myLoader, CompilationUnit unit, SourceUnit su) {
-           super(myLoader,unit,su);
+    private class MyCollector extends GroovyClassLoader.ClassCollector {
+        MyCollector(InnerLoader myLoader, CompilationUnit unit, SourceUnit su) {
+            super(myLoader, unit, su);
         }
+
         protected Class createClass(byte[] code, ClassNode classNode) {
             ClassReader cr = new ClassReader(code);
             GenericsTester classVisitor = new GenericsTester(new org.objectweb.asm.tree.ClassNode());
             cr.accept(classVisitor, ClassWriter.COMPUTE_MAXS);
-            return super.createClass(code,classNode);
-        }        
+            return super.createClass(code, classNode);
+        }
     }
+
     private class GenericsTester extends ClassVisitor {
-        public GenericsTester(ClassVisitor cv) {
+        GenericsTester(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);
+
+        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
+            if (signature != null) signatures.put("class", signature);
         }
-        public FieldVisitor visitField(int access, String name, String desc,
-                String signature, Object value) {
-            if (signature!=null) signatures.put(name,signature);
+
+        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);
         }
-        public MethodVisitor visitMethod(int access, String name, String desc,
-                String signature, String[] exceptions) {
-            if (signature!=null) signatures.put(name+desc,signature);
+
+        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);
         }
-
     }
-    
-    public void setUp(){
+
+    public void setUp() {
         loader = new MyLoader(this.getClass().getClassLoader());
-        signatures = new HashMap();
+        signatures = new HashMap<>();
     }
-    
+
     public void createClassInfo(String script) {
         loader.parseClass(script);
     }
-    
-    public Map getSignatures() {
+
+    public Map<String, String> getSignatures() {
         return signatures;
     }
-    
+
     protected void shouldNotCompile(String script) {
         try {
             loader.parseClass(script);
         } catch (CompilationFailedException cfe) {
             return;
         }
-        throw new AssertionError("compilation of script '"+script+"' should have failed, but did not.");
+        throw new AssertionError("compilation of script '" + script + "' should have failed, but did not.");
     }
 }
-


[groovy] 03/05: fix codenarc warnings

Posted by pa...@apache.org.
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 08cde8bef612639125a0841c295c8c2a358c32bc
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Apr 28 00:22:38 2019 +1000

    fix codenarc warnings
---
 src/test/org/codehaus/groovy/classgen/GenericsGenTest.groovy | 2 +-
 src/test/org/codehaus/groovy/classgen/InterfaceTest.groovy   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/test/org/codehaus/groovy/classgen/GenericsGenTest.groovy b/src/test/org/codehaus/groovy/classgen/GenericsGenTest.groovy
index f3d39f9..35f4ef7 100644
--- a/src/test/org/codehaus/groovy/classgen/GenericsGenTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/GenericsGenTest.groovy
@@ -88,6 +88,6 @@ class GenericsGenTest extends GroovyTestCase {
         File tempFile = File.createTempDir(prefix, suffix)
         tempFile.deleteOnExit()
         filesToDelete.add(tempFile)
-        return tempFile;
+        return tempFile
     }
 }
diff --git a/src/test/org/codehaus/groovy/classgen/InterfaceTest.groovy b/src/test/org/codehaus/groovy/classgen/InterfaceTest.groovy
index f267287..f57a71b 100644
--- a/src/test/org/codehaus/groovy/classgen/InterfaceTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/InterfaceTest.groovy
@@ -82,6 +82,6 @@ class InterfaceTest extends GroovyTestCase {
         File tempFile = File.createTempDir(prefix, suffix);
         tempFile.deleteOnExit()
         filesToDelete.add(tempFile)
-        return tempFile;
+        return tempFile
     }
 }