You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/01/23 17:14:16 UTC

[groovy] branch GROOVY-8433 created (now c9b1066)

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

emilles pushed a change to branch GROOVY-8433
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at c9b1066  GROOVY-8433: Category transform implies static methods

This branch includes the following new commits:

     new c9b1066  GROOVY-8433: Category transform implies static methods

The 1 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.


[groovy] 01/01: GROOVY-8433: Category transform implies static methods

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-8433
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit c9b10668e6e63d25d2500f00d1d23859119e402b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jan 23 11:14:03 2022 -0600

    GROOVY-8433: Category transform implies static methods
---
 .../codehaus/groovy/classgen/InnerClassVisitor.java  |  5 ++++-
 src/spec/test/metaprogramming/CategoryTest.groovy    | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitor.java b/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitor.java
index 8fd2cd4..3855d95 100644
--- a/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitor.java
@@ -227,7 +227,7 @@ public class InnerClassVisitor extends InnerClassVisitorHelper {
         innerClass.addConstructor(ACC_SYNTHETIC, parameters.toArray(Parameter.EMPTY_ARRAY), ClassNode.EMPTY_ARRAY, block);
     }
 
-    private boolean isStatic(InnerClassNode innerClass, VariableScope scope, ConstructorCallExpression call) {
+    private boolean isStatic(final ClassNode innerClass, final VariableScope scope, final ConstructorCallExpression call) {
         boolean isStatic = innerClass.isStaticClass();
         if (!isStatic) {
             if (currentMethod != null) {
@@ -255,6 +255,9 @@ public class InnerClassVisitor extends InnerClassVisitorHelper {
                 isStatic = currentField.isStatic();
             }
         }
+        // GROOVY-8433: Category transform implies static method
+        isStatic = isStatic || innerClass.getOuterClass().getAnnotations().stream()
+            .anyMatch(a -> a.getClassNode().getName().equals("groovy.lang.Category"));
         return isStatic;
     }
 
diff --git a/src/spec/test/metaprogramming/CategoryTest.groovy b/src/spec/test/metaprogramming/CategoryTest.groovy
index 16bd7b0..1d528c6 100644
--- a/src/spec/test/metaprogramming/CategoryTest.groovy
+++ b/src/spec/test/metaprogramming/CategoryTest.groovy
@@ -56,4 +56,24 @@ class CategoryTest extends GroovyTestCase {
             // end::time_category_anno[]
         '''
     }
+
+    // GROOVY-8433
+    void testCategoryAnnotationAndAIC() {
+        assertScript '''
+            @Category(Number)
+            class NumberCategory {
+                def m() {
+                    String variable = 'works'
+                    new Object() { // "Cannot cast object '1' with class 'java.lang.Integer' to class 'NumberCategory'" due to implicit "this"
+                        String toString() { variable }
+                    }
+                }
+            }
+
+            use (NumberCategory) {
+                String result = 1.m()
+                assert result == 'works'
+            }
+        '''
+    }
 }