Skip to content

Commit

Permalink
Bug 559172 - Null pointer exception in JavaAllCompletionProposalCompu…
Browse files Browse the repository at this point in the history
…ter (eclipse-jdt#2598)

Do not try to propose a method when completing within its own annotation.

Fixes eclipse-jdt#2597
  • Loading branch information
stephan-herrmann authored Jun 18, 2024
1 parent ef433b8 commit 0d59970
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import java.util.Hashtable;

import junit.framework.Test;

import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -26,6 +24,8 @@
import org.eclipse.jdt.internal.codeassist.CompletionEngine;
import org.eclipse.jdt.internal.codeassist.RelevanceConstants;

import junit.framework.Test;

@SuppressWarnings({"rawtypes", "unchecked"})
public class CompletionTests_1_5 extends AbstractJavaModelCompletionTests {
static {
Expand Down Expand Up @@ -14910,4 +14910,50 @@ public static <T> GH969List<T> empty() {
+ "emptyList[METHOD_REF]{emptyList(), LGH969;, <T:Ljava.lang.Object;>()LGH969List<TT;>;, null, null, emptyList, null, replace[98, 98], token[98, 98], " + symbolRelevance + "}",
requestor.getResults());
}
public void testGH2597() throws JavaModelException {
this.workingCopies = new ICompilationUnit[2];
this.workingCopies[0] = getWorkingCopy("/Completion/src/jdbi/Dao.java", """
package jdbi;

public interface Dao {

String LABELS_BOOK_TITLES = "labels_book_titles";
String LABELS_CHAPTERS = "labels_chapters";
String TRAIN_ARTICLE_DATA = "training_article_data";
String TRAIN_ARTICLE_DATA_CLEAN = "training_article_data_clean";
String TRAIN_ARTICLE_LABELS = "training_article_labels";

@SqlUpdate("update "+TRAIN) //***** THIS IS WHERE THE ERROR HAPPENS, WHEN TRYING TO AUTO COMPLETE THE PARTIAL WORD.
void resetBookTitleTrainingData();
}

interface DaoExtended extends Dao { }

@interface SqlUpdate {
String value();
}
""");
// try to provoke proposing "resetBookTitleTrainingData()":
IJavaProject javaProject = getJavaProject("Completion");
javaProject.setOption(JavaCore.CODEASSIST_SUBWORD_MATCH, JavaCore.ENABLED);
try {
CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, true, true, true, true, true);
requestor.allowAllRequiredProposals();
String str = this.workingCopies[0].getSource();
String completeBehind = "+TRAIN";
int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
int relevance = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_UNQUALIFIED + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE;
assertEquals(
"TRAIN_ARTICLE_DATA[FIELD_REF]{TRAIN_ARTICLE_DATA, Ljdbi.Dao;, Ljava.lang.String;, null, null, TRAIN_ARTICLE_DATA, null, replace[342, 347], token[342, 347], "
+ relevance + "}\n" +
"TRAIN_ARTICLE_DATA_CLEAN[FIELD_REF]{TRAIN_ARTICLE_DATA_CLEAN, Ljdbi.Dao;, Ljava.lang.String;, null, null, TRAIN_ARTICLE_DATA_CLEAN, null, replace[342, 347], token[342, 347], "
+ relevance + "}\n" +
"TRAIN_ARTICLE_LABELS[FIELD_REF]{TRAIN_ARTICLE_LABELS, Ljdbi.Dao;, Ljava.lang.String;, null, null, TRAIN_ARTICLE_LABELS, null, replace[342, 347], token[342, 347], "
+ relevance + "}",
requestor.getResults());
} finally {
javaProject.setOption(JavaCore.CODEASSIST_SUBWORD_MATCH, JavaCore.DISABLED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9488,6 +9488,8 @@ private void findLocalMethods(
next : for (int f = methods.length; --f >= 0;) {
MethodBinding method = methods[f];

if (completingInPreamble(method))
continue next;
if (method.isSynthetic()) continue next;

if (method.isDefaultAbstract()) continue next;
Expand Down Expand Up @@ -9826,6 +9828,23 @@ else if (this.source != null
methodsFound.addAll(newMethodsFound);
}

protected boolean completingInPreamble(MethodBinding method) {
// don't propose a method within its own annotation, detected via source positions:
AbstractMethodDeclaration sourceMethod = method.sourceMethod();
if (sourceMethod == null)
return false;
int completionPosition = this.actualCompletionPosition + 1; // field is initialized as completionPosition-1
if (completionPosition < sourceMethod.declarationSourceStart)
return false;
if (completionPosition >= sourceMethod.bodyStart)
return false;
if (sourceMethod.javadoc != null) {
if (completionPosition > sourceMethod.javadoc.sourceStart && completionPosition < sourceMethod.javadoc.sourceEnd)
return false; // within javadoc, doesn't count as 'preamble'
}
return true;
}

private char[] capitalize(char[] name) {
char[] result = new char[name.length];
for (int i = 0; i < name.length; i++) {
Expand Down

0 comments on commit 0d59970

Please sign in to comment.