<s> package org . eclipse . jdt . internal . core ; import java . io . IOException ; import java . util . * ; import org . codehaus . jdt . groovy . integration . LanguageSupportFactory ; import org . eclipse . core . resources . * ; import org . eclipse . core . runtime . * ; import org . eclipse . jdt . core . * ; import org . eclipse . jdt . core . compiler . * ; import org . eclipse . jdt . core . dom . AST ; import org . eclipse . jdt . internal . compiler . IProblemFactory ; import org . eclipse . jdt . internal . compiler . SourceElementParser ; import org . eclipse . jdt . internal . compiler . ast . CompilationUnitDeclaration ; import org . eclipse . jdt . internal . compiler . impl . CompilerOptions ; import org . eclipse . jdt . internal . compiler . problem . AbortCompilationUnit ; import org . eclipse . jdt . internal . compiler . problem . DefaultProblemFactory ; import org . eclipse . jdt . internal . compiler . util . SuffixConstants ; import org . eclipse . jdt . internal . core . util . MementoTokenizer ; import org . eclipse . jdt . internal . core . util . Messages ; import org . eclipse . jdt . internal . core . util . Util ; import org . eclipse . jface . text . BadLocationException ; import org . eclipse . jface . text . IDocument ; import org . eclipse . text . edits . MalformedTreeException ; import org . eclipse . text . edits . TextEdit ; import org . eclipse . text . edits . UndoEdit ; public class CompilationUnit extends Openable implements ICompilationUnit , org . eclipse . jdt . internal . compiler . env . ICompilationUnit , SuffixConstants { static final int JLS2_INTERNAL = AST . JLS2 ; private static final IImportDeclaration [ ] NO_IMPORTS = new IImportDeclaration [ 0 ] ; protected String name ; public WorkingCopyOwner owner ; public CompilationUnit ( PackageFragment parent , String name , WorkingCopyOwner owner ) { super ( parent ) ; this . name = name ; this . owner = owner ; } public UndoEdit applyTextEdit ( TextEdit edit , IProgressMonitor monitor ) throws JavaModelException { IBuffer buffer = getBuffer ( ) ; if ( buffer instanceof IBuffer . ITextEditCapability ) { return ( ( IBuffer . ITextEditCapability ) buffer ) . applyTextEdit ( edit , monitor ) ; } else if ( buffer != null ) { IDocument document = buffer instanceof IDocument ? ( IDocument ) buffer : new DocumentAdapter ( buffer ) ; try { UndoEdit undoEdit = edit . apply ( document ) ; return undoEdit ; } catch ( MalformedTreeException e ) { throw new JavaModelException ( e , IJavaModelStatusConstants . BAD_TEXT_EDIT_LOCATION ) ; } catch ( BadLocationException e ) { throw new JavaModelException ( e , IJavaModelStatusConstants . BAD_TEXT_EDIT_LOCATION ) ; } } return null ; } public void becomeWorkingCopy ( IProblemRequestor problemRequestor , IProgressMonitor monitor ) throws JavaModelException { JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; JavaModelManager . PerWorkingCopyInfo perWorkingCopyInfo = manager . getPerWorkingCopyInfo ( this , false , true , null ) ; if ( perWorkingCopyInfo == null ) { close ( ) ; BecomeWorkingCopyOperation operation = new BecomeWorkingCopyOperation ( this , problemRequestor ) ; operation . runOperation ( monitor ) ; } } public void becomeWorkingCopy ( IProgressMonitor monitor ) throws JavaModelException { IProblemRequestor requestor = this . owner == null ? null : this . owner . getProblemRequestor ( this ) ; becomeWorkingCopy ( requestor , monitor ) ; } protected boolean buildStructure ( OpenableElementInfo info , final IProgressMonitor pm , Map newElements , IResource underlyingResource ) throws JavaModelException { CompilationUnitElementInfo unitInfo = ( CompilationUnitElementInfo ) info ; IBuffer buffer = getBufferManager ( ) . getBuffer ( CompilationUnit . this ) ; if ( buffer == null ) { openBuffer ( pm , unitInfo ) ; } CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor ( this , unitInfo , newElements ) ; JavaModelManager . PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo ( ) ; IJavaProject project = getJavaProject ( ) ; boolean createAST ; boolean resolveBindings ; int reconcileFlags ; HashMap problems ; if ( info instanceof ASTHolderCUInfo ) { ASTHolderCUInfo astHolder = ( ASTHolderCUInfo ) info ; createAST = astHolder . astLevel != NO_AST ; resolveBindings = astHolder . resolveBindings ; reconcileFlags = astHolder . reconcileFlags ; problems = astHolder . problems ; } else { createAST = false ; resolveBindings = false ; reconcileFlags = 0 ; problems = null ; } boolean computeProblems = perWorkingCopyInfo != null && perWorkingCopyInfo . isActive ( ) && project != null && JavaProject . hasJavaNature ( project . getProject ( ) ) ; IProblemFactory problemFactory = new DefaultProblemFactory ( ) ; Map options = project == null ? JavaCore . getOptions ( ) : project . getOptions ( true ) ; if ( ! computeProblems ) { options . put ( JavaCore . COMPILER_TASK_TAGS , "" ) ; } CompilerOptions compilerOptions = new CompilerOptions ( options ) ; compilerOptions . ignoreMethodBodies = ( reconcileFlags & ICompilationUnit . IGNORE_METHOD_BODIES ) != 0 ; SourceElementParser parser = new SourceElementParser ( requestor , problemFactory , compilerOptions , true , ! createAST ) ; parser . reportOnlyOneSyntaxError = ! computeProblems ; parser . setMethodsFullRecovery ( true ) ; parser . setStatementsRecovery ( ( reconcileFlags & ICompilationUnit . ENABLE_STATEMENTS_RECOVERY ) != 0 ) ; if ( ! computeProblems && ! resolveBindings && ! createAST ) parser . javadocParser . checkDocComment = false ; requestor . parser = parser ; if ( underlyingResource == null ) { underlyingResource = getResource ( ) ; } if ( underlyingResource != null ) unitInfo . timestamp = ( ( IFile ) underlyingResource ) . getModificationStamp ( ) ; CompilationUnitDeclaration compilationUnitDeclaration = null ; CompilationUnit source = cloneCachingContents ( ) ; try { if ( computeProblems ) { if ( problems == null ) { problems = new HashMap ( ) ; compilationUnitDeclaration = CompilationUnitProblemFinder . process ( source , parser , this . owner , problems , createAST , reconcileFlags , pm ) ; try { perWorkingCopyInfo . beginReporting ( ) ; for ( Iterator iteraror = problems . values ( ) . iterator ( ) ; iteraror . hasNext ( ) ; ) { CategorizedProblem [ ] categorizedProblems = ( CategorizedProblem [ ] ) iteraror . next ( ) ; if ( categorizedProblems == null ) continue ; for ( int i = 0 , length = categorizedProblems . length ; i < length ; i ++ ) { perWorkingCopyInfo . acceptProblem ( categorizedProblems [ i ] ) ; } } } finally { perWorkingCopyInfo . endReporting ( ) ; } } else { compilationUnitDeclaration = CompilationUnitProblemFinder . process ( source , parser , this . owner , problems , createAST , reconcileFlags , pm ) ; } } else { compilationUnitDeclaration = parser . parseCompilationUnit ( source , true , pm ) ; } if ( createAST ) { int astLevel = ( ( ASTHolderCUInfo ) info ) . astLevel ; org . eclipse . jdt . core . dom . CompilationUnit cu = AST . convertCompilationUnit ( astLevel , compilationUnitDeclaration , options , computeProblems , source , reconcileFlags , pm ) ; ( ( ASTHolderCUInfo ) info ) . ast = cu ; } } finally { if ( compilationUnitDeclaration != null ) { compilationUnitDeclaration . cleanUp ( ) ; } } return unitInfo . isStructureKnown ( ) ; } public CompilationUnit cloneCachingContents ( ) { return new CompilationUnit ( ( PackageFragment ) this . parent , this . name , this . owner ) { private char [ ] cachedContents ; public char [ ] getContents ( ) { if ( this . cachedContents == null ) this . cachedContents = CompilationUnit . this . getContents ( ) ; return this . cachedContents ; } public CompilationUnit originalFromClone ( ) { return CompilationUnit . this ; } } ; } public boolean canBeRemovedFromCache ( ) { if ( getPerWorkingCopyInfo ( ) != null ) return false ; return super . canBeRemovedFromCache ( ) ; } public boolean canBufferBeRemovedFromCache ( IBuffer buffer ) { if ( getPerWorkingCopyInfo ( ) != null ) return false ; return super . canBufferBeRemovedFromCache ( buffer ) ; } public void close ( ) throws JavaModelException { if ( getPerWorkingCopyInfo ( ) != null ) return ; super . close ( ) ; } protected void closing ( Object info ) { if ( getPerWorkingCopyInfo ( ) == null ) { super . closing ( info ) ; } } public void codeComplete ( int offset , ICompletionRequestor requestor ) throws JavaModelException { codeComplete ( offset , requestor , DefaultWorkingCopyOwner . PRIMARY ) ; } public void codeComplete ( int offset , ICompletionRequestor requestor , WorkingCopyOwner workingCopyOwner ) throws JavaModelException { if ( requestor == null ) { throw new IllegalArgumentException ( "" ) ; } codeComplete ( offset , new org . eclipse . jdt . internal . codeassist . CompletionRequestorWrapper ( requestor ) , workingCopyOwner ) ; } public void codeComplete ( int offset , final ICodeCompletionRequestor requestor ) throws JavaModelException { if ( requestor == null ) { codeComplete ( offset , ( ICompletionRequestor ) null ) ; return ; } codeComplete ( offset , new ICompletionRequestor ( ) { public void acceptAnonymousType ( char [ ] superTypePackageName , char [ ] superTypeName , char [ ] [ ] parameterPackageNames , char [ ] [ ] parameterTypeNames , char [ ] [ ] parameterNames , char [ ] completionName , int modifiers , int completionStart , int completionEnd , int relevance ) { } public void acceptClass ( char [ ] packageName , char [ ] className , char [ ] completionName , int modifiers , int completionStart , int completionEnd , int relevance ) { requestor . acceptClass ( packageName , className , completionName , modifiers , completionStart , completionEnd ) ; } public void acceptError ( IProblem error ) { } public void acceptField ( char [ ] declaringTypePackageName , char [ ] declaringTypeName , char [ ] fieldName , char [ ] typePackageName , char [ ] typeName , char [ ] completionName , int modifiers , int completionStart , int completionEnd , int relevance ) { requestor . acceptField ( declaringTypePackageName , declaringTypeName , fieldName , typePackageName , typeName , completionName , modifiers , completionStart , completionEnd ) ; } public void acceptInterface ( char [ ] packageName , char [ ] interfaceName , char [ ] completionName , int modifiers , int completionStart , int completionEnd , int relevance ) { requestor . acceptInterface ( packageName , interfaceName , completionName , modifiers , completionStart , completionEnd ) ; } public void acceptKeyword ( char [ ] keywordName , int completionStart , int completionEnd , int relevance ) { requestor . acceptKeyword ( keywordName , completionStart , completionEnd ) ; } public void acceptLabel ( char [ ] labelName , int completionStart , int completionEnd , int relevance ) { requestor . acceptLabel ( labelName , completionStart , completionEnd ) ; } public void acceptLocalVariable ( char [ ] localVarName , char [ ] typePackageName , char [ ] typeName , int modifiers , int completionStart , int completionEnd , int relevance ) { } public void acceptMethod ( char [ ] declaringTypePackageName , char [ ] declaringTypeName , char [ ] selector , char [ ] [ ] parameterPackageNames , char [ ] [ ] parameterTypeNames , char [ ] [ ] parameterNames , char [ ] returnTypePackageName , char [ ] returnTypeName , char [ ] completionName , int modifiers , int completionStart , int completionEnd , int relevance ) { requestor . acceptMethod ( declaringTypePackageName , declaringTypeName , selector , parameterPackageNames , parameterTypeNames , returnTypePackageName , returnTypeName , completionName , modifiers , completionStart , completionEnd ) ; } public void acceptMethodDeclaration ( char [ ] declaringTypePackageName , char [ ] declaringTypeName , char [ ] selector , char [ ] [ ] parameterPackageNames , char [ ] [ ] parameterTypeNames , char [ ] [ ] parameterNames , char [ ] returnTypePackageName , char [ ] returnTypeName , char [ ] completionName , int modifiers , int completionStart , int completionEnd , int relevance ) { } public void acceptModifier ( char [ ] modifierName , int completionStart , int completionEnd , int relevance ) { requestor . acceptModifier ( modifierName , completionStart , completionEnd ) ; } public void acceptPackage ( char [ ] packageName , char [ ] completionName , int completionStart , int completionEnd , int relevance ) { requestor . acceptPackage ( packageName , completionName , completionStart , completionEnd ) ; } public void acceptType ( char [ ] packageName , char [ ] typeName , char [ ] completionName , int completionStart , int completionEnd , int relevance ) { requestor . acceptType ( packageName , typeName , completionName , completionStart , completionEnd ) ; } public void acceptVariableName ( char [ ] typePackageName , char [ ] typeName , char [ ] varName , char [ ] completionName , int completionStart , int completionEnd , int relevance ) { } } ) ; } public void codeComplete ( int offset , CompletionRequestor requestor ) throws JavaModelException { codeComplete ( offset , requestor , DefaultWorkingCopyOwner . PRIMARY ) ; } public void codeComplete ( int offset , CompletionRequestor requestor , IProgressMonitor monitor ) throws JavaModelException { codeComplete ( offset , requestor , DefaultWorkingCopyOwner . PRIMARY , monitor ) ; } public void codeComplete ( int offset , CompletionRequestor requestor , WorkingCopyOwner workingCopyOwner ) throws JavaModelException { codeComplete ( offset , requestor , workingCopyOwner , null ) ; } public void codeComplete ( int offset , CompletionRequestor requestor , WorkingCopyOwner workingCopyOwner , IProgressMonitor monitor ) throws JavaModelException { codeComplete ( this , isWorkingCopy ( ) ? ( org . eclipse . jdt . internal . compiler . env . ICompilationUnit ) getOriginalElement ( ) : this , offset , requestor , workingCopyOwner , this , monitor ) ; } public IJavaElement [ ] codeSelect ( int offset , int length ) throws JavaModelException { return codeSelect ( offset , length , DefaultWorkingCopyOwner . PRIMARY ) ; } public IJavaElement [ ] codeSelect ( int offset , int length , WorkingCopyOwner workingCopyOwner ) throws JavaModelException { return super . codeSelect ( this , offset , length , workingCopyOwner ) ; } public void commit ( boolean force , IProgressMonitor monitor ) throws JavaModelException { commitWorkingCopy ( force , monitor ) ; } public void commitWorkingCopy ( boolean force , IProgressMonitor monitor ) throws JavaModelException { CommitWorkingCopyOperation op = new CommitWorkingCopyOperation ( this , force ) ; op . runOperation ( monitor ) ; } public void copy ( IJavaElement container , IJavaElement sibling , String rename , boolean force , IProgressMonitor monitor ) throws JavaModelException { if ( container == null ) { throw new IllegalArgumentException ( Messages . operation_nullContainer ) ; } IJavaElement [ ] elements = new IJavaElement [ ] { this } ; IJavaElement [ ] containers = new IJavaElement [ ] { container } ; String [ ] renamings = null ; if ( rename != null ) { renamings = new String [ ] { rename } ; } getJavaModel ( ) . copy ( elements , containers , null , renamings , force , monitor ) ; } protected Object createElementInfo ( ) { return new CompilationUnitElementInfo ( ) ; } public IImportDeclaration createImport ( String importName , IJavaElement sibling , IProgressMonitor monitor ) throws JavaModelException { return createImport ( importName , sibling , Flags . AccDefault , monitor ) ; } public IImportDeclaration createImport ( String importName , IJavaElement sibling , int flags , IProgressMonitor monitor ) throws JavaModelException { CreateImportOperation op = new CreateImportOperation ( importName , this , flags ) ; if ( sibling != null ) { op . createBefore ( sibling ) ; } op . runOperation ( monitor ) ; return getImport ( importName ) ; } public IPackageDeclaration createPackageDeclaration ( String pkg , IProgressMonitor monitor ) throws JavaModelException { CreatePackageDeclarationOperation op = new CreatePackageDeclarationOperation ( pkg , this ) ; op . runOperation ( monitor ) ; return getPackageDeclaration ( pkg ) ; } public IType createType ( String content , IJavaElement sibling , boolean force , IProgressMonitor monitor ) throws JavaModelException { if ( ! exists ( ) ) { IPackageFragment pkg = ( IPackageFragment ) getParent ( ) ; String source = "" ; if ( ! pkg . isDefaultPackage ( ) ) { String lineSeparator = Util . getLineSeparator ( null , getJavaProject ( ) ) ; source = "package " + pkg . getElementName ( ) + ";" + lineSeparator + lineSeparator ; } CreateCompilationUnitOperation op = new CreateCompilationUnitOperation ( pkg , this . name , source , force ) ; op . runOperation ( monitor ) ; } CreateTypeOperation op = new CreateTypeOperation ( this , content , force ) ; if ( sibling != null ) { op . createBefore ( sibling ) ; } op . runOperation ( monitor ) ; return ( IType ) op . getResultElements ( ) [ 0 ] ; } public void delete ( boolean force , IProgressMonitor monitor ) throws JavaModelException { IJavaElement [ ] elements = new IJavaElement [ ] { this } ; getJavaModel ( ) . delete ( elements , force , monitor ) ; } public void destroy ( ) { try { discardWorkingCopy ( ) ; } catch ( JavaModelException e ) { if ( JavaModelManager . VERBOSE ) e . printStackTrace ( ) ; } } public void discardWorkingCopy ( ) throws JavaModelException { DiscardWorkingCopyOperation op = new DiscardWorkingCopyOperation ( this ) ; op . runOperation ( null ) ; } public boolean equals ( Object obj ) { if ( ! ( obj instanceof CompilationUnit ) ) return false ; CompilationUnit other = ( CompilationUnit ) obj ; return this . owner . equals ( other . owner ) && super . equals ( obj ) ; } public IJavaElement [ ] findElements ( IJavaElement element ) { ArrayList children = new ArrayList ( ) ; while ( element != null && element . getElementType ( ) != IJavaElement . COMPILATION_UNIT ) { children . add ( element ) ; element = element . getParent ( ) ; } if ( element == null ) return null ; IJavaElement currentElement = this ; for ( int i = children . size ( ) - 1 ; i >= 0 ; i -- ) { SourceRefElement child = ( SourceRefElement ) children . get ( i ) ; switch ( child . getElementType ( ) ) { case IJavaElement . PACKAGE_DECLARATION : currentElement = ( ( ICompilationUnit ) currentElement ) . getPackageDeclaration ( child . getElementName ( ) ) ; break ; case IJavaElement . IMPORT_CONTAINER : currentElement = ( ( ICompilationUnit ) currentElement ) . getImportContainer ( ) ; break ; case IJavaElement . IMPORT_DECLARATION : currentElement = ( ( IImportContainer ) currentElement ) . getImport ( child . getElementName ( ) ) ; break ; case IJavaElement . TYPE : switch ( currentElement . getElementType ( ) ) { case IJavaElement . COMPILATION_UNIT : currentElement = ( ( ICompilationUnit ) currentElement ) . getType ( child . getElementName ( ) ) ; break ; case IJavaElement . TYPE : currentElement = ( ( IType ) currentElement ) . getType ( child . getElementName ( ) ) ; break ; case IJavaElement . FIELD : case IJavaElement . INITIALIZER : case IJavaElement . METHOD : currentElement = ( ( IMember ) currentElement ) . getType ( child . getElementName ( ) , child . occurrenceCount ) ; break ; } break ; case IJavaElement . INITIALIZER : currentElement = ( ( IType ) currentElement ) . getInitializer ( child . occurrenceCount ) ; break ; case IJavaElement . FIELD : currentElement = ( ( IType ) currentElement ) . getField ( child . getElementName ( ) ) ; break ; case IJavaElement . METHOD : currentElement = ( ( IType ) currentElement ) . getMethod ( child . getElementName ( ) , ( ( IMethod ) child ) . getParameterTypes ( ) ) ; break ; } } if ( currentElement != null && currentElement . exists ( ) ) { return new IJavaElement [ ] { currentElement } ; } else { return null ; } } public IType findPrimaryType ( ) { String typeName = Util . getNameWithoutJavaLikeExtension ( getElementName ( ) ) ; IType primaryType = getType ( typeName ) ; if ( primaryType . exists ( ) ) { return primaryType ; } return null ; } public IJavaElement findSharedWorkingCopy ( IBufferFactory factory ) { if ( factory == null ) factory = getBufferManager ( ) . getDefaultBufferFactory ( ) ; return findWorkingCopy ( BufferFactoryWrapper . create ( factory ) ) ; } public ICompilationUnit findWorkingCopy ( WorkingCopyOwner workingCopyOwner ) { CompilationUnit cu = LanguageSupportFactory . newCompilationUnit ( ( PackageFragment ) this . parent , getElementName ( ) , workingCopyOwner ) ; if ( workingCopyOwner == DefaultWorkingCopyOwner . PRIMARY ) { return cu ; } else { JavaModelManager . PerWorkingCopyInfo perWorkingCopyInfo = cu . getPerWorkingCopyInfo ( ) ; if ( perWorkingCopyInfo != null ) { return perWorkingCopyInfo . getWorkingCopy ( ) ; } else { return null ; } } } public IType [ ] getAllTypes ( ) throws JavaModelException { IJavaElement [ ] types = getTypes ( ) ; int i ; ArrayList allTypes = new ArrayList ( types . length ) ; ArrayList typesToTraverse = new ArrayList ( types . length ) ; for ( i = 0 ; i < types . length ; i ++ ) { typesToTraverse . add ( types [ i ] ) ; } while ( ! typesToTraverse . isEmpty ( ) ) { IType type = ( IType ) typesToTraverse . get ( 0 ) ; typesToTraverse . remove ( type ) ; allTypes . add ( type ) ; types = type . getTypes ( ) ; for ( i = 0 ; i < types . length ; i ++ ) { typesToTraverse . add ( types [ i ] ) ; } } IType [ ] arrayOfAllTypes = new IType [ allTypes . size ( ) ] ; allTypes . toArray ( arrayOfAllTypes ) ; return arrayOfAllTypes ; } public ICompilationUnit getCompilationUnit ( ) { return this ; } public char [ ] getContents ( ) { IBuffer buffer = getBufferManager ( ) . getBuffer ( this ) ; if ( buffer == null ) { IFile file = ( IFile ) getResource ( ) ; String encoding ; try { encoding = file . getCharset ( ) ; } catch ( CoreException ce ) { encoding = null ; } try { return Util . getResourceContentsAsCharArray ( file , encoding ) ; } catch ( JavaModelException e ) { if ( JavaModelManager . getJavaModelManager ( ) . abortOnMissingSource . get ( ) == Boolean . TRUE ) { IOException ioException = e . getJavaModelStatus ( ) . getCode ( ) == IJavaModelStatusConstants . IO_EXCEPTION ? ( IOException ) e . getException ( ) : new IOException ( e . getMessage ( ) ) ; throw new AbortCompilationUnit ( null , ioException , encoding ) ; } else { Util . log ( e , Messages . bind ( Messages . file_notFound , file . getFullPath ( ) . toString ( ) ) ) ; } return CharOperation . NO_CHAR ; } } char [ ] contents = buffer . getCharacters ( ) ; if ( contents == null ) { if ( JavaModelManager . getJavaModelManager ( ) . abortOnMissingSource . get ( ) == Boolean . TRUE ) { IOException ioException = new IOException ( Messages . buffer_closed ) ; IFile file = ( IFile ) getResource ( ) ; String encoding ; try { encoding = file . getCharset ( ) ; } catch ( CoreException ce ) { encoding = null ; } throw new AbortCompilationUnit ( null , ioException , encoding ) ; } return CharOperation . NO_CHAR ; } return contents ; } public IResource getCorrespondingResource ( ) throws JavaModelException { PackageFragmentRoot root = getPackageFragmentRoot ( ) ; if ( root == null || root . isArchive ( ) ) { return null ; } else { return getUnderlyingResource ( ) ; } } public IJavaElement getElementAt ( int position ) throws JavaModelException { IJavaElement e = getSourceElementAt ( position ) ; if ( e == this ) { return null ; } else { return e ; } } public String getElementName ( ) { return this . name ; } public int getElementType ( ) { return COMPILATION_UNIT ; } public char [ ] getFileName ( ) { return getPath ( ) . toString ( ) . toCharArray ( ) ; } public IJavaElement getHandleFromMemento ( String token , MementoTokenizer memento , WorkingCopyOwner workingCopyOwner ) { switch ( token . charAt ( 0 ) ) { case JEM_IMPORTDECLARATION : JavaElement container = ( JavaElement ) getImportContainer ( ) ; return container . getHandleFromMemento ( token , memento , workingCopyOwner ) ; case JEM_PACKAGEDECLARATION : if ( ! memento . hasMoreTokens ( ) ) return this ; String pkgName = memento . nextToken ( ) ; JavaElement pkgDecl = ( JavaElement ) getPackageDeclaration ( pkgName ) ; return pkgDecl . getHandleFromMemento ( memento , workingCopyOwner ) ; case JEM_TYPE : if ( ! memento . hasMoreTokens ( ) ) return this ; String typeName = memento . nextToken ( ) ; JavaElement type = ( JavaElement ) getType ( typeName ) ; return type . getHandleFromMemento ( memento , workingCopyOwner ) ; } return null ; } protected char getHandleMementoDelimiter ( ) { return JavaElement . JEM_COMPILATIONUNIT ; } public IImportDeclaration getImport ( String importName ) { return getImportContainer ( ) . getImport ( importName ) ; } public IImportContainer getImportContainer ( ) { return new ImportContainer ( this ) ; } public IImportDeclaration [ ] getImports ( ) throws JavaModelException { IImportContainer container = getImportContainer ( ) ; JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; Object info = manager . getInfo ( container ) ; if ( info == null ) { if ( manager . getInfo ( this ) != null ) return NO_IMPORTS ; else { open ( null ) ; info = manager . getInfo ( container ) ; if ( info == null ) return NO_IMPORTS ; } } IJavaElement [ ] elements = ( ( ImportContainerInfo ) info ) . children ; int length = elements . length ; IImportDeclaration [ ] imports = new IImportDeclaration [ length ] ; System . arraycopy ( elements , 0 , imports , 0 , length ) ; return imports ; } public ITypeRoot getTypeRoot ( ) { return this ; } public char [ ] getMainTypeName ( ) { return Util . getNameWithoutJavaLikeExtension ( getElementName ( ) ) . toCharArray ( ) ; } public IJavaElement getOriginal ( IJavaElement workingCopyElement ) { if ( ! isWorkingCopy ( ) ) return null ; CompilationUnit cu = ( CompilationUnit ) workingCopyElement . getAncestor ( COMPILATION_UNIT ) ; if ( cu == null || ! this . owner . equals ( cu . owner ) ) { return null ; } return workingCopyElement . getPrimaryElement ( ) ; } public IJavaElement getOriginalElement ( ) { if ( ! isWorkingCopy ( ) ) return null ; return getPrimaryElement ( ) ; } public WorkingCopyOwner getOwner ( ) { return isPrimary ( ) || ! isWorkingCopy ( ) ? null : this . owner ; } public IPackageDeclaration getPackageDeclaration ( String pkg ) { return new PackageDeclaration ( this , pkg ) ; } public IPackageDeclaration [ ] getPackageDeclarations ( ) throws JavaModelException { ArrayList list = getChildrenOfType ( PACKAGE_DECLARATION ) ; IPackageDeclaration [ ] array = new IPackageDeclaration [ list . size ( ) ] ; list . toArray ( array ) ; return array ; } public char [ ] [ ] getPackageName ( ) { PackageFragment packageFragment = ( PackageFragment ) getParent ( ) ; if ( packageFragment == null ) return CharOperation . NO_CHAR_CHAR ; return Util . toCharArrays ( packageFragment . names ) ; } public IPath getPath ( ) { PackageFragmentRoot root = getPackageFragmentRoot ( ) ; if ( root == null ) return new Path ( getElementName ( ) ) ; if ( root . isArchive ( ) ) { return root . getPath ( ) ; } else { return getParent ( ) . getPath ( ) . append ( getElementName ( ) ) ; } } public JavaModelManager . PerWorkingCopyInfo getPerWorkingCopyInfo ( ) { return JavaModelManager . getJavaModelManager ( ) . getPerWorkingCopyInfo ( this , false , false , null ) ; } public ICompilationUnit getPrimary ( ) { return ( ICompilationUnit ) getPrimaryElement ( true ) ; } public IJavaElement getPrimaryElement ( boolean checkOwner ) { if ( checkOwner && isPrimary ( ) ) return this ; return LanguageSupportFactory . newCompilationUnit ( ( PackageFragment ) getParent ( ) , getElementName ( ) , DefaultWorkingCopyOwner . PRIMARY ) ; } public IResource resource ( PackageFragmentRoot root ) { if ( root == null ) return null ; return ( ( IContainer ) ( ( Openable ) this . parent ) . resource ( root ) ) . getFile ( new Path ( getElementName ( ) ) ) ; } public String getSource ( ) throws JavaModelException { IBuffer buffer = getBuffer ( ) ; if ( buffer == null ) return "" ; return buffer . getContents ( ) ; } public ISourceRange getSourceRange ( ) throws JavaModelException { return ( ( CompilationUnitElementInfo ) getElementInfo ( ) ) . getSourceRange ( ) ; } public IType getType ( String typeName ) { return new SourceType ( this , typeName ) ; } public IType [ ] getTypes ( ) throws JavaModelException { ArrayList list = getChildrenOfType ( TYPE ) ; IType [ ] array = new IType [ list . size ( ) ] ; list . toArray ( array ) ; return array ; } public IResource getUnderlyingResource ( ) throws JavaModelException { if ( isWorkingCopy ( ) && ! isPrimary ( ) ) return null ; return super . getUnderlyingResource ( ) ; } public IJavaElement getSharedWorkingCopy ( IProgressMonitor pm , IBufferFactory factory , IProblemRequestor problemRequestor ) throws JavaModelException { if ( factory == null ) factory = getBufferManager ( ) . getDefaultBufferFactory ( ) ; return getWorkingCopy ( BufferFactoryWrapper . create ( factory ) , problemRequestor , pm ) ; } public IJavaElement getWorkingCopy ( ) throws JavaModelException { return getWorkingCopy ( null ) ; } public ICompilationUnit getWorkingCopy ( IProgressMonitor monitor ) throws JavaModelException { return getWorkingCopy ( new WorkingCopyOwner ( ) { } , null , monitor ) ; } public ICompilationUnit getWorkingCopy ( WorkingCopyOwner workingCopyOwner , IProgressMonitor monitor ) throws JavaModelException { return getWorkingCopy ( workingCopyOwner , null , monitor ) ; } public IJavaElement getWorkingCopy ( IProgressMonitor monitor , IBufferFactory factory , IProblemRequestor problemRequestor ) throws JavaModelException { return getWorkingCopy ( BufferFactoryWrapper . create ( factory ) , problemRequestor , monitor ) ; } public ICompilationUnit getWorkingCopy ( WorkingCopyOwner workingCopyOwner , IProblemRequestor problemRequestor , IProgressMonitor monitor ) throws JavaModelException { if ( ! isPrimary ( ) ) return this ; JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; CompilationUnit workingCopy = LanguageSupportFactory . newCompilationUnit ( ( PackageFragment ) getParent ( ) , getElementName ( ) , workingCopyOwner ) ; JavaModelManager . PerWorkingCopyInfo perWorkingCopyInfo = manager . getPerWorkingCopyInfo ( workingCopy , false , true , null ) ; if ( perWorkingCopyInfo != null ) { return perWorkingCopyInfo . getWorkingCopy ( ) ; } BecomeWorkingCopyOperation op = new BecomeWorkingCopyOperation ( workingCopy , problemRequestor ) ; op . runOperation ( monitor ) ; return workingCopy ; } protected boolean hasBuffer ( ) { return true ; } public boolean hasResourceChanged ( ) { if ( ! isWorkingCopy ( ) ) return false ; Object info = JavaModelManager . getJavaModelManager ( ) . getInfo ( this ) ; if ( info == null ) return false ; IResource resource = getResource ( ) ; if ( resource == null ) return false ; return ( ( CompilationUnitElementInfo ) info ) . timestamp != resource . getModificationStamp ( ) ; } public boolean isBasedOn ( IResource resource ) { if ( ! isWorkingCopy ( ) ) return false ; if ( ! getResource ( ) . equals ( resource ) ) return false ; return ! hasResourceChanged ( ) ; } public boolean isConsistent ( ) { return ! JavaModelManager . getJavaModelManager ( ) . getElementsOutOfSynchWithBuffers ( ) . contains ( this ) ; } public boolean isPrimary ( ) { return this . owner == DefaultWorkingCopyOwner . PRIMARY ; } protected boolean isSourceElement ( ) { return true ; } protected IStatus validateCompilationUnit ( IResource resource ) { IPackageFragmentRoot root = getPackageFragmentRoot ( ) ; try { if ( root . getKind ( ) != IPackageFragmentRoot . K_SOURCE ) return new JavaModelStatus ( IJavaModelStatusConstants . INVALID_ELEMENT_TYPES , root ) ; } catch ( JavaModelException e ) { return e . getJavaModelStatus ( ) ; } if ( resource != null ) { char [ ] [ ] inclusionPatterns = ( ( PackageFragmentRoot ) root ) . fullInclusionPatternChars ( ) ; char [ ] [ ] exclusionPatterns = ( ( PackageFragmentRoot ) root ) . fullExclusionPatternChars ( ) ; if ( Util . isExcluded ( resource , inclusionPatterns , exclusionPatterns ) ) return new JavaModelStatus ( IJavaModelStatusConstants . ELEMENT_NOT_ON_CLASSPATH , this ) ; if ( ! resource . isAccessible ( ) ) return new JavaModelStatus ( IJavaModelStatusConstants . ELEMENT_DOES_NOT_EXIST , this ) ; } IJavaProject project = getJavaProject ( ) ; return JavaConventions . validateCompilationUnitName ( getElementName ( ) , project . getOption ( JavaCore . COMPILER_SOURCE , true ) , project . getOption ( JavaCore . COMPILER_COMPLIANCE , true ) ) ; } public boolean isWorkingCopy ( ) { return ! isPrimary ( ) || getPerWorkingCopyInfo ( ) != null ; } public void makeConsistent ( IProgressMonitor monitor ) throws JavaModelException { makeConsistent ( NO_AST , false , 0 , null , monitor ) ; } public org . eclipse . jdt . core . dom . CompilationUnit makeConsistent ( int astLevel , boolean resolveBindings , int reconcileFlags , HashMap problems , IProgressMonitor monitor ) throws JavaModelException { if ( isConsistent ( ) ) return null ; try { JavaModelManager . getJavaModelManager ( ) . abortOnMissingSource . set ( Boolean . TRUE ) ; if ( astLevel != NO_AST || problems != null ) { ASTHolderCUInfo info = new ASTHolderCUInfo ( ) ; info . astLevel = astLevel ; info . resolveBindings = resolveBindings ; info . reconcileFlags = reconcileFlags ; info . problems = problems ; openWhenClosed ( info , monitor ) ; org . eclipse . jdt . core . dom . CompilationUnit result = info . ast ; info . ast = null ; return result ; } else { openWhenClosed ( createElementInfo ( ) , monitor ) ; return null ; } } finally { JavaModelManager . getJavaModelManager ( ) . abortOnMissingSource . set ( null ) ; } } public void move ( IJavaElement container , IJavaElement sibling , String rename , boolean force , IProgressMonitor monitor ) throws JavaModelException { if ( container == null ) { throw new IllegalArgumentException ( Messages . operation_nullContainer ) ; } IJavaElement [ ] elements = new IJavaElement [ ] { this } ; IJavaElement [ ] containers = new IJavaElement [ ] { container } ; String [ ] renamings = null ; if ( rename != null ) { renamings = new String [ ] { rename } ; } getJavaModel ( ) . move ( elements , containers , null , renamings , force , monitor ) ; } protected IBuffer openBuffer ( IProgressMonitor pm , Object info ) throws JavaModelException { BufferManager bufManager = getBufferManager ( ) ; boolean isWorkingCopy = isWorkingCopy ( ) ; IBuffer buffer = isWorkingCopy ? this . owner . createBuffer ( this ) : BufferManager . createBuffer ( this ) ; if ( buffer == null ) return null ; ICompilationUnit original = null ; boolean mustSetToOriginalContent = false ; if ( isWorkingCopy ) { mustSetToOriginalContent = ! isPrimary ( ) && ( original = LanguageSupportFactory . newCompilationUnit ( ( PackageFragment ) getParent ( ) , getElementName ( ) , DefaultWorkingCopyOwner . PRIMARY ) ) . isOpen ( ) ; } synchronized ( bufManager ) { IBuffer existingBuffer = bufManager . getBuffer ( this ) ; if ( existingBuffer != null ) return existingBuffer ; if ( buffer . getCharacters ( ) == null ) { if ( isWorkingCopy ) { if ( mustSetToOriginalContent ) { buffer . setContents ( original . getSource ( ) ) ; } else { IFile file = ( IFile ) getResource ( ) ; if ( file == null || ! file . exists ( ) ) { buffer . setContents ( CharOperation . NO_CHAR ) ; } else { buffer . setContents ( Util . getResourceContentsAsCharArray ( file ) ) ; } } } else { IFile file = ( IFile ) getResource ( ) ; if ( file == null || ! file . exists ( ) ) throw newNotPresentException ( ) ; buffer . setContents ( Util . getResourceContentsAsCharArray ( file ) ) ; } } bufManager . addBuffer ( buffer ) ; buffer . addBufferChangedListener ( this ) ; } return buffer ; } protected void openAncestors ( HashMap newElements , IProgressMonitor monitor ) throws JavaModelException { if ( ! isWorkingCopy ( ) ) { super . openAncestors ( newElements , monitor ) ; } } public CompilationUnit originalFromClone ( ) { return this ; } public IMarker [ ] reconcile ( ) throws JavaModelException { reconcile ( NO_AST , false , false , null , null ) ; return null ; } public void reconcile ( boolean forceProblemDetection , IProgressMonitor monitor ) throws JavaModelException { reconcile ( NO_AST , forceProblemDetection ? ICompilationUnit . FORCE_PROBLEM_DETECTION : 0 , null , monitor ) ; } public org . eclipse . jdt . core . dom . CompilationUnit reconcile ( int astLevel , boolean forceProblemDetection , WorkingCopyOwner workingCopyOwner , IProgressMonitor monitor ) throws JavaModelException { return reconcile ( astLevel , forceProblemDetection ? ICompilationUnit . FORCE_PROBLEM_DETECTION : 0 , workingCopyOwner , monitor ) ; } public org . eclipse . jdt . core . dom . CompilationUnit reconcile ( int astLevel , boolean forceProblemDetection , boolean enableStatementsRecovery , WorkingCopyOwner workingCopyOwner , IProgressMonitor monitor ) throws JavaModelException { int flags = 0 ; if ( forceProblemDetection ) flags |= ICompilationUnit . FORCE_PROBLEM_DETECTION ; if ( enableStatementsRecovery ) flags |= ICompilationUnit . ENABLE_STATEMENTS_RECOVERY ; return reconcile ( astLevel , flags , workingCopyOwner , monitor ) ; } public org . eclipse . jdt . core . dom . CompilationUnit reconcile ( int astLevel , int reconcileFlags , WorkingCopyOwner workingCopyOwner , IProgressMonitor monitor ) throws JavaModelException { if ( ! isWorkingCopy ( ) ) return null ; if ( workingCopyOwner == null ) workingCopyOwner = DefaultWorkingCopyOwner . PRIMARY ; PerformanceStats stats = null ; if ( ReconcileWorkingCopyOperation . PERF ) { stats = PerformanceStats . getStats ( JavaModelManager . RECONCILE_PERF , this ) ; stats . startRun ( new String ( getFileName ( ) ) ) ; } ReconcileWorkingCopyOperation op = new ReconcileWorkingCopyOperation ( this , astLevel , reconcileFlags , workingCopyOwner ) ; JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; try { manager . cacheZipFiles ( this ) ; op . runOperation ( monitor ) ; } finally { manager . flushZipFiles ( this ) ; } if ( ReconcileWorkingCopyOperation . PERF ) { stats . endRun ( ) ; } return op . ast ; } public void rename ( String newName , boolean force , IProgressMonitor monitor ) throws JavaModelException { if ( newName == null ) { throw new IllegalArgumentException ( Messages . operation_nullName ) ; } IJavaElement [ ] elements = new IJavaElement [ ] { this } ; IJavaElement [ ] dests = new IJavaElement [ ] { getParent ( ) } ; String [ ] renamings = new String [ ] { newName } ; getJavaModel ( ) . rename ( elements , dests , renamings , force , monitor ) ; } public void restore ( ) throws JavaModelException { if ( ! isWorkingCopy ( ) ) return ; CompilationUnit original = ( CompilationUnit ) getOriginalElement ( ) ; IBuffer buffer = getBuffer ( ) ; if ( buffer == null ) return ; buffer . setContents ( original . getContents ( ) ) ; updateTimeStamp ( original ) ; makeConsistent ( null ) ; } public void save ( IProgressMonitor pm , boolean force ) throws JavaModelException { if ( isWorkingCopy ( ) ) { reconcile ( ) ; } else { super . save ( pm , force ) ; } } protected void toStringInfo ( int tab , StringBuffer buffer , Object info , boolean showResolvedInfo ) { if ( ! isPrimary ( ) ) { buffer . append ( tabString ( tab ) ) ; buffer . append ( "" ) ; toStringName ( buffer ) ; } else { if ( isWorkingCopy ( ) ) { buffer . append ( tabString ( tab ) ) ; buffer . append ( "" ) ; toStringName ( buffer ) ; if ( info == null ) { buffer . append ( " (not open)" ) ; } } else { super . toStringInfo ( tab , buffer , info , showResolvedInfo ) ; } } } protected void updateTimeStamp ( CompilationUnit original ) throws JavaModelException { long timeStamp = ( ( IFile ) original . getResource ( ) ) . getModificationStamp ( ) ; if ( timeStamp == IResource . NULL_STAMP ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . INVALID_RESOURCE ) ) ; } ( ( CompilationUnitElementInfo ) getElementInfo ( ) ) . timestamp = timeStamp ; } protected IStatus validateExistence ( IResource underlyingResource ) { if ( ! isWorkingCopy ( ) ) { IStatus status = validateCompilationUnit ( underlyingResource ) ; if ( ! status . isOK ( ) ) return status ; } if ( ! isPrimary ( ) && getPerWorkingCopyInfo ( ) == null ) { return newDoesNotExistStatus ( ) ; } return JavaModelStatus . VERIFIED_OK ; } } </s>