Import Tidier  Import Tidier

go to home page Student Projects full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
This essay is about a suggested student project in Java programming. This essay gives a rough overview of how it might work. It does not describe an actual complete program. I have no source, object, specifications, file layouts or anything else useful to implementing this project. Everything I have to say to help you with this project is written below. I am not prepared to help you implement it; I have too many other projects of my own.

I do contract work for a living, which could include writing a program such as this. However, I don’t do people’s homework for them. That just robs them of an education.

You have my full permission to implement this project any way you please.

This project is no longer much of a priority now that Eclipse and Intellij both do an excellent job of import tidying.

There are two questions a maintenance programmer commonly asks:

  1. What classes does this defined class use?
  2. What package is this referenced class in?
Java’s import gives only rough clues because:
  1. Some classes may be used in fully qualified form in the code bodies, hence don’t need to be mentioned in the import. You can’t tell from looking at the import statements all the classes used.
  2. Imports don’t need to mention java.lang classes.
  3. Imports can contain packages and classes never used.
  4. Imports can contain wildcards without ever mentioning the specific classes used.
Further programmers like to have rwars or just how imports and qualification should be organised.

When I am maintaining other people’s programs, I prefer explicit imports rather than wildcard import on demand. Further, I prefer all variable declarations to be fully qualified in the code body. When I am writing new code, I prefer to use wildcard imports, and unqualified declarations. This makes it easy to use new classes in my code. It takes fewer keystrokes. Others wildly disagree. It would be nice if there were some automatic way to convert my new code style to my maintenance style and back.

These problems could be rectified by a tool that tidied the Java source code to do the following things:

  1. List the fully qualified imports for each class used.
  2. List them in alphabetical order.
  3. Optionally include or exclude java.lang classes.
  4. Optionally remove any imports not used.
  5. Optionally remove any wildcard imports.
  6. Optionally remove individual imports where there is a wildcard covering them.
  7. Optionally list imports in wildcard form for packages with n or more classes, where n is an integer > 1,
  8. Optionally convert to fully qualified class names in the source code bodies.
  9. Optionally convert to unqualified class names in the source code bodies.
You might think you would have to parse the Java source code with a miniature compiler to handle this project. There is an easier way to tackle it, however, it requires a clean compile first. You look in the class file to get the list of fully qualified class names and regenerate the needed import list from that, ignoring what the programmer originally used as imports.
decompiler
disassembler
JASM
There is a catch, the class file does not list the classes of finals. You will have to find these yourself.
CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.59] The information on this page is for non-military use only.
You are visitor number 5,916. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/project/importtidier.html J:\mindprod\project\importtidier.html