<s> package org . eclipse . jdt . core . dom ; import java . io . PrintWriter ; import java . io . StringWriter ; import java . util . ArrayList ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; import org . eclipse . core . runtime . IProgressMonitor ; import org . eclipse . jdt . core . IClassFile ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . ITypeRoot ; import org . eclipse . jdt . core . IJavaProject ; import org . eclipse . jdt . core . JavaCore ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . core . WorkingCopyOwner ; import org . eclipse . jdt . core . compiler . CategorizedProblem ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . internal . compiler . ast . CompilationUnitDeclaration ; import org . eclipse . jdt . internal . compiler . ast . ConstructorDeclaration ; import org . eclipse . jdt . internal . compiler . batch . Main ; import org . eclipse . jdt . internal . compiler . env . IBinaryType ; import org . eclipse . jdt . internal . compiler . parser . RecoveryScanner ; import org . eclipse . jdt . internal . compiler . parser . RecoveryScannerData ; import org . eclipse . jdt . internal . compiler . parser . Scanner ; import org . eclipse . jdt . internal . compiler . util . SuffixConstants ; import org . eclipse . jdt . internal . core . * ; import org . eclipse . jdt . internal . core . util . CodeSnippetParsingUtil ; import org . eclipse . jdt . internal . core . util . RecordedParsingInformation ; import org . eclipse . jdt . internal . core . util . Util ; public class ASTParser { public static final int K_EXPRESSION = 0x01 ; public static final int K_STATEMENTS = 0x02 ; public static final int K_CLASS_BODY_DECLARATIONS = 0x04 ; public static final int K_COMPILATION_UNIT = 0x08 ; public static ASTParser newParser ( int level ) { return new ASTParser ( level ) ; } private final int apiLevel ; private int astKind ; private Map compilerOptions ; private int focalPointPosition ; private char [ ] rawSource = null ; private ITypeRoot typeRoot = null ; private int sourceOffset = 0 ; private int sourceLength = - 1 ; private WorkingCopyOwner workingCopyOwner = DefaultWorkingCopyOwner . PRIMARY ; private IJavaProject project = null ; private String unitName = null ; private String [ ] classpaths ; private String [ ] sourcepaths ; private String [ ] sourcepathsEncodings ; private int bits ; ASTParser ( int level ) { if ( ( level != AST . JLS2_INTERNAL ) && ( level != AST . JLS3 ) ) { throw new IllegalArgumentException ( ) ; } this . apiLevel = level ; initializeDefaults ( ) ; } private List getClasspath ( ) throws IllegalStateException { Main main = new Main ( new PrintWriter ( System . out ) , new PrintWriter ( System . err ) , false , null , null ) ; ArrayList allClasspaths = new ArrayList ( ) ; try { if ( ( this . bits & CompilationUnitResolver . INCLUDE_RUNNING_VM_BOOTCLASSPATH ) != 0 ) { org . eclipse . jdt . internal . compiler . util . Util . collectRunningVMBootclasspath ( allClasspaths ) ; } if ( this . sourcepaths != null ) { for ( int i = 0 , max = this . sourcepaths . length ; i < max ; i ++ ) { String encoding = this . sourcepathsEncodings == null ? null : this . sourcepathsEncodings [ i ] ; main . processPathEntries ( Main . DEFAULT_SIZE_CLASSPATH , allClasspaths , this . sourcepaths [ i ] , encoding , true , false ) ; } } if ( this . classpaths != null ) { for ( int i = 0 , max = this . classpaths . length ; i < max ; i ++ ) { main . processPathEntries ( Main . DEFAULT_SIZE_CLASSPATH , allClasspaths , this . classpaths [ i ] , null , false , false ) ; } } ArrayList pendingErrors = main . pendingErrors ; if ( pendingErrors != null && pendingErrors . size ( ) != 0 ) { throw new IllegalStateException ( "" ) ; } } catch ( IllegalArgumentException e ) { throw new IllegalStateException ( "" ) ; } return allClasspaths ; } private void initializeDefaults ( ) { this . astKind = K_COMPILATION_UNIT ; this . rawSource = null ; this . typeRoot = null ; this . bits = 0 ; this . sourceLength = - 1 ; this . sourceOffset = 0 ; this . workingCopyOwner = DefaultWorkingCopyOwner . PRIMARY ; this . unitName = null ; this . project = null ; this . classpaths = null ; this . sourcepaths = null ; this . sourcepathsEncodings = null ; Map options = JavaCore . getOptions ( ) ; options . remove ( JavaCore . COMPILER_TASK_TAGS ) ; this . compilerOptions = options ; } public void setBindingsRecovery ( boolean enabled ) { if ( enabled ) { this . bits |= CompilationUnitResolver . BINDING_RECOVERY ; } else { this . bits &= ~ CompilationUnitResolver . BINDING_RECOVERY ; } } public void setEnvironment ( String [ ] classpathEntries , String [ ] sourcepathEntries , String [ ] encodings , boolean includeRunningVMBootclasspath ) { this . classpaths = classpathEntries ; this . sourcepaths = sourcepathEntries ; this . sourcepathsEncodings = encodings ; if ( encodings != null ) { if ( sourcepathEntries == null || sourcepathEntries . length != encodings . length ) { throw new IllegalArgumentException ( ) ; } } this . bits |= CompilationUnitResolver . INCLUDE_RUNNING_VM_BOOTCLASSPATH ; } public void setCompilerOptions ( Map options ) { if ( options == null ) { options = JavaCore . getOptions ( ) ; } else { options = new HashMap ( options ) ; } options . remove ( JavaCore . COMPILER_TASK_TAGS ) ; this . compilerOptions = options ; } public void setResolveBindings ( boolean enabled ) { if ( enabled ) { this . bits |= CompilationUnitResolver . RESOLVE_BINDING ; } else { this . bits &= ~ CompilationUnitResolver . RESOLVE_BINDING ; } } public void setFocalPosition ( int position ) { this . bits |= CompilationUnitResolver . PARTIAL ; this . focalPointPosition = position ; } public void setKind ( int kind ) { if ( ( kind != K_COMPILATION_UNIT ) && ( kind != K_CLASS_BODY_DECLARATIONS ) && ( kind != K_EXPRESSION ) && ( kind != K_STATEMENTS ) ) { throw new IllegalArgumentException ( ) ; } this . astKind = kind ; } public void setSource ( char [ ] source ) { this . rawSource = source ; this . typeRoot = null ; } public void setSource ( ICompilationUnit source ) { setSource ( ( ITypeRoot ) source ) ; } public void setSource ( IClassFile source ) { setSource ( ( ITypeRoot ) source ) ; } public void setSource ( ITypeRoot source ) { this . typeRoot = source ; this . rawSource = null ; if ( source != null ) { this . project = source . getJavaProject ( ) ; Map options = this . project . getOptions ( true ) ; options . remove ( JavaCore . COMPILER_TASK_TAGS ) ; this . compilerOptions = options ; } } public void setSourceRange ( int offset , int length ) { if ( offset < 0 || length < - 1 ) { throw new IllegalArgumentException ( ) ; } this . sourceOffset = offset ; this . sourceLength = length ; } public void setStatementsRecovery ( boolean enabled ) { if ( enabled ) { this . bits |= CompilationUnitResolver . STATEMENT_RECOVERY ; } else { this . bits &= ~ CompilationUnitResolver . STATEMENT_RECOVERY ; } } public void setIgnoreMethodBodies ( boolean enabled ) { if ( enabled ) { this . bits |= CompilationUnitResolver . IGNORE_METHOD_BODIES ; } else { this . bits &= ~ CompilationUnitResolver . IGNORE_METHOD_BODIES ; } } public void setWorkingCopyOwner ( WorkingCopyOwner owner ) { if ( owner == null ) { this . workingCopyOwner = DefaultWorkingCopyOwner . PRIMARY ; } else { this . workingCopyOwner = owner ; } } public void setUnitName ( String unitName ) { this . unitName = unitName ; } public void setProject ( IJavaProject project ) { this . project = project ; if ( project != null ) { Map options = project . getOptions ( true ) ; options . remove ( JavaCore . COMPILER_TASK_TAGS ) ; this . compilerOptions = options ; } } public ASTNode createAST ( IProgressMonitor monitor ) { ASTNode result = null ; if ( monitor != null ) monitor . beginTask ( "" , 1 ) ; try { if ( this . rawSource == null && this . typeRoot == null ) { throw new IllegalStateException ( "" ) ; } result = internalCreateAST ( monitor ) ; } finally { initializeDefaults ( ) ; if ( monitor != null ) monitor . done ( ) ; } return result ; } public void createASTs ( ICompilationUnit [ ] compilationUnits , String [ ] bindingKeys , ASTRequestor requestor , IProgressMonitor monitor ) { try { int flags = 0 ; if ( ( this . bits & CompilationUnitResolver . STATEMENT_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_STATEMENTS_RECOVERY ; } if ( ( this . bits & CompilationUnitResolver . IGNORE_METHOD_BODIES ) != 0 ) { flags |= ICompilationUnit . IGNORE_METHOD_BODIES ; } if ( ( this . bits & CompilationUnitResolver . RESOLVE_BINDING ) != 0 ) { if ( this . project == null ) throw new IllegalStateException ( "" ) ; if ( ( this . bits & CompilationUnitResolver . BINDING_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_BINDINGS_RECOVERY ; } CompilationUnitResolver . resolve ( compilationUnits , bindingKeys , requestor , this . apiLevel , this . compilerOptions , this . project , this . workingCopyOwner , flags , monitor ) ; } else { CompilationUnitResolver . parse ( compilationUnits , requestor , this . apiLevel , this . compilerOptions , flags , monitor ) ; } } finally { initializeDefaults ( ) ; } } public void createASTs ( String [ ] sourceFilePaths , String [ ] encodings , String [ ] bindingKeys , FileASTRequestor requestor , IProgressMonitor monitor ) { try { int flags = 0 ; if ( ( this . bits & CompilationUnitResolver . STATEMENT_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_STATEMENTS_RECOVERY ; } if ( ( this . bits & CompilationUnitResolver . IGNORE_METHOD_BODIES ) != 0 ) { flags |= ICompilationUnit . IGNORE_METHOD_BODIES ; } if ( ( this . bits & CompilationUnitResolver . RESOLVE_BINDING ) != 0 ) { if ( this . classpaths == null && this . sourcepaths == null && ( ( this . bits & CompilationUnitResolver . INCLUDE_RUNNING_VM_BOOTCLASSPATH ) == 0 ) ) { throw new IllegalStateException ( "" ) ; } if ( ( this . bits & CompilationUnitResolver . BINDING_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_BINDINGS_RECOVERY ; } CompilationUnitResolver . resolve ( sourceFilePaths , encodings , bindingKeys , requestor , this . apiLevel , this . compilerOptions , getClasspath ( ) , flags , monitor ) ; } else { CompilationUnitResolver . parse ( sourceFilePaths , encodings , requestor , this . apiLevel , this . compilerOptions , flags , monitor ) ; } } finally { initializeDefaults ( ) ; } } public IBinding [ ] createBindings ( IJavaElement [ ] elements , IProgressMonitor monitor ) { try { if ( this . project == null ) throw new IllegalStateException ( "" ) ; int flags = 0 ; if ( ( this . bits & CompilationUnitResolver . STATEMENT_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_STATEMENTS_RECOVERY ; } if ( ( this . bits & CompilationUnitResolver . BINDING_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_BINDINGS_RECOVERY ; } if ( ( this . bits & CompilationUnitResolver . IGNORE_METHOD_BODIES ) != 0 ) { flags |= ICompilationUnit . IGNORE_METHOD_BODIES ; } return CompilationUnitResolver . resolve ( elements , this . apiLevel , this . compilerOptions , this . project , this . workingCopyOwner , flags , monitor ) ; } finally { initializeDefaults ( ) ; } } private ASTNode internalCreateAST ( IProgressMonitor monitor ) { boolean needToResolveBindings = ( this . bits & CompilationUnitResolver . RESOLVE_BINDING ) != 0 ; switch ( this . astKind ) { case K_CLASS_BODY_DECLARATIONS : case K_EXPRESSION : case K_STATEMENTS : if ( this . rawSource == null ) { if ( this . typeRoot != null ) { if ( this . typeRoot instanceof ICompilationUnit ) { org . eclipse . jdt . internal . compiler . env . ICompilationUnit sourceUnit = ( org . eclipse . jdt . internal . compiler . env . ICompilationUnit ) this . typeRoot ; this . rawSource = sourceUnit . getContents ( ) ; } else if ( this . typeRoot instanceof IClassFile ) { try { String sourceString = this . typeRoot . getSource ( ) ; if ( sourceString != null ) { this . rawSource = sourceString . toCharArray ( ) ; } } catch ( JavaModelException e ) { StringWriter stringWriter = new StringWriter ( ) ; PrintWriter writer = null ; try { writer = new PrintWriter ( stringWriter ) ; e . printStackTrace ( writer ) ; } finally { if ( writer != null ) writer . close ( ) ; } throw new IllegalStateException ( String . valueOf ( stringWriter . getBuffer ( ) ) ) ; } } } } if ( this . rawSource != null ) { if ( this . sourceOffset + this . sourceLength > this . rawSource . length ) { throw new IllegalStateException ( ) ; } return internalCreateASTForKind ( ) ; } break ; case K_COMPILATION_UNIT : CompilationUnitDeclaration compilationUnitDeclaration = null ; try { NodeSearcher searcher = null ; org . eclipse . jdt . internal . compiler . env . ICompilationUnit sourceUnit = null ; WorkingCopyOwner wcOwner = this . workingCopyOwner ; if ( this . typeRoot instanceof ICompilationUnit ) { sourceUnit = ( org . eclipse . jdt . internal . compiler . env . ICompilationUnit ) this . typeRoot ; sourceUnit = new BasicCompilationUnit ( sourceUnit . getContents ( ) , sourceUnit . getPackageName ( ) , new String ( sourceUnit . getFileName ( ) ) , this . project ) ; wcOwner = ( ( ICompilationUnit ) this . typeRoot ) . getOwner ( ) ; } else if ( this . typeRoot instanceof IClassFile ) { try { String sourceString = this . typeRoot . getSource ( ) ; if ( sourceString == null ) { throw new IllegalStateException ( ) ; } PackageFragment packageFragment = ( PackageFragment ) this . typeRoot . getParent ( ) ; BinaryType type = ( BinaryType ) this . typeRoot . findPrimaryType ( ) ; IBinaryType binaryType = ( IBinaryType ) type . getElementInfo ( ) ; char [ ] fileName = binaryType . getFileName ( ) ; int firstDollar = CharOperation . indexOf ( '$' , fileName ) ; if ( firstDollar != - 1 ) { char [ ] suffix = SuffixConstants . SUFFIX_class ; int suffixLength = suffix . length ; char [ ] newFileName = new char [ firstDollar + suffixLength ] ; System . arraycopy ( fileName , 0 , newFileName , 0 , firstDollar ) ; System . arraycopy ( suffix , 0 , newFileName , firstDollar , suffixLength ) ; fileName = newFileName ; } sourceUnit = new BasicCompilationUnit ( sourceString . toCharArray ( ) , Util . toCharArrays ( packageFragment . names ) , new String ( fileName ) , this . project ) ; } catch ( JavaModelException e ) { StringWriter stringWriter = new StringWriter ( ) ; PrintWriter writer = null ; try { writer = new PrintWriter ( stringWriter ) ; e . printStackTrace ( writer ) ; } finally { if ( writer != null ) writer . close ( ) ; } throw new IllegalStateException ( String . valueOf ( stringWriter . getBuffer ( ) ) ) ; } } else if ( this . rawSource != null ) { needToResolveBindings = ( ( this . bits & CompilationUnitResolver . RESOLVE_BINDING ) != 0 ) && this . unitName != null && ( this . project != null || this . classpaths != null || this . sourcepaths != null || ( ( this . bits & CompilationUnitResolver . INCLUDE_RUNNING_VM_BOOTCLASSPATH ) != 0 ) ) && this . compilerOptions != null ; sourceUnit = new BasicCompilationUnit ( this . rawSource , null , this . unitName == null ? "" : this . unitName , this . project ) ; } else { throw new IllegalStateException ( ) ; } if ( ( this . bits & CompilationUnitResolver . PARTIAL ) != 0 ) { searcher = new NodeSearcher ( this . focalPointPosition ) ; } int flags = 0 ; if ( ( this . bits & CompilationUnitResolver . STATEMENT_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_STATEMENTS_RECOVERY ; } if ( searcher == null && ( ( this . bits & CompilationUnitResolver . IGNORE_METHOD_BODIES ) != 0 ) ) { flags |= ICompilationUnit . IGNORE_METHOD_BODIES ; } if ( needToResolveBindings ) { if ( ( this . bits & CompilationUnitResolver . BINDING_RECOVERY ) != 0 ) { flags |= ICompilationUnit . ENABLE_BINDINGS_RECOVERY ; } try { compilationUnitDeclaration = CompilationUnitResolver . resolve ( sourceUnit , this . project , getClasspath ( ) , searcher , this . compilerOptions , this . workingCopyOwner , flags , monitor ) ; } catch ( JavaModelException e ) { flags &= ~ ICompilationUnit . ENABLE_BINDINGS_RECOVERY ; compilationUnitDeclaration = CompilationUnitResolver . parse ( sourceUnit , searcher , this . compilerOptions , flags ) ; needToResolveBindings = false ; } } else { compilationUnitDeclaration = CompilationUnitResolver . parse ( sourceUnit , searcher , this . compilerOptions , flags ) ; needToResolveBindings = false ; } CompilationUnit result = CompilationUnitResolver . convert ( compilationUnitDeclaration , sourceUnit . getContents ( ) , this . apiLevel , this . compilerOptions , needToResolveBindings , wcOwner , needToResolveBindings ? new DefaultBindingResolver . BindingTables ( ) : null , flags , monitor , this . project != null ) ; result . setTypeRoot ( this . typeRoot ) ; return result ; } finally { if ( compilationUnitDeclaration != null && ( ( this . bits & CompilationUnitResolver . RESOLVE_BINDING ) != 0 ) ) { compilationUnitDeclaration . cleanUp ( ) ; } } } throw new IllegalStateException ( ) ; } private ASTNode internalCreateASTForKind ( ) { final ASTConverter converter = new ASTConverter ( this . compilerOptions , false , null ) ; converter . compilationUnitSource = this . rawSource ; converter . compilationUnitSourceLength = this . rawSource . length ; converter . scanner . setSource ( this . rawSource ) ; AST ast = AST . newAST ( this . apiLevel ) ; ast . setDefaultNodeFlag ( ASTNode . ORIGINAL ) ; ast . setBindingResolver ( new BindingResolver ( ) ) ; if ( ( this . bits & CompilationUnitResolver . STATEMENT_RECOVERY ) != 0 ) { ast . setFlag ( ICompilationUnit . ENABLE_STATEMENTS_RECOVERY ) ; } converter . setAST ( ast ) ; CodeSnippetParsingUtil codeSnippetParsingUtil = new CodeSnippetParsingUtil ( ( this . bits & CompilationUnitResolver . IGNORE_METHOD_BODIES ) != 0 ) ; CompilationUnit compilationUnit = ast . newCompilationUnit ( ) ; if ( this . sourceLength == - 1 ) { this . sourceLength = this . rawSource . length ; } switch ( this . astKind ) { case K_STATEMENTS : ConstructorDeclaration constructorDeclaration = codeSnippetParsingUtil . parseStatements ( this . rawSource , this . sourceOffset , this . sourceLength , this . compilerOptions , true , ( this . bits & CompilationUnitResolver . STATEMENT_RECOVERY ) != 0 ) ; RecoveryScannerData data = constructorDeclaration . compilationResult . recoveryScannerData ; if ( data != null ) { Scanner scanner = converter . scanner ; converter . scanner = new RecoveryScanner ( scanner , data . removeUnused ( ) ) ; converter . docParser . scanner = converter . scanner ; converter . scanner . setSource ( scanner . source ) ; compilationUnit . setStatementsRecoveryData ( data ) ; } RecordedParsingInformation recordedParsingInformation = codeSnippetParsingUtil . recordedParsingInformation ; int [ ] [ ] comments = recordedParsingInformation . commentPositions ; if ( comments != null ) { converter . buildCommentsTable ( compilationUnit , comments ) ; } compilationUnit . setLineEndTable ( recordedParsingInformation . lineEnds ) ; Block block = ast . newBlock ( ) ; block . setSourceRange ( this . sourceOffset , this . sourceOffset + this . sourceLength ) ; org . eclipse . jdt . internal . compiler . ast . Statement [ ] statements = constructorDeclaration . statements ; if ( statements != null ) { int statementsLength = statements . length ; for ( int i = 0 ; i < statementsLength ; i ++ ) { if ( statements [ i ] instanceof org . eclipse . jdt . internal . compiler . ast . LocalDeclaration ) { converter . checkAndAddMultipleLocalDeclaration ( statements , i , block . statements ( ) ) ; } else { Statement statement = converter . convert ( statements [ i ] ) ; if ( statement != null ) { block . statements ( ) . add ( statement ) ; } } } } rootNodeToCompilationUnit ( ast , compilationUnit , block , recordedParsingInformation , data ) ; ast . setDefaultNodeFlag ( 0 ) ; ast . setOriginalModificationCount ( ast . modificationCount ( ) ) ; return block ; case K_EXPRESSION : org . eclipse . jdt . internal . compiler . ast . Expression expression = codeSnippetParsingUtil . parseExpression ( this . rawSource , this . sourceOffset , this . sourceLength , this . compilerOptions , true ) ; recordedParsingInformation = codeSnippetParsingUtil . recordedParsingInformation ; comments = recordedParsingInformation . commentPositions ; if ( comments != null ) { converter . buildCommentsTable ( compilationUnit , comments ) ; } compilationUnit . setLineEndTable ( recordedParsingInformation . lineEnds ) ; if ( expression != null ) { Expression expression2 = converter . convert ( expression ) ; rootNodeToCompilationUnit ( expression2 . getAST ( ) , compilationUnit , expression2 , codeSnippetParsingUtil . recordedParsingInformation , null ) ; ast . setDefaultNodeFlag ( 0 ) ; ast . setOriginalModificationCount ( ast . modificationCount ( ) ) ; return expression2 ; } else { CategorizedProblem [ ] problems = recordedParsingInformation . problems ; if ( problems != null ) { compilationUnit . setProblems ( problems ) ; } ast . setDefaultNodeFlag ( 0 ) ; ast . setOriginalModificationCount ( ast . modificationCount ( ) ) ; return compilationUnit ; } case K_CLASS_BODY_DECLARATIONS : final org . eclipse . jdt . internal . compiler . ast . ASTNode [ ] nodes = codeSnippetParsingUtil . parseClassBodyDeclarations ( this . rawSource , this . sourceOffset , this . sourceLength , this . compilerOptions , true , ( this . bits & CompilationUnitResolver . STATEMENT_RECOVERY ) != 0 ) ; recordedParsingInformation = codeSnippetParsingUtil . recordedParsingInformation ; comments = recordedParsingInformation . commentPositions ; if ( comments != null ) { converter . buildCommentsTable ( compilationUnit , comments ) ; } compilationUnit . setLineEndTable ( recordedParsingInformation . lineEnds ) ; if ( nodes != null ) { TypeDeclaration typeDeclaration = converter . convert ( nodes ) ; typeDeclaration . setSourceRange ( this . sourceOffset , this . sourceOffset + this . sourceLength ) ; rootNodeToCompilationUnit ( typeDeclaration . getAST ( ) , compilationUnit , typeDeclaration , codeSnippetParsingUtil . recordedParsingInformation , null ) ; ast . setDefaultNodeFlag ( 0 ) ; ast . setOriginalModificationCount ( ast . modificationCount ( ) ) ; return typeDeclaration ; } else { CategorizedProblem [ ] problems = recordedParsingInformation . problems ; if ( problems != null ) { compilationUnit . setProblems ( problems ) ; } ast . setDefaultNodeFlag ( 0 ) ; ast . setOriginalModificationCount ( ast . modificationCount ( ) ) ; return compilationUnit ; } } throw new IllegalStateException ( ) ; } private void propagateErrors ( ASTNode astNode , CategorizedProblem [ ] problems , RecoveryScannerData data ) { astNode . accept ( new ASTSyntaxErrorPropagator ( problems ) ) ; if ( data != null ) { astNode . accept ( new ASTRecoveryPropagator ( problems , data ) ) ; } } private void rootNodeToCompilationUnit ( AST ast , CompilationUnit compilationUnit , ASTNode node , RecordedParsingInformation recordedParsingInformation , RecoveryScannerData data ) { final int problemsCount = recordedParsingInformation . problemsCount ; switch ( node . getNodeType ( ) ) { case ASTNode . BLOCK : { Block block = ( Block ) node ; if ( problemsCount != 0 ) { final CategorizedProblem [ ] problems = recordedParsingInformation . problems ; propagateErrors ( block , problems , data ) ; compilationUnit . setProblems ( problems ) ; } TypeDeclaration typeDeclaration = ast . newTypeDeclaration ( ) ; Initializer initializer = ast . newInitializer ( ) ; initializer . setBody ( block ) ; typeDeclaration . bodyDeclarations ( ) . add ( initializer ) ; compilationUnit . types ( ) . add ( typeDeclaration ) ; } break ; case ASTNode . TYPE_DECLARATION : { TypeDeclaration typeDeclaration = ( TypeDeclaration ) node ; if ( problemsCount != 0 ) { final CategorizedProblem [ ] problems = recordedParsingInformation . problems ; propagateErrors ( typeDeclaration , problems , data ) ; compilationUnit . setProblems ( problems ) ; } compilationUnit . types ( ) . add ( typeDeclaration ) ; } break ; default : if ( node instanceof Expression ) { Expression expression = ( Expression ) node ; if ( problemsCount != 0 ) { final CategorizedProblem [ ] problems = recordedParsingInformation . problems ; propagateErrors ( expression , problems , data ) ; compilationUnit . setProblems ( problems ) ; } ExpressionStatement expressionStatement = ast . newExpressionStatement ( expression ) ; Block block = ast . newBlock ( ) ; block . statements ( ) . add ( expressionStatement ) ; Initializer initializer = ast . newInitializer ( ) ; initializer . setBody ( block ) ; TypeDeclaration typeDeclaration = ast . newTypeDeclaration ( ) ; typeDeclaration . bodyDeclarations ( ) . add ( initializer ) ; compilationUnit . types ( ) . add ( typeDeclaration ) ; } } } } </s>