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

paulk 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 f97a773  GROOVY-9501: enable access to static, non-final, 
subclassed-outer fields (port to 2_5_X)
f97a773 is described below

commit f97a7733c2cf0b703746cbe1c86e93f9c7c51b1a
Author: Eric Milles <eric.mil...@thomsonreuters.com>
AuthorDate: Sun Apr 26 11:25:46 2020 -0500

    GROOVY-9501: enable access to static, non-final, subclassed-outer fields 
(port to 2_5_X)
---
 .../groovy/classgen/AsmClassGenerator.java         |  8 +--
 src/test/gls/innerClass/InnerClassTest.groovy      | 74 +++++++++++++++++++++-
 2 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java 
b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index aa49f5e..2f1c1a8 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -979,11 +979,11 @@ public class AsmClassGenerator extends ClassGenerator {
                         if (field==null && classNode instanceof 
InnerClassNode) {
                             ClassNode outer = classNode.getOuterClass();
                             FieldNode outerClassField;
-                            while (outer!=null) {
+                            while (outer != null) {
                                 outerClassField = outer.getDeclaredField(name);
-                                if (outerClassField!=null && 
outerClassField.isStatic() && outerClassField.isFinal()) {
-                                    if (outer!=classNode.getOuterClass() && 
outerClassField.isPrivate()) {
-                                        throw new GroovyBugError("Trying to 
access private constant field 
["+outerClassField.getDeclaringClass()+"#"+outerClassField.getName()+"] from 
inner class");
+                                if (outerClassField != null && 
outerClassField.isStatic()) {
+                                    if (outer != classNode.getOuterClass() && 
outerClassField.isPrivate()) {
+                                        throw new GroovyBugError("Trying to 
access private constant field [" + outerClassField.getDeclaringClass() + "#" + 
outerClassField.getName() + "] from inner class");
                                     }
                                     PropertyExpression pexp = new 
PropertyExpression(
                                             new ClassExpression(outer),
diff --git a/src/test/gls/innerClass/InnerClassTest.groovy 
b/src/test/gls/innerClass/InnerClassTest.groovy
index 6889192..357ffc0 100644
--- a/src/test/gls/innerClass/InnerClassTest.groovy
+++ b/src/test/gls/innerClass/InnerClassTest.groovy
@@ -340,7 +340,79 @@ class InnerClassTest extends CompilableTestSupport {
         """
     }
 
-    void testUsageOfOuterMethodoverridden() {
+    // GROOVY-9501
+    void testUsageOfOuterField7() {
+        assertScript '''
+            class Main extends Outer {
+                static main(args) {
+                    newInstance().newThread()
+                    assert Outer.Inner.error == null
+                }
+            }
+
+            abstract class Outer {
+                private static volatile boolean flag
+
+                void newThread() {
+                    Thread thread = new Inner()
+                    thread.start()
+                    thread.join()
+                }
+
+                private final class Inner extends Thread {
+                    @Override
+                    void run() {
+                        try {
+                            if (!flag) {
+                                // do work
+                            }
+                        } catch (e) {
+                            error = e
+                        }
+                    }
+                    public static error
+                }
+            }
+        '''
+    }
+
+    // inner class is static instead of final
+    void testUsageOfOuterField8() {
+        assertScript '''
+            class Main extends Outer {
+                static main(args) {
+                    newInstance().newThread()
+                    assert Outer.Inner.error == null
+                }
+            }
+
+            abstract class Outer {
+                private static volatile boolean flag
+
+                void newThread() {
+                    Thread thread = new Inner()
+                    thread.start()
+                    thread.join()
+                }
+
+                private static class Inner extends Thread {
+                    @Override
+                    void run() {
+                        try {
+                            if (!flag) {
+                                // do work
+                            }
+                        } catch (e) {
+                            error = e
+                        }
+                    }
+                    public static error
+                }
+            }
+        '''
+    }
+
+    void testUsageOfOuterMethodOverridden() {
         assertScript """
             interface Run {
                 def run()

Reply via email to