Skip to content

Commit 9f6d161

Browse files
committed
More error/NPE fixes in completion
- handle completing package-qualified method parameter type - do not suggest overriding existing or declaring new methods in annotation type declarations - when building override method completions, if the `IMethod` of a given method binding cannot be found, use the parameter names from the bindings instead - prevent NPE when attempting to suggest completion for a method with an incomplete parameter type Signed-off-by: David Thompson <[email protected]>
1 parent ee0beeb commit 9f6d161

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionEngine.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,9 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
805805
typeDeclBinding = ((AnonymousClassDeclaration)simpleType.getParent().getParent()).resolveBinding();
806806
}
807807

808-
findOverridableMethods(typeDeclBinding, this.javaProject, context);
808+
if (!typeDeclBinding.isAnnotation()) {
809+
findOverridableMethods(typeDeclBinding, this.javaProject, context);
810+
}
809811
suggestClassDeclarationLikeKeywords();
810812
suggestTypeKeywords(true);
811813
suggestModifierKeywords(bodyDeclaration.getModifiers());
@@ -829,7 +831,7 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
829831
})
830832
.map(this::toProposal).forEach(this.requestor::accept);
831833
}
832-
if (!this.requestor.isIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION)) {
834+
if (!this.requestor.isIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION) && !typeDeclBinding.isAnnotation()) {
833835
int cursorStart = this.offset - this.prefix.length() - 1;
834836
while (cursorStart > 0 && Character.isWhitespace(this.textContent.charAt(cursorStart))) {
835837
cursorStart--;
@@ -1172,7 +1174,7 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
11721174

11731175
suggestDefaultCompletions = false;
11741176
} else if (qualifiedNameBinding instanceof IPackageBinding qualifierPackageBinding) {
1175-
if (!qualifierPackageBinding.isRecovered()) {
1177+
if (!qualifierPackageBinding.isRecovered() || (qualifiedName.getParent() instanceof Type && qualifiedName.getParent().getParent() instanceof VariableDeclaration)) {
11761178
// start of a known package
11771179
suggestPackages(null);
11781180
// suggests types in the package
@@ -3532,7 +3534,11 @@ private void findOverridableMethods0(ITypeBinding currentType, ITypeBinding type
35323534
proposal.setSignature(SignatureUtils.getSignatureChar(method));
35333535

35343536
try {
3535-
proposal.setParameterNames(Stream.of(((IMethod)method.getJavaElement()).getParameterNames()).map(name -> name.toCharArray()).toArray(char[][]::new));
3537+
if (method.getJavaElement() != null) {
3538+
proposal.setParameterNames(Stream.of(((IMethod)method.getJavaElement()).getParameterNames()).map(name -> name.toCharArray()).toArray(char[][]::new));
3539+
} else {
3540+
proposal.setParameterNames(Stream.of(method.getParameterNames()).map(name -> name.toCharArray()).toArray(char[][]::new));
3541+
}
35363542
} catch (JavaModelException e) {
35373543
proposal.setParameterNames(Stream.of(method.getParameterNames()).map(name -> name.toCharArray()).toArray(char[][]::new));
35383544
}
@@ -3794,10 +3800,10 @@ public void acceptTypeNameMatch(TypeNameMatch match) {
37943800
.map(t -> processMembers(t, searchEngine, scope))
37953801
.flatMap(Function.identity()));
37963802
}
3797-
3803+
37983804
private String getSignature(IMethodBinding method) {
37993805
return method.getName() + '(' +
3800-
Arrays.stream(method.getParameterTypes()).map(ITypeBinding::getName).collect(Collectors.joining(","))
3806+
Arrays.stream(method.getParameterTypes()).map(paramType -> paramType != null ? paramType.getName() : "Object").collect(Collectors.joining(","))
38013807
+ ')';
38023808
}
38033809

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionEngineBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ static void createMethod(IMethodBinding methodBinding, StringBuilder completion,
7373
ITypeBinding[] parameterTypes = methodBinding.getParameterTypes();
7474
String[] parameterNames;
7575
try {
76-
parameterNames = ((IMethod)methodBinding.getJavaElement()).getParameterNames();
76+
if (methodBinding.getJavaElement() != null) {
77+
parameterNames = ((IMethod)methodBinding.getJavaElement()).getParameterNames();
78+
} else {
79+
parameterNames = methodBinding.getParameterNames();
80+
}
7781
} catch (JavaModelException e) {
7882
parameterNames = methodBinding.getParameterNames();
7983
}

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/SignatureUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ public static char[] getSignatureChar(ITypeBinding typeBinding) {
123123
public static String getSignatureForTypeKey(String key) {
124124
return key.replace('/', '.').replaceFirst("(?<=\\.|L)[_$A-Za-z][_$A-Za-z0-9]*~", "");
125125
}
126-
126+
127127
/**
128128
* Returns the signature of the given method binding as a character array.
129129
*
130130
* @param methodBinding the method binding to get the signature of
131131
* @return the signature of the given method binding as a character array
132132
*/
133133
public static char[] getSignatureChar(IMethodBinding methodBinding) {
134-
return SignatureUtils.getSignatureForMethodKey(methodBinding.getKey()).toCharArray();
134+
return getSignatureForMethodKey(methodBinding.getKey()).toCharArray();
135135
}
136136

137137
/**

0 commit comments

Comments
 (0)