Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FieldLocator and MethodLocator to support local/anonymous classes #3314

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -15499,6 +15499,100 @@ public void testModuleConflictGh723() throws Exception {
}
}

/**
* issue 3308: SearchEngine.searchDeclarationsOfSentMessages() does not support local or anonymous classes
* @see "https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3308"
*/
public void testIssue3308() throws CoreException {
this.workingCopies = new ICompilationUnit[1];
String src= """
package issue3308;
public class Test {
public class BaseTargetClass {
}

public class OriginalClass {
public int data = 60;

public void memberMethod() {
}

public class NestedOriginalClass extends BaseTargetClass {
void setup() {
new BaseTargetClass() {
int j = 1;

void methodToBePulledUp() {
j = 2;
methodHelper();
}

void methodHelper() {
System.out.println("Helper Method in Anonymous Class: " + data);
}
};
}
}
}
}
""";

this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/issue3308/Test.java", src);
IType type = (IType) this.workingCopies[0].getElementAt(src.indexOf("new BaseTargetClass"));
IMethod method = type.getMethod("methodToBePulledUp", new String[] {});
new SearchEngine(this.workingCopies).searchDeclarationsOfSentMessages(method, this.resultCollector, null);
assertSearchResults(
"src/issue3308/Test.java void void issue3308.Test$OriginalClass$NestedOriginalClass.setup():<anonymous>#1.methodHelper() [methodHelper()] EXACT_MATCH"
);
}

/**
* issue 3308: SearchEngine.searchDeclarationsOfSentMessages() does not support local or anonymous classes
* @see "https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3308"
*/
public void testIssue3308b() throws CoreException {
this.workingCopies = new ICompilationUnit[1];
String src= """
package issue3308;
public class Test {
public class BaseTargetClass {
}

public class OriginalClass {
public int data = 60;

public void memberMethod() {
}

public class NestedOriginalClass extends BaseTargetClass {
void setup() {
new BaseTargetClass() {
int j = 1;

void methodToBePulledUp() {
j = 2;
methodHelper();
}

void methodHelper() {
System.out.println("Helper Method in Anonymous Class: " + data);
}
};
}
}
}
}
""";

this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/issue3308b/Test.java", src);
IType type = (IType) this.workingCopies[0].getElementAt(src.indexOf("new BaseTargetClass"));
IMethod method = type.getMethod("methodToBePulledUp", new String[] {});
new SearchEngine(this.workingCopies).searchDeclarationsOfAccessedFields(method, this.resultCollector, null);
assertSearchResults(
"src/issue3308b/Test.java void issue3308b.Test$OriginalClass$NestedOriginalClass.setup():<anonymous>#1.j [j] EXACT_MATCH"
);
}

private static String toString(char[][] modules) {
StringBuilder sb = new StringBuilder();
for (char[] m : modules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -280,6 +281,27 @@ protected void reportDeclaration(FieldBinding fieldBinding, MatchLocator locator

ReferenceBinding declaringClass = fieldBinding.declaringClass;
IType type = locator.lookupType(declaringClass);
if (type == null) {
if (declaringClass instanceof LocalTypeBinding) {
ReferenceBinding refBinding= declaringClass;
while (refBinding instanceof LocalTypeBinding localBinding) {
MethodBinding enclosingBinding= localBinding.enclosingMethod;
refBinding= enclosingBinding.declaringClass;
}
type= locator.lookupType(refBinding);
if (type != null) {
if (type.getTypeRoot() instanceof ICompilationUnit cu) {
FieldDeclaration fieldDecl= fieldBinding.sourceField();
if (fieldDecl != null) {
IJavaElement element= cu.getElementAt(fieldDecl.sourceStart());
if (element instanceof IField) {
type= ((IField) element).getDeclaringType();
}
}
}
}
}
}
if (type == null) return; // case of a secondary type

char[] bindingName = fieldBinding.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -664,7 +665,26 @@ protected int referenceType() {
protected void reportDeclaration(MethodBinding methodBinding, MatchLocator locator, SimpleSet knownMethods) throws CoreException {
ReferenceBinding declaringClass = methodBinding.declaringClass;
IType type = locator.lookupType(declaringClass);
if (type == null) return; // case of a secondary type
if (type == null) {
if (declaringClass instanceof LocalTypeBinding) {
ReferenceBinding refBinding= declaringClass;
while (refBinding instanceof LocalTypeBinding localBinding) {
MethodBinding enclosingBinding= localBinding.enclosingMethod;
refBinding= enclosingBinding.declaringClass;
}
type= locator.lookupType(refBinding);
if (type != null) {
if (type.getTypeRoot() instanceof ICompilationUnit cu) {
IJavaElement element= cu.getElementAt(methodBinding.sourceStart());
if (element instanceof IMethod) {
type= ((IMethod) element).getDeclaringType();
}
}
}
}
}
if (type == null)
return; // case of a secondary type

// Report match for binary
if (type.isBinary()) {
Expand Down
Loading