Skip to content

Commit 8627b6c

Browse files
committed
minor fix-ups
1 parent b328471 commit 8627b6c

File tree

12 files changed

+88
-61
lines changed

12 files changed

+88
-61
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,21 @@ public void testDGM54b() {
789789
assertDeclType(contents, "equals", "org.codehaus.groovy.runtime.DefaultGroovyMethods");
790790
}
791791

792+
@Test
793+
public void testDGM55() {
794+
String contents =
795+
//@formatter:off
796+
"def str = '''\\\n" +
797+
" blah blah\n" +
798+
"'''.stripIndent()\n";
799+
//@formatter:on
800+
801+
assertExprType(contents, "stripIndent", "java.lang.String");
802+
float version = Float.parseFloat(System.getProperty("java.specification.version"));
803+
// Java 13+: @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.TEXT_BLOCKS,essentialAPI=true)
804+
assertDeclType(contents, "stripIndent", version > 12 ? "java.lang.String" : "org.codehaus.groovy.runtime.StringGroovyMethods");
805+
}
806+
792807
@Test // GRECLIPSE-1131
793808
public void testDGMClosure1() {
794809
String contents =

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ public void testGenericsMethodReferenceSearch() throws Exception {
857857

858858
assertEquals(1, matches.size());
859859
// TODO: Determine why the accuracy changed between Java 11 and Java 12.
860-
assertEquals(Integer.parseInt(System.getProperty("java.version").split("\\.")[0]) < 12
860+
assertEquals(Float.parseFloat(System.getProperty("java.specification.version")) < 12
861861
? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE, matches.get(0).getAccuracy());
862862
assertEquals("Baz.java", ((IJavaElement) matches.get(0).getElement()).getResource().getName());
863863
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,8 @@ public void testInlinedStaticFinalAttributeValue() {
865865
};
866866
//@formatter:on
867867

868-
int release = Integer.parseInt(System.getProperty("java.version").split("\\.")[0]);
869-
runConformTest(sources, release < 9 ? "@Anno(value=abc)" : (release < 13 ? "@Anno(value=\"abc\")" : "@Anno(\"abc\")"));
868+
float version = Float.parseFloat(System.getProperty("java.specification.version"));
869+
runConformTest(sources, version < 9 ? "@Anno(value=abc)" : (version < 13 ? "@Anno(value=\"abc\")" : "@Anno(\"abc\")"));
870870
}
871871

872872
@Test

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/AnnotationCollectorTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void testAnnotationCollector3() {
109109
};
110110
//@formatter:on
111111

112-
int release = Integer.parseInt(System.getProperty("java.version").split("\\.")[0]);
113-
runConformTest(sources, release > 13 ? "@NotNull()@Length(0)" : "@NotNull()@Length(value=0)");
112+
float version = Float.parseFloat(System.getProperty("java.specification.version"));
113+
runConformTest(sources, version > 13 ? "@NotNull()@Length(0)" : "@NotNull()@Length(value=0)");
114114
}
115115
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,6 @@ public void visitVariableExpression(final VariableExpression node) {
18311831
}
18321832

18331833
scopes.getLast().setCurrentNode(node);
1834-
visitAnnotations(node);
18351834
if (node.getAccessedVariable() == node) {
18361835
// this is a local variable declaration
18371836
visitClassReference(node.getOriginType());

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -475,16 +475,14 @@ public int getEnclosingClosureResolveStrategy() {
475475
}
476476

477477
/*package*/ VariableScope getEnclosingClosureScope() {
478-
VariableScope scope = this;
479-
do {
478+
for (VariableScope scope = this; scope != null; scope = scope.parent) {
480479
if (scope.scopeNode instanceof ClosureExpression) {
481480
return scope;
482481
}
483482
if (scope.scopeNode instanceof ClassNode) {
484483
break;
485484
}
486-
} while ((scope = scope.parent) != null);
487-
485+
}
488486
return null;
489487
}
490488

ide-test/org.codehaus.groovy.eclipse.codeassist.test/src/org/codehaus/groovy/eclipse/codeassist/tests/FieldCompletionTests.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ final class FieldCompletionTests extends CompletionTestSuite {
979979
|}
980980
|'''.stripMargin()
981981
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, '.'))
982-
proposalExists(proposals, 'forName', System.getProperty('java.specification.version').toFloat() < 9 ? 2 : 3)
982+
proposalExists(proposals, 'forName', Float.parseFloat(System.getProperty('java.specification.version')) < 9 ? 2 : 3)
983983
}
984984

985985
@Test

ide-test/org.codehaus.groovy.eclipse.codeassist.test/src/org/codehaus/groovy/eclipse/codeassist/tests/MethodCompletionTests.groovy

+6-6
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ final class MethodCompletionTests extends CompletionTestSuite {
459459
|Pattern.com
460460
|'''.stripMargin()
461461
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, '.com'))
462-
int release = Integer.parseInt(System.getProperty('java.version').split(/\./)[0])
463-
proposalExists(proposals, 'componentType', release < 12 ? 1 : 2) // from Class
462+
def version = Float.parseFloat(System.getProperty('java.specification.version'))
463+
proposalExists(proposals, 'componentType', version < 12 ? 1 : 2) // from Class
464464
proposalExists(proposals, 'compile', 2) // from Pattern
465465
}
466466

