Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Refactor -> Rename programmatically
Refactor -> Rename programmatically [message #1811031] Sat, 24 August 2019 14:06 Go to next message
Kai S. is currently offline Kai S.Friend
Messages: 20
Registered: June 2019
Junior Member
Hello,

I am trying to do a rename refactoring for type declarations programmatically. I am currently trying to do so with the following method:
public static void renameType(IType type, String newName) {
	final RefactoringContribution contribution = RefactoringCore
			.getRefactoringContribution(IJavaRefactorings.RENAME_TYPE);
	final RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) contribution.createDescriptor();
	descriptor.setProject(type.getResource().getProject().getName());
	descriptor.setJavaElement(type);
	descriptor.setNewName(newName);
		final RefactoringStatus status = new RefactoringStatus();
	try {
		final Refactoring refactoring = descriptor.createRefactoring(status);
		final IProgressMonitor monitor = new NullProgressMonitor();
		refactoring.checkInitialConditions(monitor);
		refactoring.checkFinalConditions(monitor);
		final Change change = refactoring.createChange(monitor);
		change.perform(monitor);
	} catch (final Exception e) {
		e.printStackTrace();
	}
}


I then call it simply like this:
IType javaTypeToRename = ...
renameType(javaTypetoRename, "ChangedTypeName");


The renaming of the type declaration including the .java-file name works fine. But references to this type like fields in other classes etc. do not get renamed accordingly.
Does anyone maybe have an idea on how to fix this behavior to not break the project by not renaming the references?

Thanks a lot :)
Re: Refactor -> Rename programmatically [message #1811036 is a reply to message #1811031] Sat, 24 August 2019 23:07 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Has the project been built / indexed in Eclipse? To test: does manually searching for type references produce the expected results?
Re: Refactor -> Rename programmatically [message #1811041 is a reply to message #1811036] Sun, 25 August 2019 10:13 Go to previous message
Kai S. is currently offline Kai S.Friend
Messages: 20
Registered: June 2019
Junior Member
Hello,

yes, searching for type references shows all expected results. So everything looks as it should, except the references just not being updated accordingly.
Previous Topic:How to clear Target Definition cache?
Next Topic:Installing Eclipse Java 2019-06 for a virtual machine
Goto Forum:
  


Current Time: Wed Apr 24 16:15:28 GMT 2024

Powered by FUDForum. Page generated in 0.03064 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top