Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.eclipse.jdt.core.dom
Class CompilationUnit  view CompilationUnit download CompilationUnit.java

java.lang.Object
  extended byorg.eclipse.jdt.core.dom.ASTNode
      extended byorg.eclipse.jdt.core.dom.CompilationUnit

public class CompilationUnit
extends ASTNode

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 | ; }
 

Since:
2.0

Nested Class Summary
 
Nested classes inherited from class org.eclipse.jdt.core.dom.ASTNode
ASTNode.NodeList
 
Field Summary
private  DefaultCommentMapper commentMapper
          The comment mapper, or null in none; initially null.
private static Message[] EMPTY_MESSAGES
          Canonical empty list of messages.
private static org.eclipse.jdt.core.compiler.IProblem[] EMPTY_PROBLEMS
          Canonical empty list of problems.
private  ASTNode.NodeList imports
          The list of import declarations in textual order order; initially none (elementType: ImportDeclaration).
static ChildListPropertyDescriptor IMPORTS_PROPERTY
          The "imports" structural property of this node type.
private  int[] lineEndTable
          Line end table.
private  Message[] messages
          Messages reported by the compiler during parsing or name resolution.
private  java.util.List optionalCommentList
          The comment list (element type: Comment, or null if none; initially null.
(package private)  Comment[] optionalCommentTable
          The comment table, or null if none; initially null.
private  PackageDeclaration optionalPackageDeclaration
          The package declaration, or null if none; initially null.
static ChildPropertyDescriptor PACKAGE_PROPERTY
          The "package" structural property of this node type.
private  org.eclipse.jdt.core.compiler.IProblem[] problems
          Problems reported by the compiler during parsing or name resolution.
private static java.util.List PROPERTY_DESCRIPTORS
          A list of property descriptors (element type: StructuralPropertyDescriptor), or null if uninitialized.
private  ASTNode.NodeList types
          The list of type declarations in textual order order; initially none (elementType: AbstractTypeDeclaration)
static ChildListPropertyDescriptor TYPES_PROPERTY
          The "types" structural property of this node type.
 
Fields inherited from class org.eclipse.jdt.core.dom.ASTNode
ANNOTATION_TYPE_DECLARATION, ANNOTATION_TYPE_MEMBER_DECLARATION, ANONYMOUS_CLASS_DECLARATION, ARRAY_ACCESS, ARRAY_CREATION, ARRAY_INITIALIZER, ARRAY_TYPE, ASSERT_STATEMENT, ASSIGNMENT, ast, BASE_NODE_SIZE, BLOCK, BLOCK_COMMENT, BOOLEAN_LITERAL, BREAK_STATEMENT, CAST_EXPRESSION, CATCH_CLAUSE, CHARACTER_LITERAL, CLASS_INSTANCE_CREATION, COMPILATION_UNIT, CONDITIONAL_EXPRESSION, CONSTRUCTOR_INVOCATION, CONTINUE_STATEMENT, CYCLE_RISK, DO_STATEMENT, EMPTY_STATEMENT, ENHANCED_FOR_STATEMENT, ENUM_CONSTANT_DECLARATION, ENUM_DECLARATION, EXPRESSION_STATEMENT, FIELD_ACCESS, FIELD_DECLARATION, FOR_STATEMENT, HEADERS, IF_STATEMENT, IMPORT_DECLARATION, INFIX_EXPRESSION, INITIALIZER, INSTANCEOF_EXPRESSION, JAVADOC, LABELED_STATEMENT, LINE_COMMENT, MALFORMED, MANDATORY, MARKER_ANNOTATION, MEMBER_REF, MEMBER_VALUE_PAIR, METHOD_DECLARATION, METHOD_INVOCATION, METHOD_REF, METHOD_REF_PARAMETER, MODIFIER, NO_CYCLE_RISK, NORMAL_ANNOTATION, NULL_LITERAL, NUMBER_LITERAL, OPTIONAL, ORIGINAL, PACKAGE_DECLARATION, PARAMETERIZED_TYPE, PARENTHESIZED_EXPRESSION, POSTFIX_EXPRESSION, PREFIX_EXPRESSION, PRIMITIVE_TYPE, PROTECT, QUALIFIED_NAME, QUALIFIED_TYPE, RETURN_STATEMENT, SIMPLE_NAME, SIMPLE_TYPE, SINGLE_MEMBER_ANNOTATION, SINGLE_VARIABLE_DECLARATION, STRING_LITERAL, SUPER_CONSTRUCTOR_INVOCATION, SUPER_FIELD_ACCESS, SUPER_METHOD_INVOCATION, SWITCH_CASE, SWITCH_STATEMENT, SYNCHRONIZED_STATEMENT, TAG_ELEMENT, TEXT_ELEMENT, THIS_EXPRESSION, THROW_STATEMENT, TRY_STATEMENT, TYPE_DECLARATION, TYPE_DECLARATION_STATEMENT, TYPE_LITERAL, TYPE_PARAMETER, typeAndFlags, VARIABLE_DECLARATION_EXPRESSION, VARIABLE_DECLARATION_FRAGMENT, VARIABLE_DECLARATION_STATEMENT, WHILE_STATEMENT, WILDCARD_TYPE
 
Constructor Summary
(package private) CompilationUnit(AST ast)
          Creates a new AST node for a compilation owned by the given AST.
 
Method Summary
(package private)  void accept0(ASTVisitor visitor)
          Accepts the given visitor on a type-specific visit of the current node.
(package private)  void appendDebugString(java.lang.StringBuffer buffer)
          Appends a debug representation of this node to the given string buffer.
(package private)  ASTNode clone0(AST target)
          Returns a deep copy of the subtree of AST nodes rooted at this node.
 ASTNode findDeclaringNode(IBinding binding)
          Finds the corresponding AST node in the given compilation unit from which the given binding originated.
 ASTNode findDeclaringNode(java.lang.String key)
          Finds the corresponding AST node in the given compilation unit from which the binding with the given key originated.
 java.util.List getCommentList()
          Returns a list of the comments encountered while parsing this compilation unit.
(package private)  DefaultCommentMapper getCommentMapper()
          Returns the internal comment mapper.
 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.
 Message[] getMessages()
          Returns the list of messages reported by the compiler during the parsing or the type checking of this compilation unit.
(package private)  int getNodeType0()
          Returns an integer value identifying the type of this concrete AST node.
 PackageDeclaration getPackage()
          Returns the node for the package declaration of this compilation unit, or null if this compilation unit is in the default package.
 org.eclipse.jdt.core.compiler.IProblem[] getProblems()
          Returns the list of detailed problem reports noted by the compiler during the parsing or the type checking of this compilation unit.
 java.util.List imports()
          Returns the live list of nodes for the import declarations of this compilation unit, in order of appearance.
(package private)  void initCommentMapper(org.eclipse.jdt.internal.compiler.parser.Scanner scanner)
          Initializes the internal comment mapper with the given scanner.
(package private)  java.util.List internalGetChildListProperty(ChildListPropertyDescriptor property)
          Returns the list value of the given property for this node.
(package private)  ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child)
          Sets the child value of the given property for this node.
