Skip to content

Commit 7be85a9

Browse files
committed
GROOVY-10396
1 parent a31702c commit 7be85a9

File tree

4 files changed

+133
-16
lines changed

4 files changed

+133
-16
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/ImportsTests.java

+99
Original file line numberDiff line numberDiff line change
@@ -1475,4 +1475,103 @@ public void testStaticImportVersusOuterClassMethod2() {
14751475

14761476
runConformTest(sources, "CC");
14771477
}
1478+
1479+
@Test // GROOVY-10396
1480+
public void testStaticImportVersusThisOrSuperMethod1() {
1481+
//@formatter:off
1482+
String[] sources = {
1483+
"Main.groovy",
1484+
"import static p.Q.println\n" +
1485+
"println 'x'\n", // from Script
1486+
1487+
"p/Q.java",
1488+
"package p;\n" +
1489+
"public class Q {\n" +
1490+
" public static void println(String s) {\n" +
1491+
" System.out.println(\"Q:\" + s);\n" +
1492+
" }\n" +
1493+
"}\n",
1494+
};
1495+
//@formatter:on
1496+
1497+
runConformTest(sources, "x");
1498+
}
1499+
1500+
@Test // GROOVY-10396
1501+
public void testStaticImportVersusThisOrSuperMethod2() {
1502+
//@formatter:off
1503+
String[] sources = {
1504+
"Main.groovy",
1505+
"import static p.Q.println\n" +
1506+
"static void test() {\n" +
1507+
" println 'x'\n" +
1508+
"}\n" +
1509+
"test()\n",
1510+
1511+
"p/Q.java",
1512+
"package p;\n" +
1513+
"public class Q {\n" +
1514+
" public static void println(String s) {\n" +
1515+
" System.out.println(\"Q:\" + s);\n" +
1516+
" }\n" +
1517+
"}\n",
1518+
};
1519+
//@formatter:on
1520+
1521+
runConformTest(sources, "Q:x");
1522+
}
1523+
1524+
@Test // GROOVY-10396
1525+
public void testStaticImportVersusThisOrSuperMethod3() {
1526+
//@formatter:off
1527+
String[] sources = {
1528+
"Main.groovy",
1529+
"import static p.Q.println\n" +
1530+
"def obj = new Object() {\n" + // outer class extends Script
1531+
" String toString() {\n" +
1532+
" println 'AIC::x'\n" +
1533+
" super.toString()\n" +
1534+
" }\n" +
1535+
"}\n" +
1536+
"obj.toString()\n",
1537+
1538+
"p/Q.java",
1539+
"package p;\n" +
1540+
"public class Q {\n" +
1541+
" public static void println(String s) {\n" +
1542+
" System.out.println(\"Q:\" + s);\n" +
1543+
" }\n" +
1544+
"}\n",
1545+
};
1546+
//@formatter:on
1547+
1548+
runConformTest(sources, "AIC::x");
1549+
}
1550+
1551+
@Test // GROOVY-10396
1552+
public void testStaticImportVersusThisOrSuperMethod4() {
1553+
//@formatter:off
1554+
String[] sources = {
1555+
"Main.groovy",
1556+
"import static p.Q.println\n" +
1557+
"static void println(String s) {\n" + // static overload
1558+
" System.out.println(s)\n" +
1559+
"}\n" +
1560+
"static void test() {\n" +
1561+
" println 'x'\n" +
1562+
"}\n" +
1563+
"test()\n",
1564+
1565+
"p/Q.java",
1566+
"package p;\n" +
1567+
"public class Q {\n" +
1568+
" public static void println(String s) {\n" +
1569+
" System.out.println(\"Q:\" + s);\n" +
1570+
" }\n" +
1571+
"}\n",
1572+
};
1573+
//@formatter:on
1574+
1575+
runConformTest(sources, "x");
1576+
}
14781577
}

