groovy git commit: GROOVY-5912: Static compilation: Groovy doesn't fail compilation when accessing package scope methods, but fails at runtime

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: GROOVY-5912: Static compilation: Groovy doesn't fail compilation when accessing package scope methods, but fails at runtime

Daniel.Sun
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X d1970bcf6 -> 7804d4ea9


GROOVY-5912: Static compilation: Groovy doesn't fail compilation when accessing package scope methods, but fails at runtime

(cherry picked from commit b1cc8a6)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: 7804d4ea9782b42665f4c8af09cf59f851091cbd
Parents: d1970bc
Author: sunlan <[hidden email]>
Authored: Tue May 15 23:41:22 2018 +0800
Committer: sunlan <[hidden email]>
Committed: Tue May 15 23:44:04 2018 +0800

----------------------------------------------------------------------
 .../org/codehaus/groovy/ast/MethodNode.java     |  4 ++
 .../stc/StaticTypeCheckingVisitor.java          |  9 +++++
 ...PluginPathAwareFileSystemResourceLoader.java | 26 +++++++++++++
 .../groovy5912/otherpkg/Groovy5912Bug.groovy    | 40 ++++++++++++++++++++
 4 files changed, 79 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/7804d4ea/src/main/java/org/codehaus/groovy/ast/MethodNode.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/MethodNode.java b/src/main/java/org/codehaus/groovy/ast/MethodNode.java
index 0f97ab6..5952780 100644
--- a/src/main/java/org/codehaus/groovy/ast/MethodNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/MethodNode.java
@@ -170,6 +170,10 @@ public class MethodNode extends AnnotatedNode implements Opcodes {
         return (modifiers & ACC_PROTECTED) != 0;
     }
 
+    public boolean isPackageScope() {
+        return !(this.isPrivate() || this.isProtected() || this.isPublic());
+    }
+
     public boolean hasDefaultValue() {
         return this.hasDefaultValue;
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/7804d4ea/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 7dc9917..f0e105f 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4415,6 +4415,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             if (methodNode.isProtected() && !enclosingClassNode.isDerivedFrom(declaringClass)) {
                 continue;
             }
+            if (methodNode.isPackageScope() && !getPackageName(enclosingClassNode).equals(getPackageName(declaringClass))) {
+                continue;
+            }
 
             result.add(methodNode);
         }
@@ -4422,6 +4425,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         return result;
     }
 
+    private static String getPackageName(ClassNode cn) {
+        String name = cn.getPackageName();
+
+        return null == name ? "" : name;
+    }
+
     /**
      * Given a method name and a prefix, returns the name of the property that should be looked up,
      * following the java beans rules. For example, "getName" would return "name", while

http://git-wip-us.apache.org/repos/asf/groovy/blob/7804d4ea/src/test/groovy/bugs/groovy5912/PluginPathAwareFileSystemResourceLoader.java
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/groovy5912/PluginPathAwareFileSystemResourceLoader.java b/src/test/groovy/bugs/groovy5912/PluginPathAwareFileSystemResourceLoader.java
new file mode 100644
index 0000000..33a18f2
--- /dev/null
+++ b/src/test/groovy/bugs/groovy5912/PluginPathAwareFileSystemResourceLoader.java
@@ -0,0 +1,26 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package groovy.bugs.groovy5912;
+
+import java.util.Collection;
+
+public class PluginPathAwareFileSystemResourceLoader {
+    void setSearchLocations(Collection c) {}
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/7804d4ea/src/test/groovy/bugs/groovy5912/otherpkg/Groovy5912Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/groovy5912/otherpkg/Groovy5912Bug.groovy b/src/test/groovy/bugs/groovy5912/otherpkg/Groovy5912Bug.groovy
new file mode 100644
index 0000000..b1fd605
--- /dev/null
+++ b/src/test/groovy/bugs/groovy5912/otherpkg/Groovy5912Bug.groovy
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs.groovy5912.otherpkg
+
+class Groovy5912Bug extends GroovyTestCase {
+    void test() {
+        def errMsg = shouldFail '''
+        package groovy.bugs.groovy5912.otherpkg
+        
+        import groovy.bugs.groovy5912.PluginPathAwareFileSystemResourceLoader
+        
+        @groovy.transform.CompileStatic
+        class GrailsProjectLoader {
+            def access() {
+                new PluginPathAwareFileSystemResourceLoader().setSearchLocations(null)
+            }
+        }
+        
+        new GrailsProjectLoader().access()
+        '''
+
+        assert errMsg.contains('[Static type checking] - Cannot find matching method groovy.bugs.groovy5912.PluginPathAwareFileSystemResourceLoader#setSearchLocations')
+    }
+}

Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun