Eclipse JDT
2.1

org.eclipse.jdt.core.dom
Class CompilationUnit

java.lang.Object
  |
  +--org.eclipse.jdt.core.dom.ASTNode
        |
        +--org.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.

 CompilationUnit:
    [ PackageDeclaration ]
    { ImportDeclaration }
    { TypeDeclaration | ; }
 

Since:
2.0

Fields inherited from class org.eclipse.jdt.core.dom.ASTNode
ANONYMOUS_CLASS_DECLARATION, ARRAY_ACCESS, ARRAY_CREATION, ARRAY_INITIALIZER, ARRAY_TYPE, ASSERT_STATEMENT, ASSIGNMENT, BLOCK, BOOLEAN_LITERAL, BREAK_STATEMENT, CAST_EXPRESSION, CATCH_CLAUSE, CHARACTER_LITERAL, CLASS_INSTANCE_CREATION, COMPILATION_UNIT, CONDITIONAL_EXPRESSION, CONSTRUCTOR_INVOCATION, CONTINUE_STATEMENT, DO_STATEMENT, EMPTY_STATEMENT, EXPRESSION_STATEMENT, FIELD_ACCESS, FIELD_DECLARATION, FOR_STATEMENT, IF_STATEMENT, IMPORT_DECLARATION, INFIX_EXPRESSION, INITIALIZER, INSTANCEOF_EXPRESSION, JAVADOC, LABELED_STATEMENT, MALFORMED, METHOD_DECLARATION, METHOD_INVOCATION, NULL_LITERAL, NUMBER_LITERAL, PACKAGE_DECLARATION, PARENTHESIZED_EXPRESSION, POSTFIX_EXPRESSION, PREFIX_EXPRESSION, PRIMITIVE_TYPE, QUALIFIED_NAME, RETURN_STATEMENT, SIMPLE_NAME, SIMPLE_TYPE, SINGLE_VARIABLE_DECLARATION, STRING_LITERAL, SUPER_CONSTRUCTOR_INVOCATION, SUPER_FIELD_ACCESS, SUPER_METHOD_INVOCATION, SWITCH_CASE, SWITCH_STATEMENT, SYNCHRONIZED_STATEMENT, THIS_EXPRESSION, THROW_STATEMENT, TRY_STATEMENT, TYPE_DECLARATION, TYPE_DECLARATION_STATEMENT, TYPE_LITERAL, VARIABLE_DECLARATION_EXPRESSION, VARIABLE_DECLARATION_FRAGMENT, VARIABLE_DECLARATION_STATEMENT, WHILE_STATEMENT
 
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.
 Message[] getMessages()
          Returns the list of messages reported by the compiler during the parsing or the type checking of this compilation unit.
 int getNodeType()
          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.
 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 declaration 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.
 void setPackage(PackageDeclaration pkgDecl)
          Sets or clears the package declaration of this compilation unit node to the given package declaration node.
 boolean subtreeMatch(ASTMatcher matcher, Object other)
          Returns whether the subtree rooted at the given node matches the given other object as decided by the given matcher.
 List types()
          Returns the live list of nodes for the top-level type declaration of this compilation unit, in order of appearance.
 
Methods inherited from class org.eclipse.jdt.core.dom.ASTNode
accept, copySubtree, copySubtrees, equals, getAST, getFlags, getLength, getParent, getProperty, getRoot, getStartPosition, properties, setFlags, setProperty, setSourceRange, subtreeBytes, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getNodeType

public int getNodeType()
Description copied from class: ASTNode
Returns an integer value identifying the type of this concrete AST node. The values are small positive integers, suitable for use in switch statements.

For each concrete node type there is a unique node type constant (name and value). The unique node type constant for a concrete node type such as CastExpression is ASTNode.CAST_EXPRESSION.

Overrides:
getNodeType in class ASTNode
Following copied from class: org.eclipse.jdt.core.dom.ASTNode
Returns:
one of the node type constants

subtreeMatch

public boolean subtreeMatch(ASTMatcher matcher,
                            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.
Overrides:
subtreeMatch in class ASTNode
Following copied from class: org.eclipse.jdt.core.dom.ASTNode
Parameters:
matcher - the matcher
other - the other object, or null
Returns:
true if the subtree matches, or false if they do not match

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.
Returns:
the package declaration node, or null if none

setPackage

public void setPackage(PackageDeclaration pkgDecl)
Sets or clears the package declaration of this compilation unit node to the given package declaration node.
Parameters:
pkgDecl - the new package declaration node, or null if this compilation unit does not have a package declaration (that is in the default package)
Throws:
IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent

imports

public List imports()
Returns the live list of nodes for the import declaration of this compilation unit, in order of appearance.
Returns:
the live list of import declaration nodes (elementType: ImportDeclaration)

types

public List types()
Returns the live list of nodes for the top-level type declaration of this compilation unit, in order of appearance.
Returns:
the live list of top-level type declaration nodes (elementType: 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:

Each call to AST.parseCompilationUnit 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.

Parameters:
binding - the binding
Returns:
the corresponding node where the given binding is declared, or null if the binding does not correspond to a node in this compilation unit or if bindings were not requested when this AST was built
See Also:
findDeclaringNode(java.lang.String)

findDeclaringNode

public ASTNode findDeclaringNode(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:

Parameters:
key - the binding key, or null
Returns:
the corresponding node where a binding with the given key is declared, or 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 built
Since:
2.1
See Also:
IBinding.getKey()

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.
Parameters:
position - a 0-based character position, possibly negative or out of range
Returns:
the 1-based line number, or 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 unit
See Also:
AST.parseCompilationUnit(org.eclipse.jdt.core.ICompilationUnit, boolean)

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.

Returns:
the list of messages, possibly empty
See Also:
getProblems(), AST.parseCompilationUnit(org.eclipse.jdt.core.ICompilationUnit, boolean)

getProblems

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

Returns:
the list of detailed problem objects, possibly empty
Since:
2.1
See Also:
getMessages(), AST.parseCompilationUnit(org.eclipse.jdt.core.ICompilationUnit, boolean)

Eclipse JDT
2.1

Copyright (c) IBM Corp. and others 2000, 2003. All Rights Reserved.