@ Override public JCCompilationUnit parseCompilationUnit ( ) { Token firstToken = token ; JCModifiers mods = null ; boolean seenImport = false ; boolean seenPackage = false ; ListBuffer < JCTree > defs = new ListBuffer <> ( ) ; if ( token . kind == MONKEYS_AT ) { mods = modifiersOpt ( ) ; } boolean firstTypeDecl = true ; while ( token . kind != EOF ) { if ( token . pos > 0 && token . pos <= endPosTable . errorEndPos ) { // error recovery skip ( true , false , false , false ) ; if ( token . kind == EOF ) { break ; } } if ( mods == null && token . kind == IMPORT ) { seenImport = true ; defs . append ( importDeclaration ( ) ) ; } else { Comment docComment = token . comment ( CommentStyle . JAVADOC ) ; if ( firstTypeDecl && ! seenImport && ! seenPackage ) { docComment = firstToken . comment ( CommentStyle . JAVADOC ) ; } List < ? extends JCTree > udefs = replUnit ( mods , docComment ) ; // if (def instanceof JCExpressionStatement) // def = ((JCExpressionStatement)def).expr; for ( JCTree def : udefs ) { defs . append ( def ) ; } mods = null ; firstTypeDecl = false ; } break ; // Remove to process more than one snippet } List < JCTree > rdefs = defs . toList ( ) ; class ReplUnit extends JCCompilationUnit { public ReplUnit ( List < JCTree > defs ) { super ( defs ) ; } } JCCompilationUnit toplevel = new ReplUnit ( rdefs ) ; if ( rdefs . isEmpty ( ) ) { storeEnd ( toplevel , S . prevToken ( ) . endPos ) ; } toplevel . lineMap = S . getLineMap ( ) ; this . endPosTable . setParser ( null ) ; // remove reference to parser toplevel . endPositions = this . endPosTable ; return toplevel ; }
As faithful a clone of the overridden method as possible while still achieving the goal of allowing the parse of a stand - alone snippet . As a result some variables are assigned and never used tests are always true loops don t etc . This is to allow easy transition as the underlying method changes .