diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests_1_5.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests_1_5.java index 52a19b0f8a0..c38c12799c4 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests_1_5.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests_1_5.java @@ -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; @@ -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 { @@ -14910,4 +14910,50 @@ public static GH969List empty() { + "emptyList[METHOD_REF]{emptyList(), LGH969;, ()LGH969List;, 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); + } +} } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java index e322e384a1c..21656c23c4e 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java @@ -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; @@ -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++) {