Re: Need tool to find necessary "import" statements for given java
source;
On 07/29/2010 07:05 AM, Raymond Schanks wrote:
I often see java [sic] source code snippets on web pages/tutorials.
Unfortunately the are shown WITHOUT the necessary "import" statements at the top.
Is there a tool which inspects such a java [sic] source and (depending on the java [sic] version)
suggests the required "import" statement?
Sometimes the java [sic] sample code is java [sic] 1.4 related and some used functions are deprecated.
I would appreciate if this tool could suggest newer source code replacements for e.g. java [sic] 1.6.
"find /path/ \*.jar | grep" or the equivalent will help with the find part,
but the suggest part is a matter of being educated and/or looking up found
classes in their Javadocs.
There are standards, e.g., if you see 'Map' you can pretty much bet it's
'java.util.Map', or if you see 'ConcurrentHashMap' you can bet it's
'java.util.concurrent.ConcurrentHashMap'. WHen particular libraries are
intended posters should specify the library. For example, while you might
infer that a reference to 'Bag' means
'org.apache.commons.collections.bag.HashBag', it behooves the poster to be
explicit. (Apache Commons has a deprecated version of that class, something I
didn't know until just now when I looked up the Javadocs.)
Complicating the matter is that there is no standard version of 'Foo', a class
frequently mentioned in code snippets.
But usually if no import is mentioned, it's either because the class is part
of the java.* or javax.* family, or because it doesn't matter.
As for deprecation or obsolescence (not all obsolescent classes are
deprecated), that's also a matter of being educated and using Javadocs. For
example, references to 'Hashtable' ('java.util.Hashtable') are nearly always
out of date. 'org.apache.commons.collections.HashBag' is documented to be
deprecated.
Others have mentioned using IDE features. Eclipse and its children have a
compiler setting to warn of deprecated constructs. That won't help with
non-deprecated obsolescence, though.
The only real defense is to know the standard API. Get studying.
--
Lew