How to rename a class?

How to rename / move a Java class? Does the PsiClass contain a method for renaming?

For example, the VirtualFile class contains the rename, move methods.

5 comments
Comment actions Permalink

Find the name element, create a new name element, replace. Be sure to do the same for any constructors on the class, and any references to the class.

Alternatively, you could fire a "rename" refactoring by constructing a RenameRefactoringHandler. That's probably your safest bet.

In general PSI classes don't have much direct support for mutating them in their APIs.

--Dave Griffith

0
Comment actions Permalink

see PsiNamedElement.setName(). For classes it also renames file if needed.

0
Comment actions Permalink

Wow, how did I miss that?

--Dave Griffith

0
Comment actions Permalink

I can change class name but how to move the class to another package? Remove from the current package and add to another?

0
Comment actions Permalink

Hey, that's the whole refactoring! You want the references to the class to updated too, don't you? To invoke move programmatically see RefactoringActionHandlerFactory.createMoveHandler()

0

Please sign in to leave a comment.