2

Could you use the javaparser library to: Rename imported classes, methods and fields? For example:

package org.example;

import org.example.Test;

public class Example {
    public void example() {
        Test t = new Test();
        t.someMethod();
        t.randomField;
   }
}

to

package org.example;

import org.example.Test123;

public class Example {
    public void example() {
        Test123t = new TestTest123);
        t.someOtherMethod();
        t.newName;
   }
}

Edit to add:
Method overload also is taken into consideration, for example:

t.someOverloadedMethod(1, 2, false, null) -> t.name1(1, 2, false, null)
t.someOverloadedMethod() -> t.name2()
Ruiqi Li
  • 187
  • 14

1 Answers1

0

If JavaParser doesn't build a symbol table defining variable scopes, you can't do this reliably. The problem is that you may attempt to rename a variable X that appears in more than one scope. Your example of method overloading is a special case of this.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • I only want to remap imported stuff, not local vars – Ruiqi Li Apr 23 '21 at 14:05
  • OK, so you want to rename some entity named X mentioned in an import. All you see in the code is ...X... How do you know that particular X is the one mentioned in the import? You can't do this without scopes and symbol tables. – Ira Baxter Apr 23 '21 at 15:13
  • You could just hammer the code by changing all X's to whatever you want it renamed to, if you don't mind repairing the damages made by not accounting for scopes. If the name X is relatively unique ("SetASEEncyptionKey") this will probably work pretty well. If the name X is rather generic (e.g., a custom "get" routine added to a special imported class), this will likely faily badly on a big Java code. Back to my main point: you can't do this reliably without symbol tables and scopes. And I don't think JavaParser offers those. I could be wrong. – Ira Baxter Apr 23 '21 at 15:15
  • shame, is there a library that allows you to manipulate java in this way? – Ruiqi Li Apr 23 '21 at 16:39
  • I think you can bend the Java Compiler's internal APIs at the price of learning your way around inside the compiler and then implementing what you want. Might be awkward because JavaC doesn't want to be a renaming tool. My company (Semantic Designs) has a Java front end that includes name resolution in which this would be pretty easy to implement. But your easiest route might be the Eclipse-based Java Refactoring tool that will do Renames that might do exactly what you want. (I didn't provide this initially because I thought you want to use JavaParser specifically) – Ira Baxter Apr 23 '21 at 17:55
  • Could you get me a link for where to find that? – Ruiqi Li Apr 23 '21 at 19:49
  • https://www.tutorialspoint.com/eclipse/eclipse_refactoring.htm – Ira Baxter Apr 23 '21 at 21:35
  • Well I want to do this in-code, not with an UI – Ruiqi Li Apr 24 '21 at 00:38
  • You can dig around in the Eclipse Java Refactoring tool to find the APIs it uses to implement Rename, and then pull out what you want. You can also check this SO answer for other libraries: https://stackoverflow.com/a/7293821/120163 – Ira Baxter Apr 24 '21 at 15:37