Skip to content

Commit f359f84

Browse files
committed
Fix for #1370: static field and method references via class expression
see GROOVY-10592 and GROOVY-10695
1 parent 68a191e commit f359f84

3 files changed

Lines changed: 58 additions & 35 deletions

File tree

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,9 +2701,13 @@ public void testInterfaceMethodAsProperty5() {
27012701

27022702
String contents = "abstract class C extends foo.Baz {}\ndef meth(C c) {\n c.one + c.two\n}\n";
27032703

2704-
assertUnknown(contents, "one");
2705-
int offset = contents.indexOf("two");
2706-
assertDeclaringType(contents, offset, offset + 3, "foo.Baz");
2704+
if (isAtLeastGroovy(50)) {
2705+
assertType(contents, "one", "java.lang.Integer");
2706+
assertDeclaringType(contents, "one", "foo.Bar");
2707+
} else {
2708+
assertUnknown(contents, "one"); // unavailable
2709+
}
2710+
assertDeclaringType(contents, "two", "foo.Baz");
27072711
}
27082712

27092713
@Test

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/StaticInferencingTests.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public void testStaticReference10() {
345345
public void testStaticReference11() {
346346
String contents =
347347
"class C {\n" +
348-
" static def foo\n" +
348+
" static foo\n" +
349349
" static {\n" +
350350
" foo\n" +
351351
" }\n" +
@@ -358,8 +358,8 @@ public void testStaticReference11() {
358358
public void testStaticReference12() {
359359
String contents =
360360
"class C {\n" +
361-
" static def foo\n" +
362-
" static def method() {\n" +
361+
" static foo\n" +
362+
" static method() {\n" +
363363
" foo\n" +
364364
" }\n" +
365365
"}\n";
@@ -371,8 +371,8 @@ public void testStaticReference12() {
371371
public void testStaticReference13() {
372372
String contents =
373373
"class C {\n" +
374-
" static def foo\n" +
375-
" static def getFoo() {}\n" +
374+
" static foo\n" +
375+
" static getFoo() {}\n" +
376376
" static {\n" +
377377
" foo\n" +
378378
" }\n" +
@@ -385,18 +385,31 @@ public void testStaticReference13() {
385385
public void testStaticReference14() {
386386
String contents =
387387
"class C {\n" +
388-
" static def foo\n" +
389-
" static def getFoo() {}\n" +
390-
" static def method() {\n" +
388+
" static foo\n" +
389+
" static getFoo() {}\n" +
390+
" static method() {\n" +
391391
" foo\n" +
392392
" }\n" +
393393
"}\n";
394394
ASTNode decl = assertKnown(contents, "foo", "C", "java.lang.Object");
395395
Assert.assertTrue(decl instanceof FieldNode); // not MethodNode
396396
}
397397

398-
@Test
398+
@Test // GROOVY-10695
399399
public void testStaticReference15() {
400+
String contents =
401+
"class C {\n" +
402+
" static Integer foo\n" +
403+
" static getFoo() {}\n" +
404+
" static method() { \n" +
405+
" C.foo\n" +
406+
" }\n" +
407+
"}\n";
408+
assertKnown(contents, "foo", "C", isAtLeastGroovy(50) ? "java.lang.Integer" : "java.lang.Object");
409+
}
410+
411+
@Test
412+
public void testStaticReference16() {
400413
String contents =
401414
"class C {\n" +
402415
" static Number x() {\n" +
@@ -408,7 +421,7 @@ public void testStaticReference15() {
408421
}
409422

410423
@Test
411-
public void testStaticReference16() {
424+
public void testStaticReference17() {
412425
String contents =
413426
"class C {\n" +
414427
" static Number x() {\n" +
@@ -420,7 +433,7 @@ public void testStaticReference16() {
420433
}
421434

422435
@Test
423-
public void testStaticReference17() {
436+
public void testStaticReference18() {
424437
String contents =
425438
"class Two {\n" +
426439
" static Number x() {\n" +
@@ -433,7 +446,7 @@ public void testStaticReference17() {
433446
}
434447

435448
@Test
436-
public void testStaticReference18() {
449+
public void testStaticReference19() {
437450
String contents =
438451
"class Two {\n" +
439452
" static Number x() {\n" +
@@ -446,7 +459,7 @@ public void testStaticReference18() {
446459
}
447460

448461
@Test // GRECLISPE-1244
449-
public void testStaticReference19() {
462+
public void testStaticReference20() {
450463
String contents =
451464
"class Parent {\n" +
452465
" static p() {}\n" +
@@ -460,7 +473,7 @@ public void testStaticReference19() {
460473
}
461474

462475
@Test // GRECLISPE-1244
463-
public void testStaticReference20() {
476+
public void testStaticReference21() {
464477
createUnit("Parent",
465478
"class Parent {\n" +
466479
" static p() {}\n" +
@@ -475,7 +488,7 @@ public void testStaticReference20() {
475488
}
476489

477490
@Test
478-
public void testStaticReference21() {
491+
public void testStaticReference22() {
479492
createUnit("foo", "Bar", "package foo\n" +
480493
"import java.util.regex.*\n" +
481494
"class Bar {\n" +
@@ -489,7 +502,7 @@ public void testStaticReference21() {
489502
}
490503

491504
@Test
492-
public void testStaticReference22() {
505+
public void testStaticReference23() {
493506
createUnit("foo", "Bar", "package foo\n" +
494507
"import java.util.regex.*\n" +
495508
"class Bar {\n" +
@@ -503,7 +516,7 @@ public void testStaticReference22() {
503516
}
504517

505518
@Test
506-
public void testStaticReference23() {
519+
public void testStaticReference24() {
507520
createUnit("foo", "Bar", "package foo\n" +
508521
"import java.util.regex.*\n" +
509522
"class Bar {\n" +
@@ -520,7 +533,7 @@ public void testStaticReference23() {
520533
}
521534

522535
@Test
523-
public void testStaticReference24() {
536+
public void testStaticReference25() {
524537
createUnit("foo", "Bar", "package foo\n" +
525538
"import java.util.regex.*\n" +
526539
"abstract class Bar {\n" +
@@ -537,7 +550,7 @@ public void testStaticReference24() {
537550
}
538551

539552
@Test
540-
public void testStaticReference25() {
553+
public void testStaticReference26() {
541554
String contents =
542555
"import static java.util.regex.Pattern.*\n" +
543556
"import java.util.regex.*\n" +
@@ -553,7 +566,7 @@ public void testStaticReference25() {
553566
}
554567

555568
@Test
556-
public void testStaticReference26() {
569+
public void testStaticReference27() {
557570
String contents =
558571
"import static java.util.regex.Pattern.*\n" +
559572
"import java.util.regex.*\n" +
@@ -571,7 +584,7 @@ public void testStaticReference26() {
571584
}
572585

573586
@Test // https://github.com/groovy/groovy-eclipse/issues/1257
574-
public void testStaticReference27() {
587+
public void testStaticReference28() {
575588
String contents =
576589
"class C {\n" +
577590
" static name\n" +
@@ -581,7 +594,7 @@ public void testStaticReference27() {
581594
}
582595

583596
@Test // https://github.com/groovy/groovy-eclipse/issues/1257
584-
public void testStaticReference28() {
597+
public void testStaticReference29() {
585598
String contents =
586599
"class C {\n" +
587600
" static getName() {}\n" +
@@ -591,7 +604,7 @@ public void testStaticReference28() {
591604
}
592605

593606
@Test
594-
public void testStaticReference29() {
607+
public void testStaticReference30() {
595608
String contents =
596609
"class C {\n" +
597610
" static String getName() {'foo'}\n" +
@@ -605,7 +618,7 @@ public void testStaticReference29() {
605618
}
606619

607620
@Test
608-
public void testStaticReference30() {
621+
public void testStaticReference31() {
609622
String contents =
610623
"class C {\n" +
611624
" final static String name = 'foo'\n" +
@@ -619,7 +632,7 @@ public void testStaticReference30() {
619632
}
620633

621634
@Test // https://github.com/groovy/groovy-eclipse/issues/1382
622-
public void testStaticReference31() {
635+
public void testStaticReference32() {
623636
String contents =
624637
"class C {\n" +
625638
" private static String name = 'foo'\n" +
@@ -633,7 +646,7 @@ public void testStaticReference31() {
633646
}
634647

635648
@Test
636-
public void testStaticReference32() {
649+
public void testStaticReference33() {
637650
String contents =
638651
"static getStaticProperty() {}\n" +
639652
"static staticMethod() {\n" +
@@ -645,7 +658,7 @@ public void testStaticReference32() {
645658
}
646659

647660
@Test
648-
public void testStaticReference33() {
661+
public void testStaticReference34() {
649662
String contents =
650663
"static getStaticProperty() {}\n" +
651664
"def scriptMethod() {\n" +
@@ -657,7 +670,7 @@ public void testStaticReference33() {
657670
}
658671

659672
@Test // GROOVY-10859
660-
public void testStaticReference34() {
673+
public void testStaticReference35() {
661674
createUnit("Other",
662675
"class Other {\n" +
663676
" static getName() {\n" +

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ private static Stream<MethodNode> findMetaObjectMethods(final ClassNode declarin
899899
private static Optional<MethodNode> findPropertyAccessorMethod(final String propertyName, final ClassNode declaringType, final boolean isLhsExpression, final boolean isStaticExpression, final List<ClassNode> methodCallArgumentTypes) {
900900
Stream<MethodNode> accessors = AccessorSupport.findAccessorMethodsForPropertyName(propertyName, declaringType, false, !isLhsExpression ? READER : WRITER);
901901
accessors = accessors.filter(accessor -> isCompatible(accessor, isStaticExpression) && !isTraitBridge(accessor) &&
902-
(!accessor.isStatic() || !accessor.getDeclaringClass().isInterface())); // GROOVY-10592
902+
(!accessor.isStatic() || !accessor.getDeclaringClass().isInterface() || GroovyUtils.getGroovyVersion().getMajor() >= 5)); // GROOVY-10592
903903
if (isLhsExpression) {
904904
// use methodCallArgumentTypes to select closer match
905905
accessors = accessors.sorted((m1, m2) -> (m1 == closer(m2, m1, methodCallArgumentTypes) ? -1 : +1));
@@ -1117,14 +1117,20 @@ protected static Expression getObjectExpression(final VariableScope scope) {
11171117
}
11181118

11191119
protected static boolean isThisObjectExpression(final VariableScope scope) {
1120-
// TODO: Test for "Type.this.name" expressions for inner classes?
11211120
Expression expr = getObjectExpression(scope);
1122-
return (expr instanceof VariableExpression && ((VariableExpression) expr).isThisExpression());
1121+
if (expr instanceof VariableExpression) {
1122+
return ((VariableExpression) expr).isThisExpression();
1123+
} else if (expr instanceof ClassExpression) { // GROOVY-10695
1124+
return expr.getType().equals(scope.getEnclosingTypeDeclaration()) && GroovyUtils.getGroovyVersion().getMajor() >= 5;
1125+
}
1126+
return false;
11231127
}
11241128

11251129
protected static boolean isSuperObjectExpression(final VariableScope scope) {
1126-
// TODO: Test for "Type.super.name" expressions for traits?
11271130
Expression expr = getObjectExpression(scope);
1131+
if (expr instanceof PropertyExpression && ((PropertyExpression) expr).getObjectExpression() instanceof ClassExpression) {
1132+
expr = ((PropertyExpression) expr).getProperty();
1133+
}
11281134
return (expr instanceof VariableExpression && ((VariableExpression) expr).isSuperExpression());
11291135
}
11301136

0 commit comments

Comments
 (0)