Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console command to do Java auto-import

I develop in Java using notepad. I am quite happy with the development cycle, but I lack something like the auto-import feature like that Eclipse and other big IDEs have. My guess is: if Eclipse does it, then there is an external tool that can do that.

Can I just install such a tool and use it from command line?

For example I have this source code:

public class Test extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView tv = new TextView(this);
        tv.setText("Hello World!");
        setContentView(tv);
    }
}

I would like to run some utility like that:

jautoimp Test.java

and get this output:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
like image 556
exebook Avatar asked Jan 20 '13 06:01

exebook


People also ask

How do I enable auto import?

In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Enable the Optimize imports on the fly option and apply the changes.

What is the shortcut for importing the methods?

You can simply import all of them by pressing the ALT + ENTER key.


2 Answers

UPDATE: serv-inc has created a version of this program that automatically determines the classes you need to import (instead of having to manually specifying them yourself).

Usage: ./javimp.py <java-src-file> [<java-src-file> ...]

Only insert mode is supported (meaning that the file itself will be changed). This tool is probably closer to what most people who would like a command line auto-import tool are looking for.


I was looking for the same thing today, but I didn't find it. However, I spent the better part of today making a Python script with the capability of doing something similar.

javimp.py works via a CLI. There are several different modes to it, but in your case, the -i option is probably what you want.

python javimp.py -i YourJavaFile.java

Running this command will make javimp.py go through YourJavaFile.java looking for lines beginning with "import". When it finds one, it will try to match the rest of the line with the end of an entry in a database of Java classes created by web scraping, and if it finds a match, it replaces the line with import full.package.to.WhateverClassYouHadImported;. It will also account for missing (or even too many) semicolons.

In your example, this would be:

// Note that you still need to import the classes, but
// you don't need to remember the packages in your head
// anymore

import Activity; // -> import android.app.Activity;
import Bundle;   // -> import android.os.Bundle;
import TextView; // -> import android.widget.TextView;

public class Test extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView tv = new TextView(this);
        tv.setText("Hello World!");
        setContentView(tv);
    }
}

This approach is not perfect - notably, it's possible to get the wrong match if there are two classes with the same name somewhere in the database, and it also requires a somewhat up-to-date database that covers all your areas of development. However, it might be a useful tool so long as you don't trust it too much. You could also edit the source code to extend the web scraping to other webpages than the ones I specified in my quick jumble-together solution. Out of the box, it should support the Java standard library and the Android API, as of 2016/06/20.

Remember that if you haven't made javimp.py accessible from anywhere in your file system, you still need to specify the path to it if you're running it from somewhere else than the directory in which it is located. Personally, I have circumvented this by adding a command to my editor which runs it on the active file by specifying the absolute path. You might do something similar.

like image 155
winterweird Avatar answered Sep 20 '22 13:09

winterweird


The primary reason that modern IDE's can autocomplete is because they have an intimate knowledge of your complete project. A good autocomplete works even without a separate compilation step.

This implies that the editor and the compiler are very good friends so that the compiler handles files as they are edited (as opposed to compiling everything), and that the editor knows about what the compiler derives from your source.

In the Unix world this was typically called TAGS, with two dialects - one for vi and one for emacs. Your best bet as I see it is to switch to one of these editors, and then generate TAGS files as needed by hand. There was an attempt to write a fuller Java programming environment in Emacs (the Java EE project), but Eclipse killed that as it could not do refactoring.

Note that the feature you ask for is actually one of the characteristics of an IDE. You might want to consider if you are ready for more IDE features than just autocompletion.

like image 40
Thorbjørn Ravn Andersen Avatar answered Sep 19 '22 13:09

Thorbjørn Ravn Andersen