(package private)  java.util.List internalStructuralPropertiesForType(int apiLevel)
          Returns a list of property descriptors for this node type.
 int lineNumber(int position)
          Returns the line number corresponding to the given source character position in the original source string.
(package private)  int memSize()
          Returns an estimate of the memory footprint of this node in bytes.
static java.util.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.
 org.eclipse.text.edits.TextEdit rewrite(org.eclipse.jface.text.IDocument document, java.util.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.
(package private)  void setCommentTable(Comment[] commentTable)
          Sets the list of the comments encountered while parsing this compilation unit.
(package private)  void setLineEndTable(int[] lineEndTable)
          Sets the line end table 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.
(package private)  void setProblems(org.eclipse.jdt.core.compiler.IProblem[] problems)
          Sets the array of problems reported by the compiler during the parsing or name resolution of this compilation unit.
(package private)  boolean subtreeMatch0(ASTMatcher matcher, java.lang.Object other)
          Returns whether the subtree rooted at the given node matches the given other object as decided by the given matcher.
(package private)  int treeSize()
          Returns an estimate of the memory footprint in bytes of the entire subtree rooted at this node.
 java.util.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, acceptChild, acceptChildren, addProperty, appendPrintString, checkModifiable, checkNewChild, clone, copySubtree, copySubtrees, createPropertyList, delete, equals, getAST, getFlags, getLength, getLocationInParent, getNodeType, getParent, getProperty, getRoot, getStartPosition, getStructuralProperty, internalGetSetBooleanProperty, internalGetSetIntProperty, internalGetSetObjectProperty, nodeClassForType, postLazyInit, postReplaceChild, postValueChange, preLazyInit, preReplaceChild, preValueChange, properties, reapPropertyList, setFlags, setParent, setProperty, setSourceRange, setStructuralProperty, standardToString, stringSize, structuralPropertiesForType, subtreeBytes, subtreeMatch, supportedOnlyIn2, toString, unsupportedIn2
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PACKAGE_PROPERTY

public static final ChildPropertyDescriptor PACKAGE_PROPERTY
The "package" structural property of this node type.

Since:
3.0

IMPORTS_PROPERTY

public static final ChildListPropertyDescriptor IMPORTS_PROPERTY
The "imports" structural property of this node type.

Since:
3.0

TYPES_PROPERTY

public static final ChildListPropertyDescriptor TYPES_PROPERTY
The "types" structural property of this node type.

Since:
3.0

PROPERTY_DESCRIPTORS

private static final java.util.List PROPERTY_DESCRIPTORS
A list of property descriptors (element type: StructuralPropertyDescriptor), or null if uninitialized.

Since:
3.0

optionalCommentTable

Comment[] optionalCommentTable
The comment table, or null if none; initially null. This array is the storage underlying the optionalCommentList ArrayList.

Since:
3.0

optionalCommentList

private java.util.List optionalCommentList
The comment list (element type: Comment, or null if none; initially null.

Since:
3.0

optionalPackageDeclaration

private PackageDeclaration optionalPackageDeclaration
The package declaration, or null if none; initially null.


imports

private ASTNode.NodeList imports
The list of import declarations in textual order order; initially none (elementType: ImportDeclaration).


types

private ASTNode.NodeList types
The list of type declarations in textual order order; initially none (elementType: AbstractTypeDeclaration)


lineEndTable

private int[] lineEndTable
Line end table. If lineEndTable[i] == p then the line number i+1 ends at character position p. Except for the last line, the positions are that of the last character of the line delimiter. For example, the source string A\nB\nC has line end table {1, 3} (if \n is one character).


EMPTY_MESSAGES

private static final Message[] EMPTY_MESSAGES
Canonical empty list of messages.


EMPTY_PROBLEMS

private static final org.eclipse.jdt.core.compiler.IProblem[] EMPTY_PROBLEMS
Canonical empty list of problems.


messages

private Message[] messages
Messages reported by the compiler during parsing or name resolution.


problems

private org.eclipse.jdt.core.compiler.IProblem[] problems
Problems reported by the compiler during parsing or name resolution.


commentMapper

private DefaultCommentMapper commentMapper
The comment mapper, or null in none; initially null.

Since:
3.0
Constructor Detail

CompilationUnit

CompilationUnit(AST ast)
Creates a new AST node for a compilation owned by the given AST. The compilation unit initially has no package declaration, no import declarations, and no type declarations.

N.B. This constructor is package-private; all subclasses must be declared in the same package; clients are unable to declare additional subclasses.

Method Detail

propertyDescriptors

public static java.util.List propertyDescriptors(int apiLevel)
Returns a list of structural property descriptors for this node type. Clients must not modify the result.

Since:
3.0

setLineEndTable

void setLineEndTable(int[] lineEndTable)
Sets the line end table for this compilation unit. If lineEndTable[i] == p then line number i+1 ends at character position p. Except for the last line, the positions are that of (the last character of) the line delimiter. For example, the source string A\nB\nC has line end table {1, 3, 4}.


internalStructuralPropertiesForType

final java.util.List internalStructuralPropertiesForType(int apiLevel)
Description copied from class: ASTNode
Returns a list of property descriptors for this node type. Clients must not modify the result. This abstract method must be implemented in each concrete AST node type.

N.B. This method is package-private, so that the implementations of this method in each of the concrete AST node types do not clutter up the API doc.

Specified by:
internalStructuralPropertiesForType in class ASTNode

internalGetSetChildProperty

final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property,
                                          boolean get,
                                          ASTNode child)
Description copied from class: ASTNode
Sets the child value of the given property for this node. The default implementation of this method throws an exception explaining that this node does not have such a property. This method should be extended in subclasses that have at leasy one child property.

Overrides:
internalGetSetChildProperty in class ASTNode

internalGetChildListProperty

final java.util.List internalGetChildListProperty(ChildListPropertyDescriptor property)
Description copied from class: ASTNode
Returns the list value of the given property for this node. The default implementation of this method throws an exception explaining that this noed does not have such a property. This method should be extended in subclasses that have at leasy one child list property.

Overrides:
internalGetChildListProperty in class ASTNode

getNodeType0

final int getNodeType0()
Description copied from class: ASTNode
Returns an integer value identifying the type of this concrete AST node.

This internal method is implemented in each of the concrete node subclasses.

Specified by:
getNodeType0 in class ASTNode

clone0

ASTNode clone0(AST target)
Description copied from class: ASTNode
Returns a deep copy of the subtree of AST nodes rooted at this node. The resulting nodes are owned by the given AST, which may be different from the AST of this node. Even if this node has a parent, the result node will be unparented.

This method must be implemented in subclasses.

This method does not report pre- and post-clone events. All callers should instead call clone(AST) to ensure that pre- and post-clone events are reported.

N.B. This method is package-private, so that the implementations of this method in each of the concrete AST node types do not clutter up the API doc.

Specified by:
clone0 in class ASTNode

subtreeMatch0

final boolean subtreeMatch0(ASTMatcher matcher,
                            java.lang.Object other)
Description copied from class: ASTNode
Returns whether the subtree rooted at the given node matches the given other object as decided by the given matcher.

This internal method is implemented in each of the concrete node subclasses.

Specified by:
subtreeMatch0 in class ASTNode

accept0

void accept0(ASTVisitor visitor)
Description copied from class: ASTNode
Accepts the given visitor on a type-specific visit of the current node. This method must be implemented in all concrete AST node types.

General template for implementation on each concrete ASTNode class:

 
 boolean visitChildren = visitor.visit(this);
 if (visitChildren) {
    // visit children in normal left to right reading order
    acceptChild(visitor, getProperty1());
    acceptChildren(visitor, rawListProperty);
    acceptChild(visitor, getProperty2());
 }
 visitor.endVisit(this);
 
 
Note that the caller (accept) take cares of invoking visitor.preVisit(this) and visitor.postVisit(this).

Specified by:
accept0 in class ASTNode

getPackage

public PackageDeclaration getPackage()
Returns the node for the package declaration of this compilation unit, or null if this compilation unit is in the default package.


setPackage

public void setPackage(PackageDeclaration pkgDecl)
Sets or clears the package declaration of this compilation unit node to the given package declaration node.


imports

public java.util.List imports()
Returns the live list of nodes for the import declarations of this compilation unit, in order of appearance.


types

public java.util.List types()
Returns the live list of nodes for the top-level type declarations of this compilation unit, in order of appearance.

Note that in JLS3, the types may include both enum declarations and annotation type declarations introduced in J2SE 1.5. For JLS2, the elements are always TypeDeclaration.


findDeclaringNode

public ASTNode findDeclaringNode(IBinding binding)
Finds the corresponding AST node in the given compilation unit from which the given binding originated. Returns 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:

  • package - a PackageDeclaration
  • class or interface - a TypeDeclaration or a AnonymousClassDeclaration (for anonymous classes)
  • primitive type - none
  • array type - none
  • field - a VariableDeclarationFragment in a FieldDeclaration
  • local variable - a SingleVariableDeclaration, or a VariableDeclarationFragment in a VariableDeclarationStatement or VariableDeclarationExpression
  • method - a MethodDeclaration
  • constructor - a MethodDeclaration
  • annotation type - an AnnotationTypeDeclaration
  • annotation type member - an AnnotationTypeMemberDeclaration
  • enum type - an EnumDeclaration
  • enum constant - an EnumConstantDeclaration

Each call to ASTParser.createAST(org.eclipse.core.runtime.IProgressMonitor) 55 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.


findDeclaringNode

public ASTNode findDeclaringNode(java.lang.String key)
Finds the corresponding AST node in the given compilation unit from which the binding with the given key originated. Returns 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:

  • package - a PackageDeclaration
  • class or interface - a TypeDeclaration or a AnonymousClassDeclaration (for anonymous classes)
  • primitive type - none
  • array type - none
  • field - a VariableDeclarationFragment in a FieldDeclaration
  • local variable - a SingleVariableDeclaration, or a VariableDeclarationFragment in a VariableDeclarationStatement or VariableDeclarationExpression
  • method - a MethodDeclaration
  • constructor - a MethodDeclaration
  • annotation type - an AnnotationTypeDeclaration
  • annotation type member - an AnnotationTypeMemberDeclaration
  • enum type - an EnumDeclaration
  • enum constant - an EnumConstantDeclaration

Note that as explained in IBinding.getkey 55 there may be no keys for finding the declaring node for local variables, local or anonymous classes, etc.

Since:
2.1

getCommentMapper

DefaultCommentMapper getCommentMapper()
Returns the internal comment mapper.

Since:
3.0

initCommentMapper

void initCommentMapper(org.eclipse.jdt.internal.compiler.parser.Scanner scanner)
Initializes the internal comment mapper with the given scanner.

Since:
3.0

getExtendedStartPosition

public int getExtendedStartPosition(ASTNode node)
Returns the extended start position of the given node. Unlike ASTNode.getStartPosition() 55 and ASTNode.getLength() 55 , the extended source range may include comments and whitespace immediately before or after the normal source range for the node.

Since:
3.0

getExtendedLength

public int getExtendedLength(ASTNode node)
Returns the extended source length of the given node. Unlike ASTNode.getStartPosition() 55 and ASTNode.getLength() 55 , the extended source range may include comments and whitespace immediately before or after the normal source range for the node.

Since:
3.0

lineNumber

public int lineNumber(int position)
Returns the line number corresponding to the given source character position in the original source string. The initial line of the compilation unit is numbered 1, and each line extends through the last character of the end-of-line delimiter. The very last line extends through the end of the source string and has no line delimiter. For example, the source string 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.


getMessages

public Message[] getMessages()
Returns the list of messages reported by the compiler during the parsing or the type checking of this compilation unit. This list might be a subset of errors detected and reported by a Java compiler.

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

public org.eclipse.jdt.core.compiler.IProblem[] getProblems()
Returns the list of detailed problem reports noted by the compiler during the parsing or the type checking of this compilation unit. This list might be a subset of errors detected and reported by a Java compiler.

Simple clients that do little more than log the messages or display them to the user should probably call getMessages instead.

Since:
2.1

setProblems

void setProblems(org.eclipse.jdt.core.compiler.IProblem[] problems)
Sets the array of problems reported by the compiler during the parsing or name resolution of this compilation unit.


getCommentList

public java.util.List getCommentList()
Returns a list of the comments encountered while parsing this compilation unit.

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 55 . 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.

Note on comment parenting: The getParent() 55 of a doc comment associated with a body declaration is the body declaration node; for these comment nodes getRoot() 55 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() 55 will return null, and getRoot() 55 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 55 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 55 on each element.

Clients cannot modify the resulting list.

Since:
3.0

setCommentTable

void setCommentTable(Comment[] commentTable)
Sets the list of the comments encountered while parsing this compilation unit.

Since:
3.0

appendDebugString

void appendDebugString(java.lang.StringBuffer buffer)
Description copied from class: ASTNode
Appends a debug representation of this node to the given string buffer.

The ASTNode implementation of this method prints out the entire subtree. Subclasses may override to provide a more succinct representation.

Overrides:
appendDebugString in class ASTNode

memSize

int memSize()
Description copied from class: ASTNode
Returns an estimate of the memory footprint of this node in bytes. The estimate does not include the space occupied by child nodes.

Specified by:
memSize in class ASTNode

treeSize

int treeSize()
Description copied from class: ASTNode
Returns an estimate of the memory footprint in bytes of the entire subtree rooted at this node.

N.B. This method is package-private, so that the implementations of this method in each of the concrete AST node types do not clutter up the API doc.

Specified by:
treeSize in class ASTNode

recordModifications

public void recordModifications()
Enables the recording of changes to this compilation unit and its descendents. The compilation unit must have been created by 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.

Since:
3.0

rewrite

public org.eclipse.text.edits.TextEdit rewrite(org.eclipse.jface.text.IDocument document,
                                               java.util.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.

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.

Since:
3.0