diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugsTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugsTests.java index 4e2e33787ab..af709b59074 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugsTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugsTests.java @@ -15689,6 +15689,32 @@ void methodHelper() { ); } +// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/5262 +// [Search] Search for references to local declared inside static initializer block brings up nothing +public void testIssue5262() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public class X {\n" + + " static {\n" + + " int /*here*/x = 0;\n" + + " x++;\n" + + " System.out.println(x);\n" + + " }\n" + + "}\n"); + + String str = this.workingCopies[0].getSource(); + String selection = "/*here*/x"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + ILocalVariable local = (ILocalVariable) elements[0]; + search(local, REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java X.static {} [x] EXACT_MATCH\n" + + "src/X.java X.static {} [x] EXACT_MATCH"); +} + private static String toString(char[][] modules) { StringBuilder sb = new StringBuilder(); for (char[] m : modules) { diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java index 14cd6931da3..9f42eee0596 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java @@ -89,7 +89,7 @@ protected void matchReportReference(ASTNode reference, IJavaElement element, Bin } @Override protected int matchContainer() { - return METHOD_CONTAINER; + return this.pattern.focus instanceof LocalVariable local && local.getParent() instanceof org.eclipse.jdt.internal.core.Initializer ? FIELD_CONTAINER : METHOD_CONTAINER; } protected int matchLocalVariable(LocalVariableBinding variable, boolean matchName) { if (variable == null) return INACCURATE_MATCH;