public void insertCode ( ) throws IOException , ParseException { CompilationUnit cuResult = JavaParser . parse ( baseFile ) ; if ( cuResult . getImports ( ) != null ) { List < ImportDeclaration > importsFromBaseFile = cuResult . getImports ( ) ; for ( ImportDeclaration eachImport : importsFromExtendedFile ) { if ( ! importAlreadyPresent ( importsFromBaseFile , eachImport ) ) { importsFromBaseFile . add ( eachImport ) ; } } cuResult . setImports ( importsFromBaseFile ) ; } String code = cuResult . toString ( ) ; BufferedWriter b = new BufferedWriter ( new FileWriter ( baseFile ) ) ; b . write ( code ) ; b . close ( ) ; }
This method will add methods fields and import statement to existing java file