<s> package org . eclipse . jdt . core . dom ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . List ; import java . util . Map ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . ITypeRoot ; import org . eclipse . jdt . core . compiler . IProblem ; import org . eclipse . jdt . internal . compiler . parser . Scanner ; import org . eclipse . jdt . internal . compiler . util . Util ; import org . eclipse . jface . text . IDocument ; import org . eclipse . text . edits . TextEdit ; public class CompilationUnit extends ASTNode { private static final Message [ ] EMPTY_MESSAGES = new Message [ 0 ] ; private static final IProblem [ ] EMPTY_PROBLEMS = new IProblem [ 0 ] ; public static final ChildListPropertyDescriptor IMPORTS_PROPERTY = new ChildListPropertyDescriptor ( CompilationUnit . class , "imports" , ImportDeclaration . class , NO_CYCLE_RISK ) ; public static final ChildPropertyDescriptor PACKAGE_PROPERTY = new ChildPropertyDescriptor ( CompilationUnit . class , "package" , PackageDeclaration . class , OPTIONAL , NO_CYCLE_RISK ) ; private static final List PROPERTY_DESCRIPTORS ; public static final ChildListPropertyDescriptor TYPES_PROPERTY = new ChildListPropertyDescriptor ( CompilationUnit . class , "types" , AbstractTypeDeclaration . class , CYCLE_RISK ) ; static { List properyList = new ArrayList ( 4 ) ; createPropertyList ( CompilationUnit . class , properyList ) ; addProperty ( PACKAGE_PROPERTY , properyList ) ; addProperty ( IMPORTS_PROPERTY , properyList ) ; addProperty ( TYPES_PROPERTY , properyList ) ; PROPERTY_DESCRIPTORS = reapPropertyList ( properyList ) ; } public static List propertyDescriptors ( int apiLevel ) { return PROPERTY_DESCRIPTORS ; } private DefaultCommentMapper commentMapper = null ; private ITypeRoot typeRoot = null ; private ASTNode . NodeList imports = new ASTNode . NodeList ( IMPORTS_PROPERTY ) ; private int [ ] lineEndTable = Util . EMPTY_INT_ARRAY ; private Message [ ] messages ; private List optionalCommentList = null ; Comment [ ] optionalCommentTable = null ; private PackageDeclaration optionalPackageDeclaration = null ; private IProblem [ ] problems = EMPTY_PROBLEMS ; private Object statementsRecoveryData ; private ASTNode . NodeList types = new ASTNode . NodeList ( TYPES_PROPERTY ) ; protected CompilationUnit ( AST ast ) { super ( ast ) ; } protected void accept0 ( ASTVisitor visitor ) { boolean visitChildren = visitor . visit ( this ) ; if ( visitChildren ) { acceptChild ( visitor , getPackage ( ) ) ; acceptChildren ( visitor , this . imports ) ; acceptChildren ( visitor , this . types ) ; } visitor . endVisit ( this ) ; } ASTNode clone0 ( AST target ) { CompilationUnit result = new CompilationUnit ( target ) ; result . setSourceRange ( getStartPosition ( ) , getLength ( ) ) ; result . setPackage ( ( PackageDeclaration ) ASTNode . copySubtree ( target , getPackage ( ) ) ) ; result . imports ( ) . addAll ( ASTNode . copySubtrees ( target , imports ( ) ) ) ; result . types ( ) . addAll ( ASTNode . copySubtrees ( target , types ( ) ) ) ; return result ; } public int getColumnNumber ( final int position ) { if ( this . lineEndTable == null ) return - 2 ; final int line = getLineNumber ( position ) ; if ( line == - 1 ) { return - 1 ; } if ( line == 1 ) { if ( position >= getStartPosition ( ) + getLength ( ) ) return - 1 ; return position ; } int length = this . lineEndTable . length ; final int previousLineOffset = this . lineEndTable [ line - 2 ] ; final int offsetForLine = previousLineOffset + 1 ; final int currentLineEnd = line == length + 1 ? getStartPosition ( ) + getLength ( ) - 1 : this . lineEndTable [ line - 1 ] ; if ( offsetForLine > currentLineEnd ) { return - 1 ; } else { return position - offsetForLine ; } } public ASTNode findDeclaringNode ( IBinding binding ) { return this . ast . getBindingResolver ( ) . findDeclaringNode ( binding ) ; } public ASTNode findDeclaringNode ( String key ) { return this . ast . getBindingResolver ( ) . findDeclaringNode ( key ) ; } public List getCommentList ( ) { return this . optionalCommentList ; } DefaultCommentMapper getCommentMapper ( ) { return this . commentMapper ; } public int getExtendedLength ( ASTNode node ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } if ( this . commentMapper == null || node . getAST ( ) != getAST ( ) ) { return node . getLength ( ) ; } else { return this . commentMapper . getExtendedLength ( node ) ; } } public int getExtendedStartPosition ( ASTNode node ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } if ( this . commentMapper == null || node . getAST ( ) != getAST ( ) ) { return node . getStartPosition ( ) ; } else { return this . commentMapper . getExtendedStartPosition ( node ) ; } } public IJavaElement getJavaElement ( ) { return this . typeRoot ; } public Message [ ] getMessages ( ) { if ( this . messages == null ) { int problemLength = this . problems . length ; if ( problemLength == 0 ) { this . messages = EMPTY_MESSAGES ; } else { this . messages = new Message [ problemLength ] ; for ( int i = 0 ; i < problemLength ; i ++ ) { IProblem problem = this . problems [ i ] ; int start = problem . getSourceStart ( ) ; int end = problem . getSourceEnd ( ) ; this . messages [ i ] = new Message ( problem . getMessage ( ) , start , end - start + 1 ) ; } } } return this . messages ; } final int getNodeType0 ( ) { return COMPILATION_UNIT ; } public PackageDeclaration getPackage ( ) { return this . optionalPackageDeclaration ; } public int getPosition ( int line , int column ) { if ( this . lineEndTable == null ) return - 2 ; if ( line < 1 || column < 0 ) return - 1 ; int length ; if ( ( length = this . lineEndTable . length ) == 0 ) { if ( line != 1 ) return - 1 ; return column >= getStartPosition ( ) + getLength ( ) ? - 1 : column ; } if ( line == 1 ) { final int endOfLine = this . lineEndTable [ 0 ] ; return column > endOfLine ? - 1 : column ; } else if ( line > length + 1 ) { return - 1 ; } final int previousLineOffset = this . lineEndTable [ line - 2 ] ; final int offsetForLine = previousLineOffset + 1 ; final int currentLineEnd = line == length + 1 ? getStartPosition ( ) + getLength ( ) - 1 : this . lineEndTable [ line - 1 ] ; if ( ( offsetForLine + column ) > currentLineEnd ) { return - 1 ; } else { return offsetForLine + column ; } } public IProblem [ ] getProblems ( ) { return this . problems ; } public Object getStatementsRecoveryData ( ) { return this . statementsRecoveryData ; } public ITypeRoot getTypeRoot ( ) { return this . typeRoot ; } public List imports ( ) { return this . imports ; } public int firstLeadingCommentIndex ( ASTNode node ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } if ( this . commentMapper == null || node . getAST ( ) != getAST ( ) ) { return - 1 ; } return this . commentMapper . firstLeadingCommentIndex ( node ) ; } public int lastTrailingCommentIndex ( ASTNode node ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } if ( this . commentMapper == null || node . getAST ( ) != getAST ( ) ) { return - 1 ; } return this . commentMapper . lastTrailingCommentIndex ( node ) ; } void initCommentMapper ( Scanner scanner ) { this . commentMapper = new DefaultCommentMapper ( this . optionalCommentTable ) ; this . commentMapper . initialize ( this , scanner ) ; } final List internalGetChildListProperty ( ChildListPropertyDescriptor property ) { if ( property == IMPORTS_PROPERTY ) { return imports ( ) ; } if ( property == TYPES_PROPERTY ) { return types ( ) ; } return super . internalGetChildListProperty ( property ) ; } final ASTNode internalGetSetChildProperty ( ChildPropertyDescriptor property , boolean get , ASTNode child ) { if ( property == PACKAGE_PROPERTY ) { if ( get ) { return getPackage ( ) ; } else { setPackage ( ( PackageDeclaration ) child ) ; return null ; } } return super . internalGetSetChildProperty ( property , get , child ) ; } final List internalStructuralPropertiesForType ( int apiLevel ) { return propertyDescriptors ( apiLevel ) ; } public int lineNumber ( int position ) { int lineNumber = getLineNumber ( position ) ; return lineNumber < 1 ? 1 : lineNumber ; } public int getLineNumber ( int position ) { if ( this . lineEndTable == null ) return - 2 ; int length ; if ( ( length = this . lineEndTable . length ) == 0 ) { if ( position >= getStartPosition ( ) + getLength ( ) ) { return - 1 ; } return 1 ; } int low = 0 ; if ( position < 0 ) { return - 1 ; } if ( position <= this . lineEndTable [ low ] ) { return 1 ; } int hi = length - 1 ; if ( position > this . lineEndTable [ hi ] ) { if ( position >= getStartPosition ( ) + getLength ( ) ) { return - 1 ; } else { return length + 1 ; } } while ( true ) { if ( low + 1 == hi ) { return low + 2 ; } int mid = low + ( hi - low ) / 2 ; if ( position <= this . lineEndTable [ mid ] ) { hi = mid ; } else { low = mid ; } } } int memSize ( ) { int size = BASE_NODE_SIZE + 8 * 4 ; if ( this . lineEndTable != null ) { size += HEADERS + 4 * this . lineEndTable . length ; } if ( this . optionalCommentTable != null ) { size += HEADERS + 4 * this . optionalCommentTable . length ; } return size ; } public void recordModifications ( ) { getAST ( ) . recordModifications ( this ) ; } public TextEdit rewrite ( IDocument document , Map options ) { return getAST ( ) . rewrite ( document , options ) ; } void setCommentTable ( Comment [ ] commentTable ) { if ( commentTable == null ) { this . optionalCommentList = null ; this . optionalCommentTable = null ; } else { int nextAvailablePosition = 0 ; for ( int i = 0 ; i < commentTable . length ; i ++ ) { Comment comment = commentTable [ i ] ; if ( comment == null ) { throw new IllegalArgumentException ( ) ; } int start = comment . getStartPosition ( ) ; int length = comment . getLength ( ) ; if ( start < 0 || length < 0 || start < nextAvailablePosition ) { throw new IllegalArgumentException ( ) ; } nextAvailablePosition = comment . getStartPosition ( ) + comment . getLength ( ) ; } this . optionalCommentTable = commentTable ; List commentList = Arrays . asList ( commentTable ) ; this . optionalCommentList = Collections . unmodifiableList ( commentList ) ; } } void setTypeRoot ( ITypeRoot typeRoot ) { this . typeRoot = typeRoot ; } void setLineEndTable ( int [ ] lineEndTable ) { if ( lineEndTable == null ) { throw new NullPointerException ( ) ; } checkModifiable ( ) ; this . lineEndTable = lineEndTable ; } public void setPackage ( PackageDeclaration pkgDecl ) { ASTNode oldChild = this . optionalPackageDeclaration ; preReplaceChild ( oldChild , pkgDecl , PACKAGE_PROPERTY ) ; this . optionalPackageDeclaration = pkgDecl ; postReplaceChild ( oldChild , pkgDecl , PACKAGE_PROPERTY ) ; } void setProblems ( IProblem [ ] problems ) { if ( problems == null ) { throw new IllegalArgumentException ( ) ; } this . problems = problems ; } void setStatementsRecoveryData ( Object data ) { this . statementsRecoveryData = data ; } final boolean subtreeMatch0 ( ASTMatcher matcher , Object other ) { return matcher . match ( this , other ) ; } int treeSize ( ) { int size = memSize ( ) ; if ( this . optionalPackageDeclaration != null ) { size += getPackage ( ) . treeSize ( ) ; } size += this . imports . listSize ( ) ; size += this . types . listSize ( ) ; if ( this . optionalCommentList != null ) { for ( int i = 0 ; i < this . optionalCommentList . size ( ) ; i ++ ) { Comment comment = ( Comment ) this . optionalCommentList . get ( i ) ; if ( comment != null && comment . getParent ( ) == null ) { size += comment . treeSize ( ) ; } } } return size ; } public List types ( ) { return this . types ; } } </s>