|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.emf.codegen.util.ImportManager
public class ImportManager
A manager for import declarations in generated code.
An instance of ImportManager
should be created for each compilation unit being generated. It
maintains the list of imports to be added to the compilation unit and associates registered short names with
their corresponding qualified names.
Common usage of ImportManager
is very simple. This example assumes that a StringBuilder
is being used to accumulate the text for a generated source file:
StringBuilder result = new StringBuilder(); ImportManager importManager = new ImportManager("com.example.target", "Target"); ... importManager.markImportLocation(result); ... result.append(importManager.getImportedName("com.example.lib.Example", true)); ... result.append(importManager.getImportedName("java.lang.String", true)); ... result.append(importManager.getImportedName("java.util.Arrays", true)); ... result.append(importManager.getImportedName("org.test.Example", true)); ... result.append(importManager.getImportedName("org.test.Target", true)); ... result.append(importManager.getImportedName("com.example.target.Helper", true)); ... importManager.emitSortedImports();
The constructor is passed the package name and short name for the compilation unit being generated. The point
where the imports should be inserted is marked by calling markImportLocation
. Then,
getImportedName()
is called each time a type name is needed, to determine the correct name to use.
Passing true
as the second argument instructs the import manager to automatically import the specified
type, if possible.
In this case, the following names would be returned:
Example
String
Arrays
org.test.Example
org.test.Target
Helper
org.test.Example
cannot be shortened because the short name Example
is already
taken. Similarly, Target
is already taken by the compilation unit, itself. Helper
and
String
are shortened, but they don't actually require imports.
Finally, the needed import declarations are inserted by calling emitSortedImports()
. In this case,
only two import declarations are produced:
import com.example.lib.Example; import java.util.Arrays;
In addition to auto-importing, ImportManager
supports explicit pre-registration of individual and
wildcard imports via addImport()
.
Field Summary | |
---|---|
protected java.util.HashSet<java.lang.String> |
importedPackages
The set of packages that have been imported with wildcards. |
protected java.util.SortedSet<java.lang.String> |
imports
The set of imports to be added to the compilation unit. |
protected java.util.HashSet<java.lang.String> |
javaLangImports
The set of short names from java.lang for which explicit import declarations are desired. |
protected java.util.HashMap<java.lang.String,java.lang.String> |
shortNameToImportMap
The mapping from short names to qualified names for explicit and implicit imports. |
Constructor Summary | |
---|---|
ImportManager(java.lang.String compilationUnitPackage)
Creates an import manager for a compilation unit in the given package. |
|
ImportManager(java.lang.String compilationUnitPackage,
java.lang.String compilationUnitShortName)
Creates an import manager for the given compilation unit package and short name. |
Method Summary | |
---|---|
void |
addCompilationUnitImports(java.lang.String compilationUnitContents)
Registers pseudo-imports for all of the import declarations in the specified
compilation unit contents. |
void |
addImport(java.lang.String qualifiedName)
Registers an import for the given qualified name. |
void |
addImport(java.lang.String packageName,
java.lang.String shortName)
Registers an import for the given package name and short name. |
void |
addJavaLangImports(java.util.List<java.lang.String> javaLangClassNames)
Ensures that explicit import declarations will be added for classes from java.lang with the
specified short names. |
void |
addMasterImport(java.lang.String packageName,
java.lang.String shortName)
Reserves the import mapping for the given package and short name of the compilation unit. |
void |
addPseudoImport(java.lang.String qualifiedName)
Registers a pseudo-import for the given qualified name. |
java.lang.String |
computeSortedImports()
Returns the sorted, formatted import declarations that should be added to the compilation unit. |
void |
emitSortedImports()
Inserts all the computed imports for the compilation unit into the recorded
StringBuilder or StringBuffer . |
java.lang.String |
getImportedName(java.lang.String qualifiedName)
Returns the equivalent imported short name for the given qualified name, if there is one, or the qualified name itself otherwise. |
java.lang.String |
getImportedName(java.lang.String qualifiedName,
boolean autoImport)
Returns the equivalent imported short name for the given qualified name, if there is one, or the qualified name itself otherwise. |
java.util.Collection<java.lang.String> |
getImports()
Returns the list of qualified names for which imports are to be added to the compilation unit. |
java.lang.String |
getLineDelimiter()
Returns the line delimiter to be used in computeSortedImports() . |
boolean |
hasImport(java.lang.String shortName)
Returns whether a mapping for the given short name has been reserved. |
void |
markImportLocation(java.lang.StringBuffer stringBuffer)
Records the given StringBuffer and its current length, so that computed imports can later be
emitted , and adds any import declarations
that the buffer already contains. |
void |
markImportLocation(java.lang.StringBuilder stringBuilder)
Records the given StringBuilder and its current length, so that computed imports can later be
emitted , and adds any import declarations
that the builder already contains. |
void |
setLineDelimiter(java.lang.String lineDelimiter)
Sets the line delimiter to be used in computeSortedImports() . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected java.util.SortedSet<java.lang.String> imports
protected java.util.HashMap<java.lang.String,java.lang.String> shortNameToImportMap
protected java.util.HashSet<java.lang.String> importedPackages
protected java.util.HashSet<java.lang.String> javaLangImports
java.lang
for which explicit import declarations are desired.
Constructor Detail |
---|
public ImportManager(java.lang.String compilationUnitPackage, java.lang.String compilationUnitShortName)
master import
for the compilation unit.
addMasterImport(String, String)
public ImportManager(java.lang.String compilationUnitPackage)
two-argument form
is preferred.
ImportManager(String, String)
Method Detail |
---|
public java.lang.String getImportedName(java.lang.String qualifiedName, boolean autoImport)
qualifiedName
- the qualified name or parameterized type expression.autoImport
- whether to try to automatically import types as needed.
public java.lang.String getImportedName(java.lang.String qualifiedName)
qualifiedName
- the qualified name or parameterized type expression.autoImport
- whether to try to automatically import types as needed.
public void addImport(java.lang.String packageName, java.lang.String shortName)
$
notation for inner classes is not supported by this method, since the short
name is explicitly specified.
packageName
- the package name of the type to importshortName
- the short name of the type to import, or "*"
for a wildcard import.public void addImport(java.lang.String qualifiedName)
qualifiedName
- the qualified name of the type to import, which may end with ".*"
for a wildcard import.public void addPseudoImport(java.lang.String qualifiedName)
computeSortedImports()
or emitted by
emitSortedImports()
.
Note that all pseudo-imports must be added before any other ordinary imports.
qualifiedName
- the qualified name of the type to import, which may end with ".*"
for a wildcard import.computeSortedImports()
,
emitSortedImports()
public void addMasterImport(java.lang.String packageName, java.lang.String shortName)
$
notation for inner classes is not supported by this method, since the short name is
explicitly specified.
Note that a master import must be added before any pseudo- or ordinary imports. However, it need not be done
explicitly if the preferred, two-argument constructor
form is used.
ImportManager(String, String)
public boolean hasImport(java.lang.String shortName)
public java.util.Collection<java.lang.String> getImports()
public java.lang.String getLineDelimiter()
computeSortedImports()
.
By default, this is System.getProperty
("line.separator")
.
computeSortedImports()
,
setLineDelimiter(String)
public void setLineDelimiter(java.lang.String lineDelimiter)
computeSortedImports()
.
computeSortedImports()
,
getLineDelimiter()
public java.lang.String computeSortedImports()
line delimiter
before the
first import and between imports from different packages.
getLineDelimiter()
public void addCompilationUnitImports(java.lang.String compilationUnitContents)
pseudo-imports
for all of the import declarations in the specified
compilation unit contents.
This uses JDT's Java AST API to parse the code when Eclipse is running, and a simpler, less accurate approach
based on regular expressions otherwise.
Note that this must be invoked before any ordinary imports are added.
compilationUnitContents
- the contents of a Java source file.addPseudoImport(String)
public void markImportLocation(java.lang.StringBuilder stringBuilder)
StringBuilder
and its current length, so that computed imports can later be
emitted
, and adds
any import declarations
that the builder already contains.
emitSortedImports()
,
addCompilationUnitImports(String)
public void markImportLocation(java.lang.StringBuffer stringBuffer)
StringBuffer
and its current length, so that computed imports can later be
emitted
, and adds
any import declarations
that the buffer already contains.
emitSortedImports()
,
addCompilationUnitImports(String)
public void emitSortedImports()
computed imports
for the compilation unit into the recorded
StringBuilder
or StringBuffer
.
computeSortedImports()
,
markImportLocation(StringBuilder)
,
markImportLocation(StringBuffer)
public void addJavaLangImports(java.util.List<java.lang.String> javaLangClassNames)
java.lang
with the
specified short names.
By default, all the short names of classes in this package are reserved so that the implicit imports are used.
By specifying particular classes with this method, those imports, if actually used, will be made explicit.
javaLangClassNames
-
|
Copyright 2001-2006 IBM Corporation and others. All Rights Reserved. |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |