groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Refine "GROOVY-8531: Fail to resolve type defined in super class written in Java"
Date Thu, 05 Apr 2018 17:38:39 GMT
Repository: groovy
Updated Branches:
  refs/heads/master fdaef2004 -> 418162659


Refine "GROOVY-8531: Fail to resolve type defined in super class written in Java"

Check visibility of inner type


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/41816265
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/41816265
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/41816265

Branch: refs/heads/master
Commit: 418162659040e80164bc89f1623f110069c05cab
Parents: fdaef20
Author: danielsun1106 <realbluesun@hotmail.com>
Authored: Fri Apr 6 01:38:33 2018 +0800
Committer: danielsun1106 <realbluesun@hotmail.com>
Committed: Fri Apr 6 01:38:33 2018 +0800

----------------------------------------------------------------------
 .../codehaus/groovy/control/ResolveVisitor.java    | 17 ++++++++++++++++-
 .../groovy/bugs/groovy8531/Groovy8531Bug.groovy    | 16 ++++++++++++++--
 src/test/groovy/bugs/groovy8531/Reducer.java       |  4 +++-
 3 files changed, 33 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/41816265/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index a48d516..6cfd9be 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -282,7 +282,6 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
             if (resolve(type)) return true;
         }
 
-        // GROOVY-8531: Fail to resolve type defined in super class written in Java
         if (resolveToNestedOfCurrentClassAndSuperClasses(type)) return true;
 
         type.setName(saved);
@@ -290,6 +289,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
     }
 
     private boolean resolveToNestedOfCurrentClassAndSuperClasses(ClassNode type) {
+        // GROOVY-8531: Fail to resolve type defined in super class written in Java
         for (ClassNode enclosingClassNode = currentClass; ClassHelper.OBJECT_TYPE != enclosingClassNode;
enclosingClassNode = enclosingClassNode.getSuperClass()) {
             if(resolveToNested(enclosingClassNode, type)) return true;
         }
@@ -305,6 +305,8 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         if (enclosingType != type && !name.contains(".") && type.getClass().equals(ClassNode.class))
{
             ClassNode tmp = new ConstructedNestedClass(enclosingType,name);
             if (resolve(tmp)) {
+                if (!checkInnerTypeVisibility(enclosingType, tmp)) return false;
+
                 type.setRedirect(tmp);
                 return true;
             }
@@ -313,6 +315,19 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
 
     }
 
+    private boolean checkInnerTypeVisibility(ClassNode enclosingType, ClassNode innerClassNode)
{
+        if (currentClass == enclosingType) {
+            return true;
+        }
+
+        int modifiers = innerClassNode.getModifiers();
+        if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
+            return true;
+        }
+
+        return false;
+    }
+
     private void resolveOrFail(ClassNode type, String msg, ASTNode node) {
         if (resolve(type)) return;
         if (resolveToInner(type)) return;

http://git-wip-us.apache.org/repos/asf/groovy/blob/41816265/src/test/groovy/bugs/groovy8531/Groovy8531Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/groovy8531/Groovy8531Bug.groovy b/src/test/groovy/bugs/groovy8531/Groovy8531Bug.groovy
index b7ee0d2..72dd08b 100644
--- a/src/test/groovy/bugs/groovy8531/Groovy8531Bug.groovy
+++ b/src/test/groovy/bugs/groovy8531/Groovy8531Bug.groovy
@@ -19,18 +19,30 @@
 package groovy.bugs.groovy8531
 
 class Groovy8531Bug extends GroovyTestCase {
-    void test() {
+    void testPublicAndProtectedInnerType() {
         assertScript '''
             package groovy.bugs.groovy8531
             class Example extends Reducer {
-                public void reduce(Context context) {}
+                public void reduce(PublicContext context) {}
+                public void reduce2(ProtectedContext context) {}
                 public boolean isDynamic(Type type) {
                     return Type.DYNAMIC == type
                 }
             }
             
             new Example().reduce(null)
+            new Example().reduce2(null)
             assert new Example().isDynamic(Reducer.Type.DYNAMIC)
         '''
     }
+
+    void testPrivateInnerType() {
+        def errMsg = shouldFail '''
+            package groovy.bugs.groovy8531
+            class Example extends Reducer {
+                public void reduce3(PrivateContext context) {}
+            }
+        '''
+        assert errMsg.contains('unable to resolve class')
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/41816265/src/test/groovy/bugs/groovy8531/Reducer.java
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/groovy8531/Reducer.java b/src/test/groovy/bugs/groovy8531/Reducer.java
index c9b78bf..95b5b24 100644
--- a/src/test/groovy/bugs/groovy8531/Reducer.java
+++ b/src/test/groovy/bugs/groovy8531/Reducer.java
@@ -19,7 +19,9 @@
 package groovy.bugs.groovy8531;
 
 public class Reducer {
-    public abstract class Context {}
+    public abstract class PublicContext {}
+    protected abstract class ProtectedContext {}
+    private abstract class PrivateContext {}
 
     public enum Type {
         DYNAMIC, STATIC


Mime
View raw message