Class CompilationUnit
The source range for this type of node is ordinarily the entire source file, including leading and trailing whitespace and comments.
CompilationUnit:
OrdinaryCompilationUnit
ModularCompilationUnit
OrdinaryCompilationUnit:
[ PackageDeclaration ]
{ ImportDeclaration }
{ TypeDeclaration | EnumDeclaration | AnnotationTypeDeclaration | ; }
ModularCompilationUnit:
{ImportDeclaration}
ModuleDeclaration
- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ChildListPropertyDescriptorThe "imports" structural property of this node type (element type:ImportDeclaration).static final ChildPropertyDescriptorThe "module" structural property of this node type (child type:ModuleDeclaration) (added in JLS9 API).static final ChildPropertyDescriptorThe "package" structural property of this node type (child type:PackageDeclaration).static final ChildListPropertyDescriptorThe "types" structural property of this node type (element type:AbstractTypeDeclaration).Fields inherited from class ASTNode
ANNOTATION_TYPE_DECLARATION, ANNOTATION_TYPE_MEMBER_DECLARATION, ANONYMOUS_CLASS_DECLARATION, ARRAY_ACCESS, ARRAY_CREATION, ARRAY_INITIALIZER, ARRAY_TYPE, ASSERT_STATEMENT, ASSIGNMENT, BLOCK, BLOCK_COMMENT, BOOLEAN_LITERAL, BREAK_STATEMENT, CASE_DEFAULT_EXPRESSION, CAST_EXPRESSION, CATCH_CLAUSE, CHARACTER_LITERAL, CLASS_INSTANCE_CREATION, COMPILATION_UNIT, CONDITIONAL_EXPRESSION, CONSTRUCTOR_INVOCATION, CONTINUE_STATEMENT, CREATION_REFERENCE, DIMENSION, DO_STATEMENT, EitherOr_MultiPattern, EMPTY_STATEMENT, ENHANCED_FOR_STATEMENT, ENUM_CONSTANT_DECLARATION, ENUM_DECLARATION, EXPORTS_DIRECTIVE, EXPRESSION_METHOD_REFERENCE, EXPRESSION_STATEMENT, FIELD_ACCESS, FIELD_DECLARATION, FOR_STATEMENT, GUARDED_PATTERN, IF_STATEMENT, IMPORT_DECLARATION, INFIX_EXPRESSION, INITIALIZER, INSTANCEOF_EXPRESSION, INTERSECTION_TYPE, JAVADOC, JAVADOC_REGION, JAVADOC_TEXT_ELEMENT, LABELED_STATEMENT, LAMBDA_EXPRESSION, LINE_COMMENT, MALFORMED, MARKER_ANNOTATION, MEMBER_REF, MEMBER_VALUE_PAIR, METHOD_DECLARATION, METHOD_INVOCATION, METHOD_REF, METHOD_REF_PARAMETER, MODIFIER, MODULE_DECLARATION, MODULE_MODIFIER, MODULE_QUALIFIED_NAME, NAME_QUALIFIED_TYPE, NORMAL_ANNOTATION, NULL_LITERAL, NULL_PATTERN, NUMBER_LITERAL, OPENS_DIRECTIVE, ORIGINAL, PACKAGE_DECLARATION, PARAMETERIZED_TYPE, PARENTHESIZED_EXPRESSION, PATTERN_INSTANCEOF_EXPRESSION, POSTFIX_EXPRESSION, PREFIX_EXPRESSION, PRIMITIVE_TYPE, PROTECT, PROVIDES_DIRECTIVE, QUALIFIED_NAME, QUALIFIED_TYPE, RECORD_DECLARATION, RECORD_PATTERN, RECOVERED, REQUIRES_DIRECTIVE, RETURN_STATEMENT, SIMPLE_NAME, SIMPLE_TYPE, SINGLE_MEMBER_ANNOTATION, SINGLE_VARIABLE_DECLARATION, STRING_LITERAL, SUPER_CONSTRUCTOR_INVOCATION, SUPER_FIELD_ACCESS, SUPER_METHOD_INVOCATION, SUPER_METHOD_REFERENCE, SWITCH_CASE, SWITCH_EXPRESSION, SWITCH_STATEMENT, SYNCHRONIZED_STATEMENT, TAG_ELEMENT, TAG_PROPERTY, TEXT_BLOCK, TEXT_ELEMENT, THIS_EXPRESSION, THROW_STATEMENT, TRY_STATEMENT, TYPE_DECLARATION, TYPE_DECLARATION_STATEMENT, TYPE_LITERAL, TYPE_METHOD_REFERENCE, TYPE_PARAMETER, TYPE_PATTERN, UNION_TYPE, UNNAMED_CLASS, USES_DIRECTIVE, VARIABLE_DECLARATION_EXPRESSION, VARIABLE_DECLARATION_FRAGMENT, VARIABLE_DECLARATION_STATEMENT, WHILE_STATEMENT, WILDCARD_TYPE, YIELD_STATEMENTModifier and TypeFieldDescriptionstatic final intNode type constant indicating a node of typeAnnotationTypeDeclaration.static final intNode type constant indicating a node of typeAnnotationTypeMemberDeclaration.static final intNode type constant indicating a node of typeAnonymousClassDeclaration.static final intNode type constant indicating a node of typeArrayAccess.static final intNode type constant indicating a node of typeArrayCreation.static final intNode type constant indicating a node of typeArrayInitializer.static final intNode type constant indicating a node of typeArrayType.static final intNode type constant indicating a node of typeAssertStatement.static final intNode type constant indicating a node of typeAssignment.static final intNode type constant indicating a node of typeBlock.static final intNode type constant indicating a node of typeBlockComment.static final intNode type constant indicating a node of typeBooleanLiteral.static final intNode type constant indicating a node of typeBreakStatement.static final intNode type constant indicating a node of typeCaseDefaultExpression.static final intNode type constant indicating a node of typeCastExpression.static final intNode type constant indicating a node of typeCatchClause.static final intNode type constant indicating a node of typeCharacterLiteral.static final intNode type constant indicating a node of typeClassInstanceCreation.static final intNode type constant indicating a node of typeCompilationUnit.static final intNode type constant indicating a node of typeConditionalExpression.static final intNode type constant indicating a node of typeConstructorInvocation.static final intNode type constant indicating a node of typeContinueStatement.static final intNode type constant indicating a node of typeCreationReference.static final intNode type constant indicating a node of typeDimension.static final intNode type constant indicating a node of typeDoStatement.static final intNode type constant indicating a node of typeEitherOrMultiPattern.static final intNode type constant indicating a node of typeEmptyStatement.static final intNode type constant indicating a node of typeEnhancedForStatement.static final intNode type constant indicating a node of typeEnumConstantDeclaration.static final intNode type constant indicating a node of typeEnumDeclaration.static final intNode type constant indicating a node of typeExportsDirective.static final intNode type constant indicating a node of typeExpressionMethodReference.static final intNode type constant indicating a node of typeExpressionStatement.static final intNode type constant indicating a node of typeFieldAccess.static final intNode type constant indicating a node of typeFieldDeclaration.static final intNode type constant indicating a node of typeForStatement.static final intNode type constant indicating a node of typeGuardedPattern.static final intNode type constant indicating a node of typeIfStatement.static final intNode type constant indicating a node of typeImportDeclaration.static final intNode type constant indicating a node of typeInfixExpression.static final intNode type constant indicating a node of typeInitializer.static final intNode type constant indicating a node of typeInstanceofExpression.static final intNode type constant indicating a node of typeIntersectionType.static final intNode type constant indicating a node of typeJavadoc.static final intNode type constant indicating a node of typeJavaDocRegion.static final intNode type constant indicating a node of typeTextElement.static final intNode type constant indicating a node of typeLabeledStatement.static final intNode type constant indicating a node of typeLambdaExpression.static final intNode type constant indicating a node of typeLineComment.static final intFlag constant (bit mask, value 1) indicating that there is something not quite right with this AST node.static final intNode type constant indicating a node of typeMarkerAnnotation.static final intNode type constant indicating a node of typeMemberRef.static final intNode type constant indicating a node of typeMemberValuePair.static final intNode type constant indicating a node of typeMethodDeclaration.static final intNode type constant indicating a node of typeMethodInvocation.static final intNode type constant indicating a node of typeMethodRef.static final intNode type constant indicating a node of typeMethodRefParameter.static final intNode type constant indicating a node of typeModifier.static final intNode type constant indicating a node of typeModuleDeclaration.static final intNode type constant indicating a node of typeModuleModifier.static final intNode type constant indicating a node of typeModuleQualifiedName.static final intNode type constant indicating a node of typeNameQualifiedType.static final intNode type constant indicating a node of typeNormalAnnotation.static final intNode type constant indicating a node of typeNullLiteral.static final intNode type constant indicating a node of typeNullPattern.static final intNode type constant indicating a node of typeNumberLiteral.static final intNode type constant indicating a node of typeOpensDirective.static final intFlag constant (bit mask, value 2) indicating that this is a node that was created by the parser (as opposed to one created by another party).static final intNode type constant indicating a node of typePackageDeclaration.static final intNode type constant indicating a node of typeParameterizedType.static final intNode type constant indicating a node of typeParenthesizedExpression.static final intNode type constant indicating a node of typePatternInstanceofExpression.static final intNode type constant indicating a node of typePostfixExpression.static final intNode type constant indicating a node of typePrefixExpression.static final intNode type constant indicating a node of typePrimitiveType.static final intFlag constant (bit mask, value 4) indicating that this node is unmodifiable.static final intNode type constant indicating a node of typeProvidesDirective.static final intNode type constant indicating a node of typeQualifiedName.static final intNode type constant indicating a node of typeQualifiedType.static final intNode type constant indicating a node of typeRecordDeclaration.static final intNode type constant indicating a node of typeRecordPattern.static final intFlag constant (bit mask, value 8) indicating that this node or a part of this node is recovered from source that contains a syntax error detected in the vicinity.static final intNode type constant indicating a node of typeRequiresDirective.static final intNode type constant indicating a node of typeReturnStatement.static final intNode type constant indicating a node of typeSimpleName.static final intNode type constant indicating a node of typeSimpleType.static final intNode type constant indicating a node of typeSingleMemberAnnotation.static final intNode type constant indicating a node of typeSingleVariableDeclaration.static final intNode type constant indicating a node of typeStringLiteral.static final intNode type constant indicating a node of typeSuperConstructorInvocation.static final intNode type constant indicating a node of typeSuperFieldAccess.static final intNode type constant indicating a node of typeSuperMethodInvocation.static final intNode type constant indicating a node of typeSuperMethhodReference.static final intNode type constant indicating a node of typeSwitchCase.static final intNode type constant indicating a node of typeSwitchExpression.static final intNode type constant indicating a node of typeSwitchStatement.static final intNode type constant indicating a node of typeSynchronizedStatement.static final intNode type constant indicating a node of typeTagElement.static final intNode type constant indicating a node of typeTagProperty.static final intNode type constant indicating a node of typeTextBlock.static final intNode type constant indicating a node of typeTextElement.static final intNode type constant indicating a node of typeThisExpression.static final intNode type constant indicating a node of typeThrowStatement.static final intNode type constant indicating a node of typeTryStatement.static final intNode type constant indicating a node of typeTypeDeclaration.static final intNode type constant indicating a node of typeTypeDeclarationStatement.static final intNode type constant indicating a node of typeTypeLiteral.static final intNode type constant indicating a node of typeTypeMethodReference.static final intNode type constant indicating a node of typeTypeParameter.static final intNode type constant indicating a node of typeTypePattern.static final intNode type constant indicating a node of typeUnionType.static final intstatic final intNode type constant indicating a node of typeUsesDirective.static final intNode type constant indicating a node of typeVariableDeclarationExpression.static final intNode type constant indicating a node of typeVariableDeclarationFragment.static final intNode type constant indicating a node of typeVariableDeclarationStatement.static final intNode type constant indicating a node of typeWhileStatement.static final intNode type constant indicating a node of typeWildcardType.static final intNode type constant indicating a node of typeYieldStatement. -
Method Summary
Modifier and TypeMethodDescriptionfindDeclaringNode(String key) Finds the corresponding AST node in the given compilation unit from which the binding with the given key originated.findDeclaringNode(IBinding binding) Finds the corresponding AST node in the given compilation unit from which the given binding originated.intReturn the index in the whole comments listgetCommentList()of the first leading comments associated with the given node.intgetColumnNumber(int position) Returns the column number corresponding to the given source character position in the original source string.Returns a list of the comments encountered while parsing this compilation unit.intgetExtendedLength(ASTNode node) Returns the extended source length of the given node.intReturns the extended start position of the given node.The Java element (anorg.aspectj.org.eclipse.jdt.core.ICompilationUnitor anorg.aspectj.org.eclipse.jdt.core.IClassFile) this compilation unit was created from, ornullif it was not created from a Java element.intgetLineNumber(int position) Returns the line number corresponding to the given source character position in the original source string.Message[]Returns the list of messages reported by the compiler during the parsing or the type checking of this compilation unit.Returns the node for the module declaration of this compilation unit, ornullif this compilation unit is not a module info.Returns the node for the package declaration of this compilation unit, ornullif this compilation unit is in the default package.intgetPosition(int line, int column) Given a line number and column number, returns the corresponding position in the original source string.IProblem[]Returns the list of detailed problem reports noted by the compiler during the parsing or the type checking of this compilation unit.Internal method This method return internal data used to perform statements recovery.The Java type root (acompilation unitor aclass file) this compilation unit was created from, ornullif it was not created from a Java type root.imports()Returns the live list of nodes for the import declarations of this compilation unit, in order of appearance.intReturn the index in the whole comments listgetCommentList()of the last trailing comments associated with the given node.intlineNumber(int position) Deprecated.Use getLineNumber(int) instead.static ListpropertyDescriptors(int apiLevel) Returns a list of structural property descriptors for this node type.voidEnables the recording of changes to this compilation unit and its descendants.org.eclipse.text.edits.TextEditConverts 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.voidsetModule(ModuleDeclaration module) Sets or clears the module declaration of this compilation unit node to the given module declaration node.voidsetPackage(PackageDeclaration pkgDecl) Sets or clears the package declaration of this compilation unit node to the given package declaration node.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 ASTNode
accept, copySubtree, copySubtrees, delete, equals, getAST, getFlags, getLength, getLocationInParent, getNodeType, getParent, getProperty, getRoot, getStartPosition, getStructuralProperty, hashCode, nodeClassForType, properties, setFlags, setProperty, setSourceRange, setStructuralProperty, structuralPropertiesForType, subtreeBytes, subtreeMatch, toStringModifier and TypeMethodDescriptionfinal voidaccept(ASTVisitor visitor) Accepts the given visitor on a visit of the current node.static ASTNodecopySubtree(AST target, ASTNode node) Returns a deep copy of the subtree of AST nodes rooted at the given node.static ListcopySubtrees(AST target, List nodes) Returns a deep copy of the subtrees of AST nodes rooted at the given list of nodes.final voiddelete()Removes this node from its parent.final booleanTheASTNodeimplementation of thisObjectmethod uses object identity (==).final ASTgetAST()Returns this node's AST.final intgetFlags()Returns the flags associated with this node.final intReturns the length in characters of the original source file indicating where the source fragment corresponding to this node ends.Returns the location of this node within its parent, ornullif this is a root node.final intReturns an integer value identifying the type of this concrete AST node.final ASTNodeReturns this node's parent node, ornullif this is the root node.final ObjectgetProperty(String propertyName) Returns the value of the named property of this node, ornullif none.final ASTNodegetRoot()Returns the root node at or above this node; returns this node if it is a root.final intReturns the character index into the original source file indicating where the source fragment corresponding to this node begins.final ObjectReturns the value of the given structural property for this node.final inthashCode()static ClassnodeClassForType(int nodeType) Returns the node class for the corresponding node type.final MapReturns an unmodifiable table of the properties of this node with non-nullvalues.final voidsetFlags(int flags) Sets the flags associated with this node to the given value.final voidsetProperty(String propertyName, Object data) Sets the named property of this node to the given value, or tonullto clear it.final voidsetSourceRange(int startPosition, int length) Sets the source range of the original source file where the source fragment corresponding to this node was found.final voidsetStructuralProperty(StructuralPropertyDescriptor property, Object value) Sets the value of the given structural property for this node.final ListReturns a list of structural property descriptors for nodes of the same type as this node.final intReturns an estimate of the memory footprint in bytes of the entire subtree rooted at this node.final booleansubtreeMatch(ASTMatcher matcher, Object other) Returns whether the subtree rooted at the given node matches the given other object as decided by the given matcher.final StringtoString()Returns a string representation of this node suitable for debugging purposes only.
-
Field Details
-
IMPORTS_PROPERTY
The "imports" structural property of this node type (element type:ImportDeclaration).- Since:
- 3.0
-
PACKAGE_PROPERTY
The "package" structural property of this node type (child type:PackageDeclaration).- Since:
- 3.0
-
MODULE_PROPERTY
The "module" structural property of this node type (child type:ModuleDeclaration) (added in JLS9 API).- Since:
- 3.14
-
TYPES_PROPERTY
The "types" structural property of this node type (element type:AbstractTypeDeclaration).- Since:
- 3.0
-
-
Method Details
-
propertyDescriptors
Returns a list of structural property descriptors for this node type. Clients must not modify the result.- Parameters:
apiLevel- the API level; one of theAST.JLS*constants- Returns:
- a list of property descriptors (element type:
StructuralPropertyDescriptor) - Since:
- 3.0
-
getColumnNumber
public int getColumnNumber(int position) Returns the column number corresponding to the given source character position in the original source string. Column number are zero-based. Return-1if it is beyond the valid range or-2if the column number information is unknown.- Parameters:
position- a 0-based character position, possibly negative or out of range- Returns:
- the 0-based column number, or
-1if the character position does not correspond to a source line in the original source file or-2if column number information is unknown for this compilation unit - Since:
- 3.2
- See Also:
-
findDeclaringNode
Finds the corresponding AST node in the given compilation unit from which the given binding originated. Returnsnullif the binding does not correspond to any node in this compilation unit. This method always returnsnullif 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
TypeDeclarationor aAnonymousClassDeclaration(for anonymous classes) - primitive type - none
- array type - none
- field - a
VariableDeclarationFragmentin aFieldDeclaration - local variable - a
SingleVariableDeclaration, or aVariableDeclarationFragmentin aVariableDeclarationStatementorVariableDeclarationExpression - method - a
MethodDeclaration - constructor - a
MethodDeclaration - annotation type - an
AnnotationTypeDeclaration - annotation type member - an
AnnotationTypeMemberDeclaration - enum type - an
EnumDeclaration - enum constant - an
EnumConstantDeclaration - type variable - a
TypeParameter - capture binding - none
- annotation binding - an
Annotation - member value pair binding - an
MemberValuePair, ornullif it represents a default value or a single member value
For parameterized or raw type bindings, the declaring node is that of the corresponding generic type. And for parameterized or raw method bindings, the declaring node is that of the corresponding generic method.
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 returnsnullwhen the binding object comes from a different AST. UsefindDeclaringNode(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
nullif 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:
- package - a
-
findDeclaringNode
Finds the corresponding AST node in the given compilation unit from which the binding with the given key originated. Returnsnullif the corresponding node cannot be determined. This method always returnsnullif 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
TypeDeclarationor aAnonymousClassDeclaration(for anonymous classes) - primitive type - none
- array type - none
- field - a
VariableDeclarationFragmentin aFieldDeclaration - local variable - a
SingleVariableDeclaration, or aVariableDeclarationFragmentin aVariableDeclarationStatementorVariableDeclarationExpression - method - a
MethodDeclaration - constructor - a
MethodDeclaration - annotation type - an
AnnotationTypeDeclaration - annotation type member - an
AnnotationTypeMemberDeclaration - enum type - an
EnumDeclaration - enum constant - an
EnumConstantDeclaration - type variable - a
TypeParameter - capture binding - none
- Parameters:
key- the binding key, ornull- Returns:
- the corresponding node where a binding with the given
key is declared, or
nullif the key isnullor 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:
- package - a
-
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. 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()of a doc comment associated with a body declaration is the body declaration node; for these comment nodesgetRoot()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 returnnull, andgetRoot()will return the comment node itself, indicating that these comment nodes are not directly connected to the AST for the compilation unit. TheComment.getAlternateRootmethod 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
accepton each element.Clients cannot modify the resulting list.
- Returns:
- an unmodifiable list of comments in increasing order of source
start position (element type:
Comment, ornullif comment information for this compilation unit is not available - Since:
- 3.0
- See Also:
-
getExtendedLength
Returns the extended source length of the given node. UnlikeASTNode.getStartPosition()andASTNode.getLength(), the extended source range may include comments and whitespace immediately before or after the normal source range for the node.- Parameters:
node- the node- Returns:
- a (possibly 0) length, or
0if no source position information is recorded for this node - Since:
- 3.0
- See Also:
-
getExtendedStartPosition
Returns the extended start position of the given node. UnlikeASTNode.getStartPosition()andASTNode.getLength(), the extended source range may include comments and whitespace immediately before or after the normal source range for the node.- Parameters:
node- the node- Returns:
- the 0-based character index, or
-1if no source position information is recorded for this node - Since:
- 3.0
- See Also:
-
getJavaElement
The Java element (anorg.aspectj.org.eclipse.jdt.core.ICompilationUnitor anorg.aspectj.org.eclipse.jdt.core.IClassFile) this compilation unit was created from, ornullif it was not created from a Java element.- Returns:
- the Java element this compilation unit was created from, or
nullif none - Since:
- 3.1
- See Also:
-
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
getProblemsto get compiler problem objects.- Returns:
- the list of messages, possibly empty
- See Also:
-
getModule
Returns the node for the module declaration of this compilation unit, ornullif this compilation unit is not a module info.- Returns:
- the module declaration node, or
nullif none - Throws:
UnsupportedOperationException- if this operation is used below JLS9- Since:
- 3.14
-
getPackage
Returns the node for the package declaration of this compilation unit, ornullif this compilation unit is in the default package.- Returns:
- the package declaration node, or
nullif none
-
getPosition
public int getPosition(int line, int column) Given a line number and column number, returns the corresponding position in the original source string. Returns -2 if no line number information is available for this compilation unit. Returns the total size of the source string iflineis greater than the actual number lines in the unit. Returns -1 ifcolumnis less than 0, or the position of the last character of the line ifcolumnis beyond the legal range, or the given line number is less than one.- Parameters:
line- the one-based line numbercolumn- the zero-based column number- Returns:
- the 0-based character position in the source string;
-2if line/column number information is not known for this compilation unit or-1the inputs are not valid - Since:
- 3.2
-
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
getMessagesinstead.- Returns:
- the list of detailed problem objects, possibly empty
- Since:
- 2.1
- See Also:
-
getStatementsRecoveryData
Internal method This method return internal data used to perform statements recovery.- Returns:
- internal data used to perform statements recovery.
- Since:
- 3.5
-
getTypeRoot
The Java type root (acompilation unitor aclass file) this compilation unit was created from, ornullif it was not created from a Java type root.- Returns:
- the Java type root this compilation unit was created from, or
nullif none - Since:
- 3.3
-
imports
Returns the live list of nodes for the import declarations of this compilation unit, in order of appearance.- Returns:
- the live list of import declaration nodes
(elementType:
ImportDeclaration)
-
firstLeadingCommentIndex
Return the index in the whole comments listgetCommentList()of the first leading comments associated with the given node.- Parameters:
node- the node- Returns:
- 0-based index of first leading comment or -1 if node has no associated comment before its start position.
- Since:
- 3.2
-
lastTrailingCommentIndex
Return the index in the whole comments listgetCommentList()of the last trailing comments associated with the given node.- Parameters:
node- the node- Returns:
- 0-based index of last trailing comment or -1 if node has no associated comment after its end position.
- Since:
- 3.2
-
lineNumber
public int lineNumber(int position) Deprecated.Use getLineNumber(int) instead. Be careful to handle the negative values.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 stringclass 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
1if 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:
-
getLineNumber
public int getLineNumber(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 stringclass 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 -2 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
-1if the character position does not correspond to a source line in the original source file or-2if line number information is not known for this compilation unit - Since:
- 3.2
- See Also:
-
recordModifications
public void recordModifications()Enables the recording of changes to this compilation unit and its descendants. The compilation unit must have been created byASTParserand 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, callrewrite(IDocument, Map)to get an object representing the corresponding edits to the original source code string.Note that this way of manipulating an AST only allows a single line of modifications, and it lacks important functionality like
string placeholdersand support forcopyingnodes including comments and formatting.To future-proof your code, consider using an external
ASTRewriteinstead, which doesn't suffer from these limitations and allows to modify an AST in a non-destructive way.- Throws:
IllegalArgumentException- if this compilation unit is marked as unmodifiable, or if this compilation unit has already been tampered with, or recording has already been enabled- Since:
- 3.0
- See Also:
-
rewrite
public org.eclipse.text.edits.TextEdit rewrite(org.eclipse.jface.text.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.The compilation unit must have been created by
ASTParserfrom the source code string in the given document, and recording must have been turned on with a prior call torecordModifications()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 effect of all those changes.
- Parameters:
document- original document containing source code for this compilation unitoptions- the table of formatter options (key type:String; value type:String); ornullto use the standard global optionsJavaCore.getOptions().- Returns:
- text edit object describing the changes to the document corresponding to the recorded AST modifications
- Throws:
IllegalArgumentException- if the document passed isnullor does not correspond to this ASTIllegalStateException- ifrecordModificationswas not called to enable recording- Since:
- 3.0
- See Also:
-
setModule
Sets or clears the module declaration of this compilation unit node to the given module declaration node.- Parameters:
module- the new module declaration node, ornullif this compilation unit does not have a module- Throws:
IllegalArgumentException- if:- the node belongs to a different AST
- the node already has a parent
UnsupportedOperationException- if this operation is used below JLS9- Since:
- 3.14
-
setPackage
Sets or clears the package declaration of this compilation unit node to the given package declaration node.- Parameters:
pkgDecl- the new package declaration node, ornullif 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
-
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 5. For JLS2, the elements are always
TypeDeclaration.- Returns:
- the live list of top-level type declaration
nodes (element type:
AbstractTypeDeclaration)
-