Skip to content

Commit b0fb24a

Browse files
Protect against error for anonymous class in lambda in unopened class (#2720)
* Protect against error for anonymous class in lambda in unopened class - add isAnonymous check in MethodChecks.isDeclaredInInterface() - add isAnonymous check in RenameMethodProcessor.classesDeclareMethodName() - add isAnonymous check in RenameVirtualMethodProcessor.classesDeclareOverridingNativeMethod() - add test for refactor rename error in lambda with subtype - fixes #4704 Co-authored-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
1 parent 8adc178 commit b0fb24a

File tree

11 files changed

+133
-27
lines changed

11 files changed

+133
-27
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/rename/MethodChecks.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2018 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -109,15 +109,17 @@ public static IMethod isDeclaredInInterface(IMethod method, ITypeHierarchy hiera
109109
IType[] classes= hierarchy.getAllClasses();
110110
subMonitor.beginTask("", classes.length); //$NON-NLS-1$
111111
for (IType clazz : classes) {
112-
IType[] superinterfaces= null;
113-
if (clazz.equals(hierarchy.getType()))
114-
superinterfaces= hierarchy.getAllSuperInterfaces(clazz);
115-
else
116-
superinterfaces= clazz.newSupertypeHierarchy(Progress.subMonitor(subMonitor, 1)).getAllSuperInterfaces(clazz);
117-
for (IType superinterface : superinterfaces) {
118-
IMethod found= Checks.findSimilarMethod(method, superinterface);
119-
if (found != null && !found.equals(method))
120-
return found;
112+
if (!clazz.isAnonymous()) { // no need to look at an anonymous instance
113+
IType[] superinterfaces= null;
114+
if (clazz.equals(hierarchy.getType()))
115+
superinterfaces= hierarchy.getAllSuperInterfaces(clazz);
116+
else
117+
superinterfaces= clazz.newSupertypeHierarchy(Progress.subMonitor(subMonitor, 1)).getAllSuperInterfaces(clazz);
118+
for (IType superinterface : superinterfaces) {
119+
IMethod found= Checks.findSimilarMethod(method, superinterface);
120+
if (found != null && !found.equals(method))
121+
return found;
122+
}
121123
}
122124
subMonitor.worked(1);
123125
}

org.eclipse.jdt.core.manipulation/refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameMethodProcessor.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -701,15 +701,20 @@ private static IMethod[] classesDeclareMethodName(ITypeHierarchy hier, List<ITyp
701701
boolean isMethodPrivate= JdtFlags.isPrivate(method);
702702

703703
for (IType clazz : classes) {
704-
boolean isSubclass= subtypes.contains(clazz);
705-
for (IMethod m : clazz.getMethods()) {
706-
IMethod foundMethod= Checks.findMethod(newName, parameterCount, false, new IMethod[]{m});
707-
if (foundMethod == null)
708-
continue;
709-
if (isSubclass
710-
|| type.equals(clazz)
711-
|| (!isMethodPrivate && !JdtFlags.isPrivate(m))) {
712-
result.add(foundMethod);
704+
// try and find the method declaration -.don't look at anonymous types as
705+
// we will find the method in the real type and there is a current bug scenario where accessing
706+
// may cause a model exception when the class is created in a lambda expression and the file is unopen
707+
if (!clazz.isAnonymous()) {
708+
boolean isSubclass= subtypes.contains(clazz);
709+
for (IMethod m : clazz.getMethods()) {
710+
IMethod foundMethod= Checks.findMethod(newName, parameterCount, false, new IMethod[]{m});
711+
if (foundMethod == null)
712+
continue;
713+
if (isSubclass
714+
|| type.equals(clazz)
715+
|| (!isMethodPrivate && !JdtFlags.isPrivate(m))) {
716+
result.add(foundMethod);
717+
}
713718
}
714719
}
715720
}

org.eclipse.jdt.core.manipulation/refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameVirtualMethodProcessor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,12 @@ private Set<IType> getRelatedTypes() {
247247
//---- Class checks -------------------------------------
248248

249249
private boolean classesDeclareOverridingNativeMethod(IType[] classes) throws CoreException {
250-
for (IType type : classes) {
251-
for (IMethod method : type.getMethods()) {
252-
if ((!method.equals(getMethod())) && (JdtFlags.isNative(method)) && (null != Checks.findSimilarMethod(getMethod(), new IMethod[]{method}))) {
253-
return true;
250+
for (IType type : classes) {
251+
if (!type.isAnonymous()) {
252+
for (IMethod method : type.getMethods()) {
253+
if ((!method.equals(getMethod())) && (JdtFlags.isNative(method)) && (null != Checks.findSimilarMethod(getMethod(), new IMethod[]{method}))) {
254+
return true;
255+
}
254256
}
255257
}
256258
}

org.eclipse.jdt.ui.tests.refactoring/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.jdt.ui.tests.refactoring
33
Bundle-ManifestVersion: 2
44
Bundle-Name: %Plugin.name
55
Bundle-SymbolicName: org.eclipse.jdt.ui.tests.refactoring; singleton:=true
6-
Bundle-Version: 3.16.300.qualifier
6+
Bundle-Version: 3.16.400.qualifier
77
Bundle-Activator: org.eclipse.jdt.ui.tests.refactoring.infra.RefactoringTestPlugin
88
Bundle-ActivationPolicy: lazy
99
Bundle-Vendor: %Plugin.providerName

org.eclipse.jdt.ui.tests.refactoring/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</parent>
2020
<groupId>org.eclipse.jdt</groupId>
2121
<artifactId>org.eclipse.jdt.ui.tests.refactoring</artifactId>
22-
<version>3.16.300-SNAPSHOT</version>
22+
<version>3.16.400-SNAPSHOT</version>
2323
<packaging>eclipse-test-plugin</packaging>
2424
<properties>
2525
<testSuite>${project.artifactId}</testSuite>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package p;
2+
//renaming A.m to k
3+
abstract class A {
4+
void m() {}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package p;
2+
class B {
3+
public void t() {
4+
Runnable r = () -> new A() {};
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package p;
2+
//renaming A.m to k
3+
abstract class A {
4+
void k() {}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package p;
2+
class B {
3+
public void t() {
4+
Runnable r = () -> new A() {};
5+
}
6+
}

org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/AllRefactoringTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2021 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -84,6 +84,7 @@
8484
RenamePrivateMethodTests.class,
8585
RenameStaticMethodTests.class,
8686
RenameParametersTests.class,
87+
RenameMethodWithSubtypeInLambdaTests.class,
8788
MoveInstanceMethodTests.class,
8889
MoveInstanceMethodTests1d8.class,
8990
IntroduceIndirectionTests.class,

0 commit comments

Comments
 (0)