Skip to content

Commit

Permalink
GROOVY-9893
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 18, 2021
1 parent 3602002 commit 89341d0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5557,4 +5557,49 @@ public void testCompileStatic9892() {

runConformTest(sources, "X1Y0");
}

@Test
public void testCompileStatic9893() {
//@formatter:off
String[] sources = {
"Main.groovy",
"abstract class A {\n" +
" void setX(String s) { print 'String' }\n" +
"}\n" +
"class C extends A {\n" +
" void setX(boolean b) { print 'boolean' }\n" +
"}\n" +
"@groovy.transform.CompileStatic\n" +
"void test() {\n" +
" def c = new C()\n" +
" c.x = 'value'\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources, "String");
}

@Test
public void testCompileStatic9893a() {
//@formatter:off
String[] sources = {
"Main.groovy",
"interface I {\n" +
" void setX(String s)\n" +
"}\n" +
"abstract class A implements I {\n" +
" void setX(boolean b) { print 'boolean' }\n" +
"}\n" +
"@groovy.transform.CompileStatic\n" +
"void test(A a) {\n" +
" a.x = 'value'\n" +
"}\n" +
"test(new A() { void setX(String s) { print 'String' } })\n",
};
//@formatter:on

runConformTest(sources, "String");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,7 @@ public static boolean isClassClassNodeWrappingConcreteType(ClassNode classNode)
}

public static List<MethodNode> findSetters(ClassNode cn, String setterName, boolean voidOnly) {
/* GRECLIPSE edit -- GROOVY-9893
List<MethodNode> result = null;
for (MethodNode method : cn.getDeclaredMethods(setterName)) {
if (setterName.equals(method.getName())
Expand All @@ -2369,9 +2370,28 @@ public static List<MethodNode> findSetters(ClassNode cn, String setterName, bool
}
return Collections.emptyList();
}
*/
List<MethodNode> result = new ArrayList<>();
if (!cn.isInterface()) {
for (MethodNode method : cn.getMethods(setterName)) {
if (isSetter(method, voidOnly)) result.add(method);
}
}
for (ClassNode in : cn.getAllInterfaces()) {
for (MethodNode method : in.getDeclaredMethods(setterName)) {
if (isSetter(method, voidOnly)) result.add(method);
}
}
// GRECLIPSE end
return result;
}

// GRECLIPSE add
private static boolean isSetter(final MethodNode mn, final boolean voidOnly) {
return (!voidOnly || mn.isVoidMethod()) && mn.getParameters().length == 1;
}
// GRECLIPSE end

public static ClassNode isTraitSelf(VariableExpression vexp) {
if (Traits.THIS_OBJECT.equals(vexp.getName())) {
Variable accessedVariable = vexp.getAccessedVariable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,7 @@ public static boolean isClassClassNodeWrappingConcreteType(final ClassNode class
}

public static List<MethodNode> findSetters(final ClassNode cn, final String setterName, final boolean voidOnly) {
/* GRECLIPSE edit -- GROOVY-9893
List<MethodNode> result = null;
for (MethodNode method : cn.getDeclaredMethods(setterName)) {
if (setterName.equals(method.getName())
Expand All @@ -2233,9 +2234,28 @@ public static List<MethodNode> findSetters(final ClassNode cn, final String sett
}
return Collections.emptyList();
}
*/
List<MethodNode> result = new ArrayList<>();
if (!cn.isInterface()) {
for (MethodNode method : cn.getMethods(setterName)) {
if (isSetter(method, voidOnly)) result.add(method);
}
}
for (ClassNode in : cn.getAllInterfaces()) {
for (MethodNode method : in.getDeclaredMethods(setterName)) {
if (isSetter(method, voidOnly)) result.add(method);
}
}
// GRECLIPSE end
return result;
}

// GRECLIPSE add
private static boolean isSetter(final MethodNode mn, final boolean voidOnly) {
return (!voidOnly || mn.isVoidMethod()) && mn.getParameters().length == 1;
}
// GRECLIPSE end

public static ClassNode isTraitSelf(final VariableExpression vexp) {
if (Traits.THIS_OBJECT.equals(vexp.getName())) {
Variable accessedVariable = vexp.getAccessedVariable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,7 @@ public static boolean isClassClassNodeWrappingConcreteType(final ClassNode class
}

public static List<MethodNode> findSetters(final ClassNode cn, final String setterName, final boolean voidOnly) {
/* GRECLIPSE edit -- GROOVY-9893
List<MethodNode> result = null;
for (MethodNode method : cn.getDeclaredMethods(setterName)) {
if (setterName.equals(method.getName())
Expand All @@ -2228,9 +2229,28 @@ public static List<MethodNode> findSetters(final ClassNode cn, final String sett
}
return Collections.emptyList();
}
*/
List<MethodNode> result = new ArrayList<>();
if (!cn.isInterface()) {
for (MethodNode method : cn.getMethods(setterName)) {
if (isSetter(method, voidOnly)) result.add(method);
}
}
for (ClassNode in : cn.getAllInterfaces()) {
for (MethodNode method : in.getDeclaredMethods(setterName)) {
if (isSetter(method, voidOnly)) result.add(method);
}
}
// GRECLIPSE end
return result;
}

// GRECLIPSE add
private static boolean isSetter(final MethodNode mn, final boolean voidOnly) {
return (!voidOnly || mn.isVoidMethod()) && mn.getParameters().length == 1;
}
// GRECLIPSE end

public static ClassNode isTraitSelf(final VariableExpression vexp) {
if (Traits.THIS_OBJECT.equals(vexp.getName())) {
Variable accessedVariable = vexp.getAccessedVariable();
Expand Down

0 comments on commit 89341d0

Please sign in to comment.