views:

329

answers:

5

Hello,

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 looks like the code was reverse engineered. Is there a good tool for Java that refactors the code so that the explicitly typed package prefixes are removed from class references and moved into import statements.

Thank you in advance.

EDIT:

I think an example will help. I want to have the imports at the top, and I don't care how many imports there are.

javax.swing.JButton button1 = new javax.swing.JButton();

Imagine the code above but absolutely everywhere in thousands upon thousands of lines of code amongst thousands of class files. I would like to be able to remove all of the prefixes and just have a nice import javax.swing.JButton; at the top of each class file.

+2  A: 

This thread says:

select the package explorer view, right click on your project, choose source then organise imports. Bobs your uncle - all unwanted imports are removed

To make it better formatted:

Right click project > Source > Organize imports

Now, what remains, is to find a way to strip the fully-qualified names from the code. You may think of some regular expression. Take a look at this library - it seems helpful. This article should also be useful.

Bozho
This will not change fully qualified type declarations (e.g. `java.awt.Font foo`) to unqualified declarations (e.g. `Font foo`) with import statements. At least, this is the case in Eclipse 3.5.
McDowell
yes, I updated my answer with the next (previous, actually) step towards that goal
Bozho
Interesting library and article... I will have to look into these to see how easy I can apply it to my project, thank you.
AlbertoPL
+5  A: 

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.

stacker
Yes this is what I thought I would have to do. Only 2 hours once the regex is written... sounds promising and may be doing that. Thank you.
AlbertoPL
+1 for the last bullet point.
Stephen C
It might not be as simple as removing all explicit package references; if there are ambiguities (same class name) then those explicit references need to remain.
Ken Liu
Using eclipse's save actions, at least the order (and visibility) can be changed
stacker
+2  A: 

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.

Andreas_D
I will have to look into scripting that, however I don't know if it'd be easier just to write a script that finds and replaces using a regex. Still, if I could find a plugin that's already written I will certainly try using it. Thank you.
AlbertoPL
+1  A: 

IntelliJ Idea has tools to do this on a per-file basis. You can probably also do it in bulk, but I don't know how.

Try out a 30 day evaluation and you'll probably be pleased by more than the import cleanup features.

Darron
A: 

You probably need to replace all javax.swing.. Then do organize imports.

fastcodejava