GROOVY-7987: Type checker doesn't flag static method calls to instance methods with otherwise the same signature (closes #456)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9d5de4f5 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9d5de4f5 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9d5de4f5 Branch: refs/heads/GROOVY_2_4_X Commit: 9d5de4f59c6b2d2aef8ca5d72c9adf05542939b1 Parents: c6519a3 Author: paulk <[email protected]> Authored: Wed Nov 2 16:17:25 2016 +1000 Committer: paulk <[email protected]> Committed: Mon Nov 7 09:38:26 2016 +1000 ---------------------------------------------------------------------- .../stc/StaticTypeCheckingVisitor.java | 6 +++ src/test/groovy/bugs/Groovy7987Bug.groovy | 40 ++++++++++++++++++++ 2 files changed, 46 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/9d5de4f5/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 5564ab2..7bf0bcc 100644 --- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -2959,6 +2959,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport { mn = disambiguateMethods(mn, chosenReceiver!=null?chosenReceiver.getType():null, args, call); if (mn.size() == 1) { MethodNode directMethodCallCandidate = mn.get(0); + if (call.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION) == null && + !directMethodCallCandidate.isStatic() && objectExpression instanceof ClassExpression && + !"java.lang.Class".equals(directMethodCallCandidate.getDeclaringClass().getName())) { + ClassNode owner = directMethodCallCandidate.getDeclaringClass(); + addStaticTypeError("Non static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call); + } if (chosenReceiver==null) { chosenReceiver = Receiver.make(directMethodCallCandidate.getDeclaringClass()); if (chosenReceiver==null) { http://git-wip-us.apache.org/repos/asf/groovy/blob/9d5de4f5/src/test/groovy/bugs/Groovy7987Bug.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/bugs/Groovy7987Bug.groovy b/src/test/groovy/bugs/Groovy7987Bug.groovy new file mode 100644 index 0000000..6d03466 --- /dev/null +++ b/src/test/groovy/bugs/Groovy7987Bug.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 + +import gls.CompilableTestSupport + +class Groovy7987Bug extends CompilableTestSupport { + void testBindablePropertySettersHaveValidModifiersForMethod() { + def message = shouldNotCompile """ + @groovy.transform.TypeChecked + class Foo { + def bar() { } + } + + @groovy.transform.TypeChecked + def method() { + Foo.bar() + } + + method() + """ + assert message.contains('Non static method Foo#bar cannot be called from static context') + } +}
