Rename id in java program

I need to programmatically rename identifiers within a given area, for example, a method in a java program. For example, given the following java function:

public void doSomething(){
   int x = 10;
   int y = x * 2;
   int z = x + y;
}

after renaming the variables (from x to a, y to b and from z to c) I should get the following function:

public void doSomething(){
   int a = 10;
   int b = a * 2;
   int c = a + b;
}

How can I programmatically implement such a renaming of identifiers and their references?

I studied the Eclipse AST and Java model. In any case, I must implement a search for all occurrences of any given identifier, and then replace them. I am wondering if there is a better way to do this (how does the Eclipse Refactoring user interface support such variable renaming)? Or should I look in the Language Toolkit (org.eclipse.ltk.core.refactoring)? Any tutorial, sample code, or suggestion?

, .

+3
4

, .

RenameSupport renameSupport = RenameSupport.create(field, newName, RenameSupport.UPDATE_REFERENCES);
renameSupport.perform(workbench.getShell(), workbench);

. , ? , .

0

, (.. ) , . , :

  • API Java java.lang.reflect, , , .
  • . API org.eclipse.jdt.core, , . unit.
  • . - : Old == > OldRenamed. , .
  • Eclipse org.eclipse.jdt.ui.refactoring API . Eclipse Rename API.

.

+1

, . , , , ( ).

, , - , , A (, ), (, , ..), B (, ).

, , . x, y z . , .

-1
  • rename

Eclipse ,

-3
source

Source: https://habr.com/ru/post/1760176/


All Articles