/ Forside / Teknologi / Udvikling / Java / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Java
#NavnPoint
molokyle 3688
Klaudi 855
strarup 740
Forvirret 660
gøgeungen 500
Teil 373
Stouenberg 360
vnc 360
pmbruun 341
10  mccracken 320
Rationalising imports
Fra : Colin Samuel Rosenth~


Dato : 03-03-04 14:06

Suppose I have a class with a whole lot of wild-card imports, like
java.util.*; etc. Is there a pre-processor which will take this
file (and a classpath) and replace the import list with one containing
just the actual classes referenced? Or could one reverse-engineer such a
list from the compiled .class file?

--
Colin Rosenthal Sabbagh's Second Law: The biggest problem with
communication is the illusion that it has occurred.

 
 
Jonas Kongslund (03-03-2004)
Kommentar
Fra : Jonas Kongslund


Dato : 03-03-04 14:20

Colin Samuel Rosenthal wrote:

> Suppose I have a class with a whole lot of wild-card imports, like
> java.util.*; etc. Is there a pre-processor which will take this
> file (and a classpath) and replace the import list with one containing
> just the actual classes referenced? Or could one reverse-engineer such a
> list from the compiled .class file?

You have misunderstood the import keyword. You should consider it as a
compiler directive that is only used at compile time to fully qualify all
unqualified class names.

Take a look at
<http://users.bigpond.net.au/stephen.davies/papers/javaImports.html>.

--
Jonas Kongslund

Colin Samuel Rosenth~ (03-03-2004)
Kommentar
Fra : Colin Samuel Rosenth~


Dato : 03-03-04 20:47

In article <SWk1c.412$DU4.202@news.get2net.dk>, Jonas Kongslund
(dont@mail.me.at.all) says...
> Colin Samuel Rosenthal wrote:
>
> > Suppose I have a class with a whole lot of wild-card imports, like
> > java.util.*; etc. Is there a pre-processor which will take this
> > file (and a classpath) and replace the import list with one containing
> > just the actual classes referenced? Or could one reverse-engineer such a
> > list from the compiled .class file?
>
> You have misunderstood the import keyword. You should consider it as a
> compiler directive that is only used at compile time to fully qualify all
> unqualified class names.

Possibly, but I still think you have misunderstood what I am asking for.
I am aware that at compile time it is the fully qualified classes whch
are actually used. That is why it should be possible to obtain the list
of actual (fully-qualified) class names from the class file.

For example, if I have
import java.util.*;
but only use, say, List and Vector im my code, I would like a program
that would take my class file as input and spit out
java.util.List
java.util.Vector
as output. Why? So that I can clean up my source code and make it
conform to the convention of importing only fully-qualified class names.

Actually I think I may have found exactly what I'm looking for here
http://groups.google.com/groups?q=java+import+wildcard&hl=da&lr=&ie=UTF-
8&oe=UTF-8&selm=3adc85be%241%40post.newsfeeds.com&rnum=3

>
> Take a look at
> <http://users.bigpond.net.au/stephen.davies/papers/javaImports.html>.

good link though.

--
Colin Rosenthal Sabbagh's Second Law: The biggest problem with
communication is the illusion that it has occurred.

Jonas Kongslund (04-03-2004)
Kommentar
Fra : Jonas Kongslund


Dato : 04-03-04 23:59

Colin Samuel Rosenthal wrote:
> Possibly, but I still think you have misunderstood what I am asking for.

Yep, I can see that now after re-reading your posting. Sorry about that.

Besides Eclipse, I know that IntelliJ IDEA and Omnicore CodeGuide support
the feature that you are looking for.

--
Jonas Kongslund

Colin Samuel Rosenth~ (08-03-2004)
Kommentar
Fra : Colin Samuel Rosenth~


Dato : 08-03-04 10:26

In article <RvO1c.4822$L57.2813@news.get2net.dk>, Jonas Kongslund
(dont@mail.me.at.all) says...
> Colin Samuel Rosenthal wrote:
> > Possibly, but I still think you have misunderstood what I am asking for.
>
> Yep, I can see that now after re-reading your posting. Sorry about that.
>
> Besides Eclipse, I know that IntelliJ IDEA and Omnicore CodeGuide support
> the feature that you are looking for.

Thanks for all the suggestions. The program I found on dejanews (see
this thread for a link) works pretty well, but it's interesting to see
how many other solutions people have come up with.

--
Colin Rosenthal Sabbagh's Second Law: The biggest problem with
communication is the illusion that it has occurred.

Lasse Reichstein Nie~ (03-03-2004)
Kommentar
Fra : Lasse Reichstein Nie~


Dato : 03-03-04 21:17

Colin Samuel Rosenthal <colinandmarianne@adslhome.thisisnotanemailaddress.dk> writes:

> For example, if I have
> import java.util.*;
> but only use, say, List and Vector im my code, I would like a program
> that would take my class file as input and spit out
> java.util.List
> java.util.Vector
> as output. Why? So that I can clean up my source code and make it
> conform to the convention of importing only fully-qualified class names.

That sounds like a job for your editor. I know Eclipse can do it (the
menu entry is called "Organize imports" in the "Source" menu). It
automatically expands *'s, but you can also set a limit so that if
more than that number of classes are imported from a single package,
it uses .* instead.

I am sure most other specialized Java editors can do the same.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Thorbjørn Ravn Ander~ (04-03-2004)
Kommentar
Fra : Thorbjørn Ravn Ander~


Dato : 04-03-04 10:19

Colin Samuel Rosenthal wrote:

> Suppose I have a class with a whole lot of wild-card imports, like
> java.util.*; etc. Is there a pre-processor which will take this
> file (and a classpath) and replace the import list with one containing
> just the actual classes referenced? Or could one reverse-engineer such a
> list from the compiled .class file?

This is what Ctrl-Shift-O in Eclipse do.

--
Thorbjoern Ravn Andersen "...plus...Tubular Bells!"

Mikkel Bundgaard (04-03-2004)
Kommentar
Fra : Mikkel Bundgaard


Dato : 04-03-04 23:22

On Wed, 03 Mar 2004 14:05:39 +0100, Colin Samuel Rosenthal wrote:

> Suppose I have a class with a whole lot of wild-card imports, like
> java.util.*; etc. Is there a pre-processor which will take this
> file (and a classpath) and replace the import list with one containing
> just the actual classes referenced? Or could one reverse-engineer such a
> list from the compiled .class file?
Maybe you can use ImportScrubber (http://importscrubber.sourceforge.net/)
"No more unnecessary imports, no more import on demand - just good old
single type imports. And they're sorted, too!".

Otherwise look at the bottom of the referenced homepage (under "Related
projects") for other pointers.
--
Mikkel Bundgaard
Ph.D. student at IT University of Copenhagen
http://www.itu.dk/people/mikkelbu/
Codito, Ergo Sum

Søg
Reklame
Statistik
Spørgsmål : 177580
Tips : 31968
Nyheder : 719565
Indlæg : 6409083
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste