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 2021/02/25 20:28:02 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-6183: unknown static "this.@name" throws NPE

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

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new bb181f4  GROOVY-6183: unknown static "this.@name" throws NPE
bb181f4 is described below

commit bb181f4501b5fedb0c95b143496c2eb34f60988f
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Feb 25 14:27:48 2021 -0600

    GROOVY-6183: unknown static "this.@name" throws NPE
---
 .../groovy/classgen/AsmClassGenerator.java         |  5 +-
 src/test/groovy/bugs/Groovy4418Bug.groovy          | 69 ++++++++++++----------
 2 files changed, 39 insertions(+), 35 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index 6515714..9f8b59e 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -1054,9 +1054,8 @@ public class AsmClassGenerator extends ClassGenerator {
                         field = classNode.getDeclaredField(name);
                         if (field == null && controller.isStaticContext()
                                 && expression instanceof AttributeExpression) {
-                            // GROOVY-6183: checks supers
-                            field = classNode.getField(name);
-                            if (!field.isPublic() && !field.isProtected()) {
+                            field = classNode.getField(name); // GROOVY-6183: check supers
+                            if (field != null && !field.isPublic() && !field.isProtected()) {
                                 field = null;
                             }
                         }
diff --git a/src/test/groovy/bugs/Groovy4418Bug.groovy b/src/test/groovy/bugs/Groovy4418Bug.groovy
index 33fe40b..cf3b7f5 100644
--- a/src/test/groovy/bugs/Groovy4418Bug.groovy
+++ b/src/test/groovy/bugs/Groovy4418Bug.groovy
@@ -21,7 +21,7 @@ package groovy.bugs
 import org.junit.Test
 
 import static groovy.test.GroovyAssert.assertScript
-import static groovy.util.GroovyAssert.shouldFail
+import static groovy.test.GroovyAssert.shouldFail
 
 final class Groovy4418Bug {
 
@@ -53,6 +53,15 @@ final class Groovy4418Bug {
             assert A.x == 2
             assert A.@x == 2
         '''
+
+        // TODO: MissingFieldException
+        shouldFail IncompatibleClassChangeError, '''
+            class A {
+                static main(args) {
+                    this.@x // NullPointerException
+                }
+            }
+        '''
     }
 
     @Test // GROOVY-8385
@@ -155,41 +164,37 @@ final class Groovy4418Bug {
 
     @Test // GROOVY-8385
     void testParentClassPrivateStaticAttributeSetAccessShouldCallSetter() {
-        shouldFail(MissingFieldException) {
-            Eval.me'''
-                class A {
-                    static private p
-    
-                    void setP(def val) { p = 2 }
-    
-                    def getP() { -1 }
-                }
-                class B extends A {
-                    def m() { this.@p = 1 }
-                }
-                def x = new B()
-                assert A.@p == null
-                x.m()
-            '''
-        }
+        shouldFail MissingFieldException, '''
+            class A {
+                static private p
+
+                void setP(def val) { p = 2 }
+
+                def getP() { -1 }
+            }
+            class B extends A {
+                def m() { this.@p = 1 }
+            }
+            def x = new B()
+            assert A.@p == null
+            x.m()
+        '''
     }
 
     @Test // GROOVY-8385
     void testParentClassPrivateNonStaticAttributeSetAccessShouldNotCallSetter() {
-        shouldFail(MissingFieldException) {
-            Eval.me'''
-                class A {
-                    private p
-                    void setP(def val) { p = 2 }
-                    def getP() { -1 }
-                }
-                class B extends A {
-                    def m() { this.@p = 1 }
-                }
-                def x = new B()
-                assert x.@p == null
-                x.m()
+        shouldFail MissingFieldException, '''
+            class A {
+                private p
+                void setP(def val) { p = 2 }
+                def getP() { -1 }
+            }
+            class B extends A {
+                def m() { this.@p = 1 }
+            }
+            def x = new B()
+            assert x.@p == null
+            x.m()
         '''
-        }
     }
 }