|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.wst.jsdt.core.dom.rewrite.ImportRewrite
public final class ImportRewrite
The ImportRewrite
helps updating imports following a import order and on-demand imports threshold as configured by a project.
The import rewrite is created on a javaScript unit and collects references to types that are added or removed. When adding imports, e.g. using
addImport(String)
, the import rewrite evaluates if the type can be imported and returns the a reference to the type that can be used in code.
This reference is either unqualified if the import could be added, or fully qualified if the import failed due to a conflict with another element of the same name.
On rewriteImports(IProgressMonitor)
the rewrite translates these descriptions into
text edits that can then be applied to the original source. The rewrite infrastructure tries to generate minimal text changes and only
works on the import statements. It is possible to combine the result of an import rewrite with the result of a ASTRewrite
as long as no import statements are modified by the AST rewrite.
The options controlling the import order and on-demand thresholds are:
setImportOrder(String[])
specifies the import groups and their preferred ordersetOnDemandImportThreshold(int)
specifies the number of imports in a group needed for a on-demand import statement (star import)setStaticOnDemandImportThreshold(int)
specifies the number of static imports in a group needed for a on-demand import statement (star import)
Nested Class Summary | |
---|---|
static class |
ImportRewrite.ImportRewriteContext
A ImportRewrite.ImportRewriteContext can optionally be used in e.g. |
Method Summary | |
---|---|
java.lang.String |
addImport(ITypeBinding binding)
Adds a new import to the rewriter's record and returns a type reference that can be used in the code. |
Type |
addImport(ITypeBinding binding,
AST ast)
Adds a new import to the rewriter's record and returns a Type that can be used
in the code. |
Type |
addImport(ITypeBinding binding,
AST ast,
ImportRewrite.ImportRewriteContext context)
Adds a new import to the rewriter's record and returns a Type that can be used
in the code. |
java.lang.String |
addImport(ITypeBinding binding,
ImportRewrite.ImportRewriteContext context)
Adds a new import to the rewriter's record and returns a type reference that can be used in the code. |
java.lang.String |
addImport(java.lang.String qualifiedTypeName)
Adds a new import to the rewriter's record and returns a type reference that can be used in the code. |
java.lang.String |
addImport(java.lang.String qualifiedTypeName,
java.lang.String packageName,
ImportRewrite.ImportRewriteContext context)
Adds a new import to the rewriter's record and returns a type reference that can be used in the code. |
Type |
addImportFromSignature(java.lang.String typeSig,
AST ast)
Adds a new import to the rewriter's record and returns a Type node that can be used
in the code as a reference to the type. |
Type |
addImportFromSignature(java.lang.String typeSig,
AST ast,
ImportRewrite.ImportRewriteContext context)
Adds a new import to the rewriter's record and returns a Type node that can be used
in the code as a reference to the type. |
java.lang.String |
addStaticImport(IBinding binding)
Adds a new static import to the rewriter's record and returns a reference that can be used in the code. |
java.lang.String |
addStaticImport(IBinding binding,
ImportRewrite.ImportRewriteContext context)
Adds a new static import to the rewriter's record and returns a reference that can be used in the code. |
java.lang.String |
addStaticImport(java.lang.String declaringTypeName,
java.lang.String simpleName,
boolean isField)
Adds a new static import to the rewriter's record and returns a reference that can be used in the code. |
java.lang.String |
addStaticImport(java.lang.String declaringTypeName,
java.lang.String simpleName,
boolean isField,
ImportRewrite.ImportRewriteContext context)
Adds a new static import to the rewriter's record and returns a reference that can be used in the code. |
static ImportRewrite |
create(IJavaScriptUnit cu,
boolean restoreExistingImports)
Creates a ImportRewrite from a IJavaScriptUnit . |
static ImportRewrite |
create(JavaScriptUnit astRoot,
boolean restoreExistingImports)
Creates a ImportRewrite from a an AST (JavaScriptUnit ). |
java.lang.String[] |
getAddedImports()
Returns all non-static imports that are recorded to be added. |
java.lang.String[] |
getAddedStaticImports()
Returns all static imports that are recorded to be added. |
IJavaScriptUnit |
getCompilationUnit()
The javaScript unit for which this import rewrite was created for. |
java.lang.String[] |
getCreatedImports()
Returns all new non-static imports created by the last invocation of rewriteImports(IProgressMonitor)
or null if these methods have not been called yet. |
java.lang.String[] |
getCreatedStaticImports()
Returns all new static imports created by the last invocation of rewriteImports(IProgressMonitor)
or null if these methods have not been called yet. |
ImportRewrite.ImportRewriteContext |
getDefaultImportRewriteContext()
Returns the default rewrite context that only knows about the imported types. |
java.lang.String[] |
getRemovedImports()
Returns all non-static imports that are recorded to be removed. |
java.lang.String[] |
getRemovedStaticImports()
Returns all static imports that are recorded to be removed. |
boolean |
hasRecordedChanges()
Returns true if imports have been recorded to be added or removed. |
boolean |
isImportMatchesType()
|
boolean |
removeImport(java.lang.String qualifiedName)
Records to remove a import. |
boolean |
removeStaticImport(java.lang.String qualifiedName)
Records to remove a static import. |
TextEdit |
rewriteImports(IProgressMonitor monitor)
Converts all modifications recorded by this rewriter into an object representing the corresponding text edits to the source code of the rewrite's javaScript unit. |
void |
setFilterImplicitImports(boolean filterImplicitImports)
Specifies that implicit imports (types in default package, package java.lang or
in the same package as the rewrite javaScript unit should not be created except if necessary
to resolve an on-demand import conflict. |
void |
setImportOrder(java.lang.String[] order)
Defines the import groups and order to be used by the ImportRewrite . |
void |
setOnDemandImportThreshold(int threshold)
Sets the on-demand import threshold for normal (non-static) imports. |
void |
setStaticOnDemandImportThreshold(int threshold)
Sets the on-demand import threshold for static imports. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static ImportRewrite create(IJavaScriptUnit cu, boolean restoreExistingImports) throws JavaScriptModelException
ImportRewrite
from a IJavaScriptUnit
. If restoreExistingImports
is true
, all existing imports are kept, and new imports will be inserted at best matching locations. If
restoreExistingImports
is false
, the existing imports will be removed and only the
newly added imports will be created.
Note that create(IJavaScriptUnit, boolean)
is more efficient than this method if an AST for
the javaScript unit is already available.
cu
- the javaScript unit to create the imports forrestoreExistingImports
- specifies if the existing imports should be kept or removed.
JavaScriptModelException
- thrown when the javaScript unit could not be accessed.public static ImportRewrite create(JavaScriptUnit astRoot, boolean restoreExistingImports)
ImportRewrite
from a an AST (JavaScriptUnit
). The AST has to be created from a
IJavaScriptUnit
, that means ASTParser.setSource(IJavaScriptUnit)
has been used when creating the
AST. If restoreExistingImports
is true
, all existing imports are kept, and new imports
will be inserted at best matching locations. If restoreExistingImports
is false
, the
existing imports will be removed and only the newly added imports will be created.
Note that this method is more efficient than using create(IJavaScriptUnit, boolean)
if an AST is already available.
astRoot
- the AST root node to create the imports forrestoreExistingImports
- specifies if the existing imports should be kept or removed.
java.lang.IllegalArgumentException
- thrown when the passed AST is null or was not created from a javaScript unit.public void setImportOrder(java.lang.String[] order)
ImportRewrite
.
Imports are added to the group matching their qualified name most. The empty group name groups all imports not matching
any other group. Static imports are managed in separate groups. Static import group names are prefixed with a '#' character.
order
- A list of strings defining the import groups. A group name must be a valid package name or empty. If can be
prefixed by the '#' character for static import groupspublic void setOnDemandImportThreshold(int threshold)
threshold
- a positive number defining the on-demand import threshold
for normal (non-static) imports.
java.lang.IllegalArgumentException
- a IllegalArgumentException
is thrown
if the number is not positive.public void setStaticOnDemandImportThreshold(int threshold)
threshold
- a positive number defining the on-demand import threshold
for normal (non-static) imports.
java.lang.IllegalArgumentException
- a IllegalArgumentException
is thrown
if the number is not positive.public IJavaScriptUnit getCompilationUnit()
public ImportRewrite.ImportRewriteContext getDefaultImportRewriteContext()
public void setFilterImplicitImports(boolean filterImplicitImports)
java.lang
or
in the same package as the rewrite javaScript unit should not be created except if necessary
to resolve an on-demand import conflict. The filter is enabled by default.
filterImplicitImports
- if set, implicit imports will be filtered.public Type addImportFromSignature(java.lang.String typeSig, AST ast)
Type
node that can be used
in the code as a reference to the type. The type binding can be an array binding, type variable or wildcard.
If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type
arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard
of wildcards are ignored.
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
typeSig
- the signature of the type to be added.ast
- the AST to create the returned type for.
public Type addImportFromSignature(java.lang.String typeSig, AST ast, ImportRewrite.ImportRewriteContext context)
Type
node that can be used
in the code as a reference to the type. The type binding can be an array binding, type variable or wildcard.
If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type
arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard
of wildcards are ignored.
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
typeSig
- the signature of the type to be added.ast
- the AST to create the returned type for.context
- an optional context that knows about types visible in the current scope or null
to use the default context only using the available imports.
public java.lang.String addImport(ITypeBinding binding)
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
binding
- the signature of the type to be added.
public java.lang.String addImport(ITypeBinding binding, ImportRewrite.ImportRewriteContext context)
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
binding
- the signature of the type to be added.context
- an optional context that knows about types visible in the current scope or null
to use the default context only using the available imports.
public Type addImport(ITypeBinding binding, AST ast)
Type
that can be used
in the code. The type binding can be an array binding, type variable or wildcard.
If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type
arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard
of wildcards are ignored.
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
binding
- the signature of the type to be added.ast
- the AST to create the returned type for.
public Type addImport(ITypeBinding binding, AST ast, ImportRewrite.ImportRewriteContext context)
Type
that can be used
in the code. The type binding can be an array binding, type variable or wildcard.
If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type
arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard
of wildcards are ignored.
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
binding
- the signature of the type to be added.ast
- the AST to create the returned type for.context
- an optional context that knows about types visible in the current scope or null
to use the default context only using the available imports.
public java.lang.String addImport(java.lang.String qualifiedTypeName, java.lang.String packageName, ImportRewrite.ImportRewriteContext context)
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
qualifiedTypeName
- the qualified type name of the type to be addedcontext
- an optional context that knows about types visible in the current scope or null
to use the default context only using the available imports.
public java.lang.String addImport(java.lang.String qualifiedTypeName)
No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
qualifiedTypeName
- the qualified type name of the type to be added
public java.lang.String addStaticImport(IBinding binding)
No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
binding
- The binding of the static field or method to be added.
java.lang.IllegalArgumentException
- an IllegalArgumentException
is thrown if the binding is not a static field
or method.public java.lang.String addStaticImport(IBinding binding, ImportRewrite.ImportRewriteContext context)
No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
binding
- The binding of the static field or method to be added.context
- an optional context that knows about members visible in the current scope or null
to use the default context only using the available imports.
java.lang.IllegalArgumentException
- an IllegalArgumentException
is thrown if the binding is not a static field
or method.public java.lang.String addStaticImport(java.lang.String declaringTypeName, java.lang.String simpleName, boolean isField)
No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
declaringTypeName
- The qualified name of the static's member declaring typesimpleName
- the simple name of the member; either a field or a method name.isField
- true
specifies that the member is a field, false
if it is a
method.
public java.lang.String addStaticImport(java.lang.String declaringTypeName, java.lang.String simpleName, boolean isField, ImportRewrite.ImportRewriteContext context)
No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.
declaringTypeName
- The qualified name of the static's member declaring typesimpleName
- the simple name of the member; either a field or a method name.isField
- true
specifies that the member is a field, false
if it is a
method.context
- an optional context that knows about members visible in the current scope or null
to use the default context only using the available imports.
public boolean removeImport(java.lang.String qualifiedName)
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that an import has been removed.
qualifiedName
- The import name to remove.
true
is returned of an import of the given name could be found.public boolean removeStaticImport(java.lang.String qualifiedName)
The content of the javaScript unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been removed.
qualifiedName
- The import name to remove.
true
is returned of an import of the given name could be found.public final TextEdit rewriteImports(IProgressMonitor monitor) throws CoreException
Calling this methods does not discard the modifications on record. Subsequence modifications are added to the ones already on record. If this method is called again later, the resulting text edit object will accurately reflect the net cumulative affect of all those changes.
monitor
- the progress monitor or null
CoreException
- the exception is thrown if the rewrite fails.public java.lang.String[] getCreatedImports()
rewriteImports(IProgressMonitor)
or null
if these methods have not been called yet.
Note that this list doesn't need to be the same as the added imports (see getAddedImports()
) as
implicit imports are not created and some imports are represented by on-demand imports instead.
public java.lang.String[] getCreatedStaticImports()
rewriteImports(IProgressMonitor)
or null
if these methods have not been called yet.
Note that this list doesn't need to be the same as the added static imports (getAddedStaticImports()
) as
implicit imports are not created and some imports are represented by on-demand imports instead.
public java.lang.String[] getAddedImports()
public java.lang.String[] getAddedStaticImports()
public java.lang.String[] getRemovedImports()
public java.lang.String[] getRemovedStaticImports()
public boolean hasRecordedChanges()
true
if imports have been recorded to be added or removed.
public boolean isImportMatchesType()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |