groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [groovy] 01/02: Tweak GROOVY-8444: add more checks for modifiers
Date Tue, 22 Oct 2019 02:54:35 GMT
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 497d040cc9e8a970a50f98774ad5ce8370a44230
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Tue Oct 22 09:18:45 2019 +0800

    Tweak GROOVY-8444: add more checks for modifiers
---
 .../transform/stc/EnumTypeCheckingExtension.java   | 12 +++--
 src/test/groovy/bugs/Groovy8444.groovy             | 63 ++++++++++++++++++++++
 2 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/EnumTypeCheckingExtension.java
b/src/main/java/org/codehaus/groovy/transform/stc/EnumTypeCheckingExtension.java
index e7a1b1a..45415c0 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/EnumTypeCheckingExtension.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/EnumTypeCheckingExtension.java
@@ -23,6 +23,8 @@ import org.codehaus.groovy.ast.FieldNode;
 import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.stmt.SwitchStatement;
 
+import java.lang.reflect.Modifier;
+
 import static org.codehaus.groovy.transform.stc.StaticTypesMarker.SWITCH_CONDITION_EXPRESSION_TYPE;
 
 /**
@@ -48,9 +50,13 @@ public class EnumTypeCheckingExtension extends TypeCheckingExtension {
 
         if (type.isEnum()) {
             FieldNode fieldNode = type.redirect().getField(vexp.getName());
-            if (null != fieldNode && type.equals(fieldNode.getType())) {
-                vexp.putNodeMetaData(SWITCH_CONDITION_EXPRESSION_TYPE, type);
-                return true;
+            if (null != fieldNode) {
+                int modifiers = fieldNode.getModifiers();
+                if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
&& Modifier.isFinal(modifiers)
+                        && type.equals(fieldNode.getType())) {
+                    vexp.putNodeMetaData(SWITCH_CONDITION_EXPRESSION_TYPE, type);
+                    return true;
+                }
             }
         }
 
diff --git a/src/test/groovy/bugs/Groovy8444.groovy b/src/test/groovy/bugs/Groovy8444.groovy
index 0d1138a..c1f66bd 100644
--- a/src/test/groovy/bugs/Groovy8444.groovy
+++ b/src/test/groovy/bugs/Groovy8444.groovy
@@ -67,6 +67,69 @@ final class Groovy8444 {
     }
 
     @Test
+    void testAccessingNonEnumConstantInSwitchCase2() {
+        def err = shouldFail '''\
+            enum SomeEnum {
+                A, B
+                
+                SomeEnum C = A
+            }
+            @groovy.transform.CompileStatic
+            def meth(SomeEnum e) {
+                switch (e) {
+                    case C: return 3
+                }
+            }
+            meth(SomeEnum.C)
+        '''
+
+        assert err.message.contains('[Static type checking] - The variable [C] is undeclared')
+        assert err.message.contains('@ line 9, column 26.')
+    }
+
+    @Test
+    void testAccessingNonEnumConstantInSwitchCase3() {
+        def err = shouldFail '''\
+            enum SomeEnum {
+                A, B
+                
+                static SomeEnum C = A
+            }
+            @groovy.transform.CompileStatic
+            def meth(SomeEnum e) {
+                switch (e) {
+                    case C: return 3
+                }
+            }
+            meth(SomeEnum.C)
+        '''
+
+        assert err.message.contains('[Static type checking] - The variable [C] is undeclared')
+        assert err.message.contains('@ line 9, column 26.')
+    }
+
+    @Test
+    void testAccessingNonEnumConstantInSwitchCase4() {
+        def err = shouldFail '''\
+            enum SomeEnum {
+                A, B
+                
+                static final SomeEnum C = A
+            }
+            @groovy.transform.CompileStatic
+            def meth(SomeEnum e) {
+                switch (e) {
+                    case C: return 3
+                }
+            }
+            meth(SomeEnum.C)
+        '''
+
+        assert err.message.contains('[Static type checking] - The variable [C] is undeclared')
+        assert err.message.contains('@ line 9, column 26.')
+    }
+
+    @Test
     void testAccessingEnumConstantInNestedSwitchCase() {
         assertScript '''\
             enum SomeEnum {


Mime
View raw message