You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/03/02 22:07:22 UTC
[groovy] branch GROOVY_3_0_X updated: GROOVY-9893: STC: findSetters
must look in super types
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new f3ea64d GROOVY-9893: STC: findSetters must look in super types
f3ea64d is described below
commit f3ea64d2d6ca11c4773fe28cfa71255d3bf2e73d
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Jan 18 11:14:05 2021 -0600
GROOVY-9893: STC: findSetters must look in super types
---
.../transform/stc/StaticTypeCheckingSupport.java | 25 ++++------
.../stc/FieldsAndPropertiesSTCTest.groovy | 58 +++++++++++++++++++++-
2 files changed, 68 insertions(+), 15 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 63788c5..76ca9c3 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -2207,27 +2207,24 @@ public abstract class StaticTypeCheckingSupport {
}
public static List<MethodNode> findSetters(final ClassNode cn, final String setterName, final boolean voidOnly) {
- List<MethodNode> result = null;
- for (MethodNode method : cn.getDeclaredMethods(setterName)) {
- if (setterName.equals(method.getName())
- && (!voidOnly || VOID_TYPE == method.getReturnType())
- && method.getParameters().length == 1) {
- if (result == null) {
- result = new LinkedList<>();
- }
- result.add(method);
+ List<MethodNode> result = new ArrayList<>();
+ if (!cn.isInterface()) {
+ for (MethodNode method : cn.getMethods(setterName)) {
+ if (isSetter(method, voidOnly)) result.add(method);
}
}
- if (result == null) {
- ClassNode parent = cn.getSuperClass();
- if (parent != null) {
- return findSetters(parent, setterName, voidOnly);
+ for (ClassNode in : cn.getAllInterfaces()) {
+ for (MethodNode method : in.getDeclaredMethods(setterName)) {
+ if (isSetter(method, voidOnly)) result.add(method);
}
- return Collections.emptyList();
}
return result;
}
+ private static boolean isSetter(final MethodNode mn, final boolean voidOnly) {
+ return (!voidOnly || mn.isVoidMethod()) && mn.getParameters().length == 1;
+ }
+
public static ClassNode isTraitSelf(final VariableExpression vexp) {
if (Traits.THIS_OBJECT.equals(vexp.getName())) {
Variable accessedVariable = vexp.getAccessedVariable();
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 272327c..ea5bbfa 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -795,6 +795,62 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
'''
}
+ // GROOVY-9893
+ void testPropertyWithMultipleSetters2() {
+ assertScript '''
+ abstract class A { String which
+ void setX(String s) { which = 'String' }
+ }
+ class C extends A {
+ void setX(boolean b) { which = 'boolean' }
+ }
+
+ void test() {
+ def c = new C()
+ c.x = 'value'
+ assert c.which == 'String'
+ }
+ test()
+ '''
+ }
+
+ // GROOVY-9893
+ void testPropertyWithMultipleSetters3() {
+ assertScript '''
+ interface I {
+ void setX(String s)
+ }
+ abstract class A implements I { String which
+ void setX(boolean b) { which = 'boolean' }
+ }
+
+ void test(A a) {
+ a.x = 'value'
+ assert a.which == 'String'
+ }
+ test(new A() { void setX(String s) { which = 'String' } })
+ '''
+ }
+
+ // GROOVY-9893
+ void testPropertyWithMultipleSetters4() {
+ assertScript '''
+ trait T { String which
+ void setX(String s) { which = 'String' }
+ }
+ class C implements T {
+ void setX(boolean b) { which = 'boolean' }
+ }
+
+ void test() {
+ def c = new C()
+ c.x = 'value'
+ assert c.which == 'String'
+ }
+ test()
+ '''
+ }
+
void testPropertyAssignmentAsExpression() {
assertScript '''
class Foo {
@@ -803,7 +859,7 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
def f = new Foo()
def v = f.x = 3
assert v == 3
-'''
+ '''
}
void testPropertyAssignmentInSubClassAndMultiSetter() {