java.lang.String string; |
Two of these three qualified names are not necessary if there are appropriate import statements. This seems to be of no importance if you look at just these three lines of code. However unnecessary qualified names are annoying if you have plenty of them in your source code. They make code less readable and complicate code maintainability.
With jMDA it is easy to avoid unnecessary qualified names. jmda.gen contains a class called ImportManager helps to identify which type names have to be qualified and which ones can be imported and used with their simple names. ImportManager objects are always related to a particular package. Usually that package is the target for the code that a generator produces. This way it is easy for ImportManager to find out if the generated code uses other types from the target package. If so these types don't need qualified names.
jMDA generators like DefaultClassGenerator for example automatically produce correct import statements if you use ImportManager. The following code fragment illustrates how to use ImportManager:
ImportManager importManager =
|
The above code produces the following output:
import statements:
import java.util.Date;
type [Type]
utilDate [Date]
sqlDate [java.sql.Date]
string [String]
As you can see ImportManager generated just one import statement which is correct because all other used types are either from "some.target.package" or from "java.lang". Types from both packages can be used with their simple names without explicit import statements.
ImportManager also recognizes that sqlDate needs to be used with its qualified type name. This is because utilDate already uses java.util.Date and has a respective import statement. An additional java.sql.Date import statement would collide with java.util.Date and the compiler will refuse occurances of both import statements in the same compilation unit.
No comments:
Post a Comment