@@ -471,8 +471,8 @@ final class MethodCompletionTests extends CompletionTestSuite {
471471
|Pattern.class.com
472472
|'''.stripMargin()
473473
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, '.com'))
474-
int release = Integer.parseInt(System.getProperty('java.version').split(/\./)[0])
475-
proposalExists(proposals, 'componentType', release < 12 ? 1 : 2) // from Class
474+
def version = Float.parseFloat(System.getProperty('java.specification.version'))
475+
proposalExists(proposals, 'componentType', version < 12 ? 1 : 2) // from Class
476476
proposalExists(proposals, 'compile', 2) // from Pattern
477477
}
478478

@@ -484,8 +484,8 @@ final class MethodCompletionTests extends CompletionTestSuite {
484484
|pat.com
485485
|'''.stripMargin()
486486
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, '.com'))
487-
int release = Integer.parseInt(System.getProperty('java.version').split(/\./)[0])
488-
proposalExists(proposals, 'componentType', release < 12 ? 1 : 2) // from Class
487+
def version = Float.parseFloat(System.getProperty('java.specification.version'))
488+
proposalExists(proposals, 'componentType', version < 12 ? 1 : 2) // from Class
489489
proposalExists(proposals, 'compile', 2) // from Pattern
490490
}
491491

ide-test/org.codehaus.groovy.eclipse.codeassist.test/src/org/codehaus/groovy/eclipse/codeassist/tests/TypeCompletionTests.groovy

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ final class TypeCompletionTests extends CompletionTestSuite {
214214
void testCompleteClass2() {
215215
String contents = 'class Foo { }\nFoo.com'
216216
ICompletionProposal[] proposals = createProposalsAtOffset(contents, contents.length())
217-
int release = Integer.parseInt(System.getProperty('java.version').split(/\./)[0])
218-
proposalExists(proposals, 'componentType', release < 12 ? 1 : 2, true)
217+
def version = Float.parseFloat(System.getProperty('java.specification.version'))
218+
proposalExists(proposals, 'componentType', version < 12 ? 1 : 2, true)
219219
}
220220

221221
@Test
@@ -229,8 +229,8 @@ final class TypeCompletionTests extends CompletionTestSuite {
229229
void testCompleteClass4() {
230230
String contents = 'class Foo { }\nFoo.class.com'
231231
ICompletionProposal[] proposals = createProposalsAtOffset(contents, contents.length())
232-
int release = Integer.parseInt(System.getProperty('java.version').split(/\./)[0])
233-
proposalExists(proposals, 'componentType', release < 12 ? 1 : 2)
232+
def version = Float.parseFloat(System.getProperty('java.specification.version'))
233+
proposalExists(proposals, 'componentType', version < 12 ? 1 : 2)
234234
}
235235

236236
@Test

ide-test/org.codehaus.groovy.eclipse.codebrowsing.test/src/org/codehaus/groovy/eclipse/codebrowsing/tests/PartialVisitTests.groovy

+40-36
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.codehaus.groovy.eclipse.codebrowsing.tests
1717

18-
import static org.junit.Assert.*
19-
2018
import org.codehaus.groovy.ast.ASTNode
2119
import org.codehaus.groovy.eclipse.codebrowsing.requestor.CodeSelectHelper
2220
import org.codehaus.groovy.eclipse.codebrowsing.requestor.CodeSelectRequestor
@@ -36,41 +34,47 @@ final class PartialVisitTests extends BrowsingTestSuite {
3634

3735
private final PartialCodeSelectHelper helper = new PartialCodeSelectHelper()
3836

39-
@Test // should not visit the Foo class or the main method
37+
@Test // should not visit the class body or the main method
4038
void testBasic1() {
41-
String contents = 'new Foo().x\nclass Foo {\n def x\n}\n'
39+
String contents = 'new C().x\nclass C {\n int x\n}'
4240
assertCodeSelectWithSkippedNames(contents, indexOf(contents, 'x'), 'x', 'main(args)')
4341
}
4442

45-
@Test // should not visit the Foo class or the main method
43+
@Test // should not visit the class body or the main method
4644
void testBasic2() {
47-
String contents = 'class Foo {\n def x\n}\nnew Foo().x'
48-
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'x'), 'x', 'Foo', 'main(args)')
45+
String contents = 'class C {\n int x\n}\nnew C().x'
46+
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'x'), 'x', 'C', 'main(args)')
4947
}
5048

51-
@Test
49+
@Test // should not visit the field
5250
void testBasic3() {
53-
String contents = 'class Foo {\n def x\n def blah() {\n x\n}\n}'
54-
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'x'), 'x')
51+
String contents = 'class C {\n int x\n def m() {\n x\n }\n}'
52+
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'x'), 'x', 'x')
5553
}
5654

57-
@Test
55+
@Test // should not visit the field
56+
void testBasic4() {
57+
String contents = 'class C {\n Object x\n def m() {\n x\n }\n}'
58+
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'x'), 'x', 'x')
59+
}
60+
61+
@Test // should not visit the other field
5862
void testFieldInitializer() {
59-
String contents = 'class Foo {\n Foo() {}\n def y\n def x = y\n}'
60-
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'y'), 'y')
63+
String contents = 'class C {\n C() {}\n Number x\n private y = x\n}'
64+
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'x'), 'x', 'x')
6165
}
6266

63-
@Test
67+
@Test // should not visit the other fields
6468
void testStaticFieldInitializer() {
65-
String contents = 'class Foo { Foo() {}\n static y\n def z\n static x = y }'
66-
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'y'), 'y')
69+
String contents = 'class C {\n C() {}\n static Number x\n Number y\n static z = x }'
70+
assertCodeSelectWithSkippedNames(contents, lastIndexOf(contents, 'x'), 'x', 'x', 'y')
6771
}
6872

6973
@Test
7074
void testInnerClass1() {
7175
String contents = '''\
72-
|class Foo {
73-
| Foo() {}
76+
|class C {
77+
| C() {}
7478
| static y
7579
| def z
7680
| static x = y
@@ -87,8 +91,8 @@ final class PartialVisitTests extends BrowsingTestSuite {
8791
@Test
8892
void testInnerClass2() {
8993
String contents = '''\
90-
|class Foo {
91-
| Foo() {}
94+
|class C {
95+
| C() {}
9296
| static y
9397
| def z
9498
| static x = y
@@ -109,9 +113,10 @@ final class PartialVisitTests extends BrowsingTestSuite {
109113
private static String getElementName(IJavaElement element) {
110114
if (element instanceof IMethod) {
111115
try {
112-
String[] params = ((IMethod) element).parameterNames
116+
String[] params = element.parameterNames
113117
return element.elementName + (params.length < 1 ? '()' : Arrays.toString(params).replace('[', '(').replace(']', ')'))
114-
} catch (JavaModelException e) {}
118+
} catch (JavaModelException ignore) {
119+
}
115120
}
116121
return element.elementName
117122
}
@@ -124,22 +129,22 @@ final class PartialVisitTests extends BrowsingTestSuite {
124129
return new Region(contents.lastIndexOf(string), string.length())
125130
}
126131

127-
private void assertCodeSelectWithSkippedNames(String contents, Region region, String expectedElementName, String... skippedElementNames) {
128-
GroovyCompilationUnit unit = addGroovySource(contents, 'Hello')
132+
private void assertCodeSelectWithSkippedNames(String contents, Region region, String expectedElementName, String... expectedSkippedElements) {
133+
def unit = addGroovySource(contents)
129134

130-
IJavaElement[] elems = helper.select(unit, region.offset, region.length)
131-
assertEquals('Should have found a single selection: ' + Arrays.toString(elems), 1, elems.length)
132-
assertEquals('Wrong element selected', expectedElementName, getElementName(elems[0]))
135+
def selected = helper.select(unit, region.offset, region.length)
133136

134-
for (skipped in skippedElementNames) {
135-
assertTrue('Element ' + skipped + ' should have been skipped\nExpected: ' + Arrays.toString(skippedElementNames) + '\nWas: ' + helper.skippedElements, helper.skippedElements.contains(skipped))
136-
}
137+
assert selected.length == 1
138+
assert getElementName(selected[0]) == expectedElementName
137139

138-
assertEquals('Wrong number of elements skipped\nExpected: ' + Arrays.toString(skippedElementNames) + '\nWas: ' + helper.skippedElements, skippedElementNames.length, helper.skippedElements.size())
140+
for (shouldSkip in expectedSkippedElements) {
141+
assert shouldSkip in helper.skippedElements
142+
}
143+
assert helper.skippedElements.size() == expectedSkippedElements.length
139144
}
140145

141146
private static class PartialCodeSelectRequestor extends CodeSelectRequestor {
142-
private final Set<String> skippedElements = new HashSet<String>()
147+
private final Set<String> skippedElements = []
143148

144149
PartialCodeSelectRequestor(ASTNode node, GroovyCompilationUnit unit) {
145150
super(node, unit)
@@ -149,16 +154,15 @@ final class PartialVisitTests extends BrowsingTestSuite {
149154
VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaElement enclosingElement) {
150155
VisitStatus status = super.acceptASTNode(node, result, enclosingElement)
151156
if (status == VisitStatus.CANCEL_MEMBER) {
152-
assert !skippedElements.contains(getElementName(enclosingElement)) :
153-
"Element has been skipped twice, but should only have been skipped once: $enclosingElement"
154-
skippedElements.add(getElementName(enclosingElement))
157+
assert skippedElements.add(getElementName(enclosingElement)) :
158+
"Element '$enclosingElement.elementName' has been skipped more than once"
155159
}
156160
return status
157161
}
158162
}
159163

160164
private static class PartialCodeSelectHelper extends CodeSelectHelper {
161-
private Set<String> skippedElements = new HashSet<String>()
165+
private Set<String> skippedElements = []
162166

163167
@Override
164168
protected CodeSelectRequestor createRequestor(ASTNode node, Region r1, Region r2, GroovyCompilationUnit unit) {

ide/org.codehaus.groovy.eclipse.codeassist/src/org/codehaus/groovy/eclipse/codeassist/processors/StatementAndExpressionCompletionProcessor.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.eclipse.jdt.core.CompletionProposal;
7474
import org.eclipse.jdt.core.CompletionRequestor;
7575
import org.eclipse.jdt.core.Flags;
76+
import org.eclipse.jdt.core.IField;
7677
import org.eclipse.jdt.core.IJavaElement;
7778
import org.eclipse.jdt.core.IMethod;
7879
import org.eclipse.jdt.core.ISourceRange;
@@ -716,13 +717,18 @@ private boolean doTest(final ASTNode node) {
716717
private boolean interestingElement(final IJavaElement enclosingElement) {
717718
try {
718719
switch (enclosingElement.getElementType()) {
719-
case IJavaElement.INITIALIZER:
720720
case IJavaElement.FIELD:
721-
return true;
721+
if ("Qjava.lang.Object;".equals(((IField) enclosingElement).getTypeSignature())) {
722+
return true;
723+
}
724+
break;
722725
case IJavaElement.METHOD:
723726
if (((IMethod) enclosingElement).isConstructor()) {
724727
return true;
725728
}
729+
break;
730+
case IJavaElement.INITIALIZER:
731+
return true;
726732
}
727733

728734
if (enclosingElement instanceof ISourceReference) {

ide/org.codehaus.groovy.eclipse.codebrowsing/src/org/codehaus/groovy/eclipse/codebrowsing/requestor/CodeSelectRequestor.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,18 @@ public VisitStatus acceptASTNode(ASTNode node, final TypeLookupResult result, fi
147147
*/
148148
private boolean interestingElement(final IJavaElement enclosingElement) throws JavaModelException {
149149
switch (enclosingElement.getElementType()) {
150-
case IJavaElement.INITIALIZER:
151150
case IJavaElement.FIELD:
152-
return true;
151+
if ("Qjava.lang.Object;".equals(((IField) enclosingElement).getTypeSignature())) {
152+
return true;
153+
}
154+
break;
153155
case IJavaElement.METHOD:
154156
if (((IMethod) enclosingElement).isConstructor()) {
155157
return true;
156158
}
159+
break;
160+
case IJavaElement.INITIALIZER:
161+
return true;
157162
}
158163

159164
if (enclosingElement instanceof ISourceReference) {

0 commit comments

Comments
 (0)