Skip to content

Commit

Permalink
Prevent rename of method to a static imported method (#1402)
Browse files Browse the repository at this point in the history
- modify RenameMethodProcessor and Checks classes to check for
  a static import collision
- add new tests to RenamePrivateMethodTests
- fixes #1361
  • Loading branch information
jjohnstn committed May 11, 2024
1 parent fc3838c commit 9fa4117
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
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 @@ -36,7 +36,9 @@
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;

import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModelMarker;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -50,13 +52,16 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.VariableDeclaration;
Expand All @@ -67,6 +72,7 @@
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
import org.eclipse.jdt.internal.corext.refactoring.util.JavaStatusContext;
Expand Down Expand Up @@ -908,4 +914,46 @@ public static boolean isDeclaredIn(VariableDeclaration tempDeclaration, Class<?
return false;
return true;
}

private static CompilationUnit convertICUtoCU(ICompilationUnit compilationUnit) {
ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(compilationUnit);
parser.setResolveBindings(true);

return (CompilationUnit) parser.createAST(null);
}

public static IImportDeclaration findMatchingStaticMethodImport(IMethod method, String newName) {
if (method.getCompilationUnit() != null) {
try {
IImportDeclaration[] imports=method.getCompilationUnit().getImports();
CompilationUnit cu= null;
for (IImportDeclaration importDecl : imports) {
if (Flags.isStatic(importDecl.getFlags())) {
String importName= importDecl.getElementName();
String[] fragments= importName.split("\\."); //$NON-NLS-1$
if (fragments.length > 0 && newName.equals(fragments[fragments.length - 1])) {
cu= convertICUtoCU(method.getCompilationUnit());
if (cu != null) {
List<ImportDeclaration> importList= cu.imports();
for (ImportDeclaration importListDecl : importList) {
if (importListDecl.getName().getFullyQualifiedName().equals(importName)) {
IBinding binding= importListDecl.resolveBinding();
if (binding instanceof IMethodBinding) {
return importDecl;
}
}
}
}
return null;
}
}
}
} catch (JavaModelException e) {
// do nothing
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,10 @@ public final class RefactoringCoreMessages extends NLS {

public static String RenameMethodRefactoring_not_in_model;

public static String RenameMethodRefactoring_overrides_static_name;

public static String RenameMethodRefactoring_overrides_static_name2;

public static String RenameMethodRefactoring_same_name;

public static String RenameMethodRefactoring_same_name2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ RenameMethodRefactoring_no_native=Renaming native methods will cause an unsatisf
RenameMethodRefactoring_no_native_1=Related method ''{0}'' (declared in ''{1}'') is native. Renaming will cause an UnsatisfiedLinkError on runtime.
RenameMethodRefactoring_no_read_only=Related method ''{0}'' (declared in ''{1}'') is read-only. Refactoring cannot be performed.
RenameMethodRefactoring_not_in_model=Related method ''{0}'' (declared in ''{1}'') does not exist in the model.
RenameMethodRefactoring_same_name=This name already exists.
RenameMethodRefactoring_overrides_static_name=This name collides with a static import name.
RenameMethodRefactoring_overrides_static_name2=The static import ''{0}'' in type ''{1}'' already has the assigned name.
enameMethodRefactoring_same_name=This name already exists.
RenameMethodRefactoring_same_name2=The method ''{0}'' in type ''{1}'' already has the assigned name.
RenameMethodRefactoring_update_occurrence=Update method reference
RenameMethodRefactoring_update_declaration=Update method declaration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 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 @@ -232,6 +232,13 @@ public final RefactoringStatus checkNewElementName(String newName) {
? Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_same_name2, new String[] { BasicElementLabels.getJavaElementName(newName), getDeclaringTypeLabel() } )
: RefactoringCoreMessages.RenameMethodRefactoring_same_name,
JavaStatusContext.create(fMethod));

if (Checks.findMatchingStaticMethodImport(fMethod, newName) != null) {
status.addFatalError(fIsComposite
? Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_overrides_static_name2, new String[] { BasicElementLabels.getJavaElementName(newName), getDeclaringTypeLabel() } )
: RefactoringCoreMessages.RenameMethodRefactoring_overrides_static_name,
JavaStatusContext.create(Checks.findMatchingStaticMethodImport(fMethod, newName)));
}
return status;
}
private String getDeclaringTypeLabel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package p;

import static java.lang.String.valueOf;

public class A {
private String foo(int x) {
return valueOf(x);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2020 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 @@ -131,6 +131,11 @@ public void testFail5() throws Exception{
helper1();
}

@Test
public void testIssue1361() throws Exception{
helper1_0("foo", "valueOf", new String[0]);
}

@Test
public void test0() throws Exception{
helper2();
Expand Down

0 comments on commit 9fa4117

Please sign in to comment.