tags:

views:

78

answers:

3

Is there a tool/library that allows me to import the java packages automatically outside the IDE?

For instrance if I type in notepad something like:

JFrame f = new JFrame();

Then run this magic tool, and then have automatically written as:

import javax.swing.JFrame 

....

 JFrame f = new JFrame();

Is there something like that? This is what comes to my mind as sample usage:

import java.io.File;


public class TesteImport {

    public static void main(String[] args) {
     AutoImport autoImport = new AutoImport();
     File clazz = new File("SampleClazz.java");
     autoImport.setImportClass(clazz);
     autoImport.addLib("LibA.jar");
     autoImport.addLib("LibB.jar");
     autoImport.importAll();
    }

}
A: 

In Eclipse, you have Crtl-Shift-O (or Command-Shift-O on a mac). Perhaps you could dig into the Eclipse source code (open source) and find the Java code that drives this feature and re-use it. Good luck!

Asaph
This looks related: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jdt.ui/core%20extension/org/eclipse/jdt/internal/corext/codemanipulation/ImportReferencesCollector.java?view=markup
Tim
He did specifically say "Outside of the IDE"
Bill K
@Bill K: I already addressed that in my response by suggesting that he look at the source code that makes this feature work in the IDE and do the same thing.
Asaph
A: 

What you are asking for is a tool that will modify your source code outside of the IDE. That's really not a good idea--codegen always ends up sucking, no matter how cool and limited it seems at first.

The only decent case for code generation is where the programmer NEVER sees the intermediate version--this happens with C preprocessor--it makes an intermediate pre-processed version that you never see.

That said, what you might want is something like Groovy. IIRC, groovy allows something like "import *" for import everything.

The thing is, Java is more of a professional tool--you really don't WANT it doing tricky things. Many Java programmers don't even like "import java.util.*" and insist on expanding the exact imports so that you know exactly where each class is coming from.

With lighter languages like groovy, ruby, etc this isn't really as much of a problem--being terse is more important.

PS. If you have to use Java, honestly the answer is no, there is no good solution outside the development environment GUI. Embrace your GUI.

Bill K
I'm one of those. For snippets is ok to have import `java.util.*;` For production code I would insist in listing each and every one of the used classes.
OscarRyz
Interesting, at least one or two votes down with no comment. I really wish you had to give some clue as to why you were voting... Anyone care to guess how I offended (or is this just wrong?)
Bill K
sorry, I still do not know how to use the tool!
paulocordeiro
There is no tool, it would be difficult to write (there are multiple "Point" classes defined in Java, which should it auto-import) and you don't want to do so anyway because it would be code gen. Sorry if I used too many words to say that! If you want an easy fix, use Eclipse and type shift-alt-O.
Bill K
+1  A: 

Even if there is such a tool, it won't work always automatically without user input.

If you have for example this code:

List myList;

It has to ask, if the List should be from java.awt.List or java.util.List.

michael.kebe