|
Eclipse JDT Release 3.1 |
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.jdt.core.dom.ASTNode
org.eclipse.jdt.core.dom.CompilationUnit
Java compilation unit AST node type. This is the type of the root of an AST.
The source range for this type of node is ordinarily the entire source file, including leading and trailing whitespace and comments.
For JLS2:CompilationUnit: [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration | ; }For JLS3, the kinds of type declarations grew to include enum and annotation type declarations:
CompilationUnit: [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration | EnumDeclaration | AnnotationTypeDeclaration | ; }
Field Summary | |
---|---|
static ChildListPropertyDescriptor |
IMPORTS_PROPERTY
The "imports" structural property of this node type. |
static ChildPropertyDescriptor |
PACKAGE_PROPERTY
The "package" structural property of this node type. |
static ChildListPropertyDescriptor |
TYPES_PROPERTY
The "types" structural property of this node type. |
Method Summary | |
---|---|
ASTNode |
findDeclaringNode(IBinding binding)
Finds the corresponding AST node in the given compilation unit from which the given binding originated. |
ASTNode |
findDeclaringNode(String key)
Finds the corresponding AST node in the given compilation unit from which the binding with the given key originated. |
List |
getCommentList()
Returns a list of the comments encountered while parsing this compilation unit. |
int |
getExtendedLength(ASTNode node)
Returns the extended source length of the given node. |
int |
getExtendedStartPosition(ASTNode node)
Returns the extended start position of the given node. |
IJavaElement |
getJavaElement()
The Java element (an org.eclipse.jdt.core.ICompilationUnit or an org.eclipse.jdt.core.IClassFile )
this compilation unit was created from, or null if it was not created from a Java element. |
Message[] |
getMessages()
Returns the list of messages reported by the compiler during the parsing or the type checking of this compilation unit. |
PackageDeclaration |
getPackage()
Returns the node for the package declaration of this compilation unit, or null if this compilation unit is in the
default package. |
IProblem[] |
getProblems()
Returns the list of detailed problem reports noted by the compiler during the parsing or the type checking of this compilation unit. |
List |
imports()
Returns the live list of nodes for the import declarations of this compilation unit, in order of appearance. |
int |
lineNumber(int position)
Returns the line number corresponding to the given source character position in the original source string. |
static List |
propertyDescriptors(int apiLevel)
Returns a list of structural property descriptors for this node type. |
void |
recordModifications()
Enables the recording of changes to this compilation unit and its descendents. |
TextEdit |
rewrite(IDocument document,
Map options)
Converts all modifications recorded for this compilation unit into an object representing the corresponding text edits to the given document containing the original source code for this compilation unit. |
void |
setPackage(PackageDeclaration pkgDecl)
Sets or clears the package declaration of this compilation unit node to the given package declaration node. |
List |
types()
Returns the live list of nodes for the top-level type declarations of this compilation unit, in order of appearance. |
Methods inherited from class org.eclipse.jdt.core.dom.ASTNode |
---|
accept, copySubtree, copySubtrees, delete, equals, getAST, getFlags, getLength, getLocationInParent, getNodeType, getParent, getProperty, getRoot, getStartPosition, getStructuralProperty, nodeClassForType, properties, setFlags, setProperty, setSourceRange, setStructuralProperty, structuralPropertiesForType, subtreeBytes, subtreeMatch, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final ChildPropertyDescriptor PACKAGE_PROPERTY
public static final ChildListPropertyDescriptor IMPORTS_PROPERTY
public static final ChildListPropertyDescriptor TYPES_PROPERTY
Method Detail |
public static List propertyDescriptors(int apiLevel)
apiLevel
- the API level; one of the
AST.JLS*
constants
StructuralPropertyDescriptor
)public PackageDeclaration getPackage()
null
if this compilation unit is in the
default package.
null
if nonepublic void setPackage(PackageDeclaration pkgDecl)
pkgDecl
- the new package declaration node, or
null
if this compilation unit does not have a package
declaration (that is in the default package)
IllegalArgumentException
- if:
public List imports()
ImportDeclaration
)public List types()
Note that in JLS3, the types may include both enum declarations
and annotation type declarations introduced in J2SE 5.
For JLS2, the elements are always TypeDeclaration
.
AbstractTypeDeclaration
)public ASTNode findDeclaringNode(IBinding binding)
null
if the
binding does not correspond to any node in this compilation unit.
This method always returns null
if bindings were not requested
when this AST was built.
The following table indicates the expected node type for the various different kinds of bindings:
PackageDeclaration
TypeDeclaration
or a
AnonymousClassDeclaration
(for anonymous classes)VariableDeclarationFragment
in a
FieldDeclaration
SingleVariableDeclaration
, or
a VariableDeclarationFragment
in a
VariableDeclarationStatement
or
VariableDeclarationExpression
MethodDeclaration
MethodDeclaration
AnnotationTypeDeclaration
AnnotationTypeMemberDeclaration
EnumDeclaration
EnumConstantDeclaration
TypeParameter
Each call to ASTParser.createAST(org.eclipse.core.runtime.IProgressMonitor)
with a request for bindings
gives rise to separate universe of binding objects. This method always returns
null
when the binding object comes from a different AST.
Use findDeclaringNode(binding.getKey())
when the binding comes
from a different AST.
binding
- the binding
null
if the binding does not correspond to a node in this
compilation unit or if bindings were not requested when this AST was builtfindDeclaringNode(String)
public ASTNode findDeclaringNode(String key)
null
if the corresponding node cannot be determined.
This method always returns null
if bindings were not requested
when this AST was built.
The following table indicates the expected node type for the various different kinds of binding keys:
PackageDeclaration
TypeDeclaration
or a
AnonymousClassDeclaration
(for anonymous classes)VariableDeclarationFragment
in a
FieldDeclaration
SingleVariableDeclaration
, or
a VariableDeclarationFragment
in a
VariableDeclarationStatement
or
VariableDeclarationExpression
MethodDeclaration
MethodDeclaration
AnnotationTypeDeclaration
AnnotationTypeMemberDeclaration
EnumDeclaration
EnumConstantDeclaration
TypeParameter
key
- the binding key, or null
null
if the key is null
or if the key does not correspond to a node in this compilation unit
or if bindings were not requested when this AST was builtIBinding.getKey()
public int getExtendedStartPosition(ASTNode node)
ASTNode.getStartPosition()
and ASTNode.getLength()
,
the extended source range may include comments and whitespace
immediately before or after the normal source range for the node.
node
- the node
-1
if no source position information is recorded for this nodegetExtendedLength(ASTNode)
public int getExtendedLength(ASTNode node)
ASTNode.getStartPosition()
and ASTNode.getLength()
,
the extended source range may include comments and whitespace
immediately before or after the normal source range for the node.
node
- the node
0
if no source position information is recorded for this nodegetExtendedStartPosition(ASTNode)
public int lineNumber(int position)
class A\n{\n}
has 3 lines
corresponding to inclusive character ranges [0,7], [8,9], and [10,10].
Returns 1 for a character position that does not correspond to any
source line, or if no line number information is available for this
compilation unit.
position
- a 0-based character position, possibly
negative or out of range
1
if the character
position does not correspond to a source line in the original
source file or if line number information is not known for this
compilation unitASTParser
public IJavaElement getJavaElement()
org.eclipse.jdt.core.ICompilationUnit
or an org.eclipse.jdt.core.IClassFile
)
this compilation unit was created from, or null
if it was not created from a Java element.
null
if nonepublic Message[] getMessages()
This list of messages is suitable for simple clients that do little
more than log the messages or display them to the user. Clients that
need further details should call getProblems
to get
compiler problem objects.
getProblems()
,
ASTParser
public IProblem[] getProblems()
Simple clients that do little more than log the messages or display
them to the user should probably call getMessages
instead.
getMessages()
,
ASTParser
public List getCommentList()
Since the Java language allows comments to appear most anywhere
in the source text, it is problematic to locate comments in relation
to the structure of an AST. The one exception is doc comments
which, by convention, immediately precede type, field, and
method declarations; these comments are located in the AST
by BodyDeclaration.getJavadoc
.
Other comments do not show up in the AST. The table of comments
is provided for clients that need to find the source ranges of
all comments in the original source string. It includes entries
for comments of all kinds (line, block, and doc), arranged in order
of increasing source position.
getParent()
of a doc comment associated with a body declaration is the body
declaration node; for these comment nodes
getRoot()
will return the compilation unit
(assuming an unmodified AST) reflecting the fact that these nodes
are property located in the AST for the compilation unit.
However, for other comment nodes, getParent()
will return null
, and getRoot()
will return the comment node itself, indicating that these comment nodes
are not directly connected to the AST for the compilation unit. The
Comment.getAlternateRoot
method provides a way to navigate from a comment to its compilation
unit.
A note on visitors: The only comment nodes that will be visited when
visiting a compilation unit are the doc comments parented by body
declarations. To visit all comments in normal reading order, iterate
over the comment table and call accept
on each element.
Clients cannot modify the resulting list.
null
if comment information
for this compilation unit is not availableASTParser
public void recordModifications()
ASTParser
and still be in
its original state. Once recording is on,
arbitrary changes to the subtree rooted at this compilation
unit are recorded internally. Once the modification has
been completed, call rewrite
to get an object
representing the corresponding edits to the original
source code string.
IllegalArgumentException
- if this compilation unit is
marked as unmodifiable, or if this compilation unit has already
been tampered with, or recording has already been enabledpublic TextEdit rewrite(IDocument document, Map options)
The compilation unit must have been created by
ASTParser
from the source code string in the
given document, and recording must have been turned
on with a prior call to recordModifications
while the AST was still in its original state.
Calling this methods does not discard the modifications on record. Subsequence modifications made to the AST 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.
document
- original document containing source code
for this compilation unitoptions
- the table of formatter options
(key type: String
; value type: String
);
or null
to use the standard global options
JavaCore.getOptions()
.
IllegalArgumentException
- if the document passed is
null
or does not correspond to this AST
IllegalStateException
- if recordModifications
was not called to enable recordingrecordModifications()
|
Eclipse JDT Release 3.1 |
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |