This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 8f51c62 GROOVY-8433: Category transform implies static methods
8f51c62 is described below
commit 8f51c62ef925b71d24123f41060b3e2889329665
Author: Eric Milles <[email protected]>
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 87b386b..f6aeeb2 100644
--- a/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitor.java
@@ -225,7 +225,7 @@ public class InnerClassVisitor extends
InnerClassVisitorHelper implements Opcode
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) {
@@ -253,6 +253,9 @@ public class InnerClassVisitor extends
InnerClassVisitorHelper implements Opcode
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'
+ }
+ '''
+ }
}