public static String format ( final String code , final Properties options , final LineEnding lineEnding ) { Check . notEmpty ( code , "code" ) ; Check . notEmpty ( options , "options" ) ; Check . notNull ( lineEnding , "lineEnding" ) ; final CodeFormatter formatter = ToolFactory . createCodeFormatter ( options ) ; final String lineSeparator = LineEnding . find ( lineEnding , code ) ; TextEdit te = null ; try { te = formatter . format ( CodeFormatter . K_COMPILATION_UNIT , code , 0 , code . length ( ) , 0 , lineSeparator ) ; } catch ( final Exception formatFailed ) { LOG . warn ( "Formatting failed" , formatFailed ) ; } String formattedCode = code ; if ( te == null ) { LOG . info ( "Code cannot be formatted. Possible cause is unmatched source/target/compliance version." ) ; } else { final IDocument doc = new Document ( code ) ; try { te . apply ( doc ) ; } catch ( final Exception e ) { LOG . warn ( e . getLocalizedMessage ( ) , e ) ; } formattedCode = doc . get ( ) ; } return formattedCode ; }
Pretty prints the given source code .