protected void addOrRemoveGeneratedImport ( boolean add ) { final String IMPORT = "import javax.annotation.Generated;" ; int packageLine = - 1 ; int importLine = - 1 ; int lastJavaImport = - 1 ; int firstNonJavaImport = - 1 ; for ( int ii = 0 , nn = _lines . size ( ) ; ii < nn ; ii ++ ) { String line = _lines . get ( ii ) ; if ( line . startsWith ( IMPORT ) ) { if ( add ) { return ; // we already got one! } importLine = ii ; break ; } else if ( line . startsWith ( "package " ) ) { packageLine = ii ; } else if ( line . startsWith ( "import java" ) ) { lastJavaImport = ii ; } else if ( firstNonJavaImport == - 1 && line . startsWith ( "import " ) ) { firstNonJavaImport = ii ; } } if ( importLine != - 1 ) { // we must be removing, or we'd have already exited _lines . remove ( importLine ) ; } else if ( ! add ) { return ; // it's already not there! } else { importLine = ( lastJavaImport != - 1 ) ? lastJavaImport + 1 : ( ( firstNonJavaImport != - 1 ) ? firstNonJavaImport : packageLine + 1 ) ; _lines . add ( importLine , IMPORT ) ; } // the import line is always above these other lines, so they can be adjusted wholesale int adjustment = add ? 1 : - 1 ; _nstart += adjustment ; _nend += adjustment ; _mstart += adjustment ; _mend += adjustment ; }
Add or remove an import for