Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X ff9a3f6a9 -> 5771100d9
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/5771100d Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/5771100d Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/5771100d Branch: refs/heads/GROOVY_2_6_X Commit: 5771100d92ba94fc477bb9793635ce4c34d69956 Parents: ff9a3f6 Author: sunlan <[email protected]> Authored: Tue May 15 23:41:22 2018 +0800 Committer: sunlan <[email protected]> Committed: Tue May 15 23:43:33 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/5771100d/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 902502e..7134b86 100644 --- a/src/main/java/org/codehaus/groovy/ast/MethodNode.java +++ b/src/main/java/org/codehaus/groovy/ast/MethodNode.java @@ -169,6 +169,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/5771100d/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 7ff7ff8..a60ad35 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -4483,6 +4483,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); } @@ -4490,6 +4493,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/5771100d/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/5771100d/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') + } +}