base/org.codehaus.groovy25/src/org/codehaus/groovy/control/StaticImportVisitor.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,31 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
245245
Expression object = transform(mce.getObjectExpression());
246246
Expression method = transform(mce.getMethod());
247247
Expression args = transform(mce.getArguments());
248-
248+
// GRECLIPSE add
249+
boolean staticWrtCurrent = inSpecialConstructorCall || currentMethod != null && currentMethod.isStatic();
250+
// GRECLIPSE end
249251
if (mce.isImplicitThis()) {
250-
if (currentClass.tryFindPossibleMethod(mce.getMethodAsString(), args) == null // GRECLIPSE add -- GROOVY-5239
251-
&& currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(mce.getMethodAsString(), args) != null)) {
252+
/* GRECLIPSE edit -- GROOVY-5239, GROOVY-10396
253+
if (currentClass.tryFindPossibleMethod(mce.getMethodAsString(), args) == null) {
254+
*/
255+
String name = mce.getMethodAsString();
256+
boolean thisOrSuperMethod = staticWrtCurrent ? hasPossibleStaticMethod(currentClass, name, args, true) : currentClass.tryFindPossibleMethod(name, args) != null;
257+
if (!thisOrSuperMethod && currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(name, args) != null)) {
258+
// GRECLIPSE end
252259
Expression result = findStaticMethodImportFromModule(method, args);
253260
if (result != null) {
254261
// GRECLIPSE add
255-
if (!((MethodCall) result).getMethodAsString().equals(method.getText())) {
256-
// store the identifier to facilitate organizing static imports
257-
result.putNodeMetaData("static.import.alias", method.getText());
262+
if (!((MethodCall) result).getMethodAsString().equals(name)) {
263+
// store the identifier to facilitate organizing imports
264+
result.putNodeMetaData("static.import.alias", name);
258265
}
259266
// GRECLIPSE end
260267
result.setSourcePosition(mce);
261268
return result;
262269
}
263270
if (method instanceof ConstantExpression && !inLeftExpression) {
264271
// could be a closure field
265-
result = findStaticFieldOrPropAccessorImportFromModule(method.getText());
272+
result = findStaticFieldOrPropAccessorImportFromModule(name);
266273
if (result != null) {
267274
// GRECLIPSE add
268275
setSourcePosition(result, method);
@@ -285,7 +292,7 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
285292
}
286293
}
287294
}
288-
} else if (currentMethod != null && currentMethod.isStatic() && (object instanceof VariableExpression && ((VariableExpression) object).isSuperExpression())) {
295+
} else if (staticWrtCurrent && (object instanceof VariableExpression && ((VariableExpression) object).isSuperExpression())) {
289296
Expression result = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
290297
result.setSourcePosition(mce);
291298
return result;
@@ -295,7 +302,7 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
295302
|| (object instanceof VariableExpression && (((VariableExpression) object).isThisExpression() || ((VariableExpression) object).isSuperExpression())))) {
296303
String methodName = (String) ((ConstantExpression) method).getValue();
297304

298-
boolean foundInstanceMethod = (currentMethod != null && !currentMethod.isStatic() && currentClass.hasPossibleMethod(methodName, args));
305+
boolean foundInstanceMethod = !staticWrtCurrent && currentClass.hasPossibleMethod(methodName, args);
299306

300307
if (mce.isImplicitThis()) {
301308
if (isInnerClass(currentClass)) {

base/org.codehaus.groovy30/src/org/codehaus/groovy/control/StaticImportVisitor.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,18 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
250250
Expression object = transform(mce.getObjectExpression());
251251
Expression method = transform(mce.getMethod());
252252
Expression args = transform(mce.getArguments());
253-
253+
// GRECLIPSE add
254+
boolean staticWrtCurrent = inSpecialConstructorCall || currentMethod != null && currentMethod.isStatic();
255+
// GRECLIPSE end
254256
if (mce.isImplicitThis()) {
257+
/* GRECLIPSE edit -- GROOVY-10396
255258
if (currentClass.tryFindPossibleMethod(mce.getMethodAsString(), args) == null // GRECLIPSE add -- GROOVY-5239
256259
&& currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(mce.getMethodAsString(), args) != null)) {
260+
*/
261+
String name = mce.getMethodAsString();
262+
boolean thisOrSuperMethod = staticWrtCurrent ? hasPossibleStaticMethod(currentClass, name, args, true) : currentClass.tryFindPossibleMethod(name, args) != null;
263+
if (!thisOrSuperMethod && currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(name, args) != null)) {
264+
// GRECLIPSE end
257265
Expression result = findStaticMethodImportFromModule(method, args);
258266
if (result != null) {
259267
// GRECLIPSE add
@@ -290,7 +298,7 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
290298
}
291299
}
292300
}
293-
} else if (currentMethod != null && currentMethod.isStatic() && (object instanceof VariableExpression && ((VariableExpression) object).isSuperExpression())) {
301+
} else if (staticWrtCurrent && (object instanceof VariableExpression && ((VariableExpression) object).isSuperExpression())) {
294302
Expression result = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
295303
result.setSourcePosition(mce);
296304
return result;
@@ -300,7 +308,7 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
300308
|| (object instanceof VariableExpression && (((VariableExpression) object).isThisExpression() || ((VariableExpression) object).isSuperExpression())))) {
301309
String methodName = (String) ((ConstantExpression) method).getValue();
302310

303-
boolean foundInstanceMethod = (currentMethod != null && !currentMethod.isStatic() && currentClass.hasPossibleMethod(methodName, args));
311+
boolean foundInstanceMethod = !staticWrtCurrent && currentClass.hasPossibleMethod(methodName, args);
304312

305313
Predicate<ClassNode> hasPossibleStaticMember = cn -> {
306314
if (hasPossibleStaticMethod(cn, methodName, args, true)) {

base/org.codehaus.groovy40/src/org/codehaus/groovy/control/StaticImportVisitor.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,13 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
246246
Expression method = transform(mce.getMethod());
247247
Expression args = transform(mce.getArguments());
248248

249+
// GROOVY-10396: skip the instance method checks when the context is static with-respect-to current class
250+
boolean staticWrtCurrent = inSpecialConstructorCall || currentMethod != null && currentMethod.isStatic();
251+
249252
if (mce.isImplicitThis()) {
250253
String name = mce.getMethodAsString();
251-
if (currentClass.tryFindPossibleMethod(name, args) == null
252-
&& currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(name, args) != null)) {
254+
boolean thisOrSuperMethod = staticWrtCurrent ? hasPossibleStaticMethod(currentClass, name, args, true) : currentClass.tryFindPossibleMethod(name, args) != null;
255+
if (!thisOrSuperMethod && currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(name, args) != null)) {
253256
Expression result = findStaticMethodImportFromModule(method, args);
254257
if (result != null) {
255258
// GRECLIPSE add
@@ -277,7 +280,7 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
277280
return result;
278281
}
279282
}
280-
} else if (currentMethod != null && currentMethod.isStatic() && isSuperExpression(object)) {
283+
} else if (staticWrtCurrent && isSuperExpression(object)) {
281284
Expression result = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
282285
result.setSourcePosition(mce);
283286
return result;
@@ -286,7 +289,7 @@ protected Expression transformMethodCallExpression(MethodCallExpression mce) {
286289
if (method instanceof ConstantExpression && ((ConstantExpression) method).getValue() instanceof String && (mce.isImplicitThis() || isThisOrSuper(object))) {
287290
String methodName = (String) ((ConstantExpression) method).getValue();
288291

289-
boolean foundInstanceMethod = (currentMethod != null && !currentMethod.isStatic() && currentClass.hasPossibleMethod(methodName, args));
292+
boolean foundInstanceMethod = !staticWrtCurrent && currentClass.hasPossibleMethod(methodName, args);
290293

291294
Predicate<ClassNode> hasPossibleStaticMember = cn -> {
292295
if (hasPossibleStaticMethod(cn, methodName, args, true)) {

0 commit comments

Comments
 (0)