Skip to content

Commit

Permalink
Fix for #711: handle raw types in GenericsMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 10, 2018
1 parent d897df4 commit dd823e2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public void testDGM47() throws Throwable {
" it\n" +
"}\n";
int start = contents.lastIndexOf("collect"), until = start + "collect".length();
assertTypeOneOf(contents, start, until, "java.util.List", "java.util.List<T>", "java.util.List<java.lang.Object<T>>");
assertTypeOneOf(contents, start, until, "java.util.List", "java.util.List<java.lang.Object>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public void testClassReference4a() {
public void testClassReference4b() {
String contents = "Class clazz = String; clazz.package";
assertExprType(contents, "package", "java.lang.Package");
assertDeclType(contents, "package", "java.lang.Class");
assertDeclType(contents, "package", "java.lang.Class<java.lang.Object>");
}

@Test // GRECLIPSE-1229: constructors with map parameters
Expand Down Expand Up @@ -1637,7 +1637,7 @@ public void testListRemove() {
" }\n" +
"}";
int offset = contents.indexOf("a.remove") + 2;
MethodNode m = assertDeclaration(contents, offset, offset + "remove".length(), "java.util.List", "remove", DeclarationKind.METHOD);
MethodNode m = assertDeclaration(contents, offset, offset + "remove".length(), "java.util.List<java.lang.Object>", "remove", DeclarationKind.METHOD);
Assert.assertEquals("Should resolve to remove(int) due to return type of inner call", "int", printTypeName(m.getParameters()[0].getType()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.eclipse.jdt.groovy.search;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -60,8 +61,14 @@ public static GenericsMapper gatherGenerics(ClassNode resolvedType, ClassNode de
GenericsType[] rgts = GroovyUtils.getGenericsTypes(rCandidate);
GenericsType[] ugts = GroovyUtils.getGenericsTypes(uCandidate);

int n = Math.min(rgts.length, ugts.length);
Map<String, ClassNode> resolved = (n <= 0) ? Collections.EMPTY_MAP : new TreeMap<>();
int n = ugts.length;
if (n > 0 && rgts.length < 1) {
rgts = new GenericsType[n]; // assume rCandidate is a raw type
Arrays.fill(rgts, new GenericsType(VariableScope.OBJECT_CLASS_NODE));
}
assert rgts.length == ugts.length;

Map<String, ClassNode> resolved = (n > 0 ? new TreeMap<>() : Collections.EMPTY_MAP);
for (int i = 0; i < n; i += 1) {
// now try to resolve the parameter in the context of the
// most recently visited type. If it doesn't exist, then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
x
}
'''.stripIndent()
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), getIndexOf(contents, 'x'))
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), contents.indexOf('x'))
proposalExists(proposals, 'm(List<String> strings, Object[] objects) : String - Override method in \'I\'', 1)
proposalExists(proposals, 'equals(Object obj) : boolean - Override method in \'Object\'', 1)
}
Expand All @@ -167,31 +167,42 @@ final class MethodCompletionTests extends CompletionTestSuite {
x
}
'''.stripIndent()
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), getIndexOf(contents, 'x'))
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), contents.indexOf('x'))
proposalExists(proposals, 'compareTo(String o) : int - Override method in \'Comparable\'', 1)
}

@Test
@Test // https://github.com/groovy/groovy-eclipse/issues/711
void testOverride3() {
String contents = '''\
class A implements Comparable {
x
}
'''.stripIndent()
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), contents.indexOf('x'))
proposalExists(proposals, 'compareTo(Object o) : int - Override method in \'Comparable\'', 1)
}

@Test
void testOverride4() {
String contents = '''\
import java.util.concurrent.Callable
class A implements Callable<String> {
x
}
'''.stripIndent()
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), getIndexOf(contents, 'x'))
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), contents.indexOf('x'))
proposalExists(proposals, 'call() : String - Override method in \'Callable\'', 1)
}

@Test
void testOverride4() {
void testOverride5() {
String contents = '''\
// Comparator redeclares equals(Object)
class A implements Comparator<String> {
x
}
'''.stripIndent()
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), getIndexOf(contents, 'x'))
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), contents.indexOf('x'))
proposalExists(proposals, 'equals(Object obj) : boolean - Override method in \'Comparator\'', 1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.CompletionRequestor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.groovy.search.GenericsMapper;
import org.eclipse.jdt.groovy.search.VariableScope;
Expand All @@ -64,10 +63,9 @@ public List<ICompletionProposal> generateProposals(IProgressMonitor progressMoni
List<ICompletionProposal> proposals = new ArrayList<>();

ContentAssistContext context = getContext();
IType enclosingType = context.getEnclosingType();
if (enclosingType != null) {
if (context.getEnclosingType() != null) {
for (MethodNode method : collectUnimplementedMethods(context.completionExpression, context.getEnclosingGroovyType())) {
proposals.add(createProposal(method, enclosingType, progressMonitor));
proposals.add(createProposal(method, progressMonitor));
}
}
// add proposals from relevant proposal providers
Expand All @@ -76,7 +74,7 @@ public List<ICompletionProposal> generateProposals(IProgressMonitor progressMoni
List<MethodNode> methods = provider.getNewMethodProposals(context);
if (methods != null) {
for (MethodNode method : methods) {
proposals.add(createProposal(method, enclosingType, progressMonitor));
proposals.add(createProposal(method, progressMonitor));
}
}
}
Expand All @@ -87,7 +85,7 @@ public List<ICompletionProposal> generateProposals(IProgressMonitor progressMoni
return proposals;
}

private ICompletionProposal createProposal(MethodNode method, IType enclosingType, IProgressMonitor progressMonitor) {
private ICompletionProposal createProposal(MethodNode method, IProgressMonitor progressMonitor) {
ContentAssistContext context = getContext();
IJavaProject project = getJavaContext().getProject();
ClassNode declaringClass = method.getDeclaringClass();
Expand Down

0 comments on commit dd823e2

Please sign in to comment.