A simple way to create static imports for constants in existing code?

前端 未结 9 853
心在旅途
心在旅途 2021-01-30 21:17

If I have a constant BAR in Foo, which I will use in a class C I\'ll have to write

Object o = Foo.BAR + \"...\";

which I can use Ctrl-Shift-M i

9条回答
  •  梦如初夏
    2021-01-30 21:44

    Well something which pops in as a solution. I would recommend using a simple groovy script. Basically the semantics are related to parsing the import statements and the files they refer to. Some basic steps could be like

    1. Recurse each file inside directory.Check whether the file extension is .java
    2. Match any line which starts with import
    3. For each such line open the relevant source file(you already know the path from the import statement).
    4. Match the field/method declaration for a static modifier using reflection
    5. Add static after the import statement for matches found in step 5.

    If i get some time i will try and post a small groovy script for doing the same.

提交回复
热议问题