Automatically remove explicit package declarations with import statements in Java

前端 未结 8 1047
不思量自难忘°
不思量自难忘° 2021-01-07 19:46

I have a project created by others that includes thousands of class files and has the package names explicitly typed out for every reference to any of their classes. It look

相关标签:
8条回答
  • 2021-01-07 20:05

    For a single type, eclipse offers the 'Add import' action (Shift+Ctrl+M). It does exactly what you want - with the big, big limitation: you have to place the cursor on a type and it will only affect that 'selected' occurrence.

    But maybe this action can be used in a scripted/global method. A JDT plugin could crawl through the AST and call this action on every type it finds.

    0 讨论(0)
  • 2021-01-07 20:15

    I don't know a tool for this use case, but I had to do something similar a few month ago.

    • Write a script or do a search replace with regex to get rid of the explicitly typed package prefixes.

    • Than let eclipse do the rest using "organize imports". Ctrl-1

    Hint: to avoid ambiguities, setup the classpath with no more than the required libs. For sround about 800 classes I was done in 2 hours.

    • Or get someone who deserved it to do this job.

    EDIT: You should know that in Prefeneces/Java/Editor/Save Actions, Organize imports can be configured as save action.

    0 讨论(0)
提交回复
热议问题