public void run ( String [ ] args ) throws Exception { // Check the argument if ( args . length != 1 ) throw new RuntimeException ( "A file path was expected as an argument." ) ; File targetFile = new File ( args [ 0 ] ) ; if ( ! targetFile . isFile ( ) ) throw new RuntimeException ( "File " + targetFile + " does not exist." ) ; // Prepare the comments for all the properties Field [ ] fields = IPreferencesMngr . class . getDeclaredFields ( ) ; Map < String , String > propertyNameToComment = new HashMap <> ( ) ; for ( Field field : fields ) { String constantName = ( String ) field . get ( null ) ; PreferenceDescription annotation = field . getAnnotation ( PreferenceDescription . class ) ; if ( annotation == null ) throw new RuntimeException ( "Documentation was not written for the " + field . getName ( ) + " constant in IPreferencesMngr.java." ) ; StringBuilder sb = new StringBuilder ( ) ; sb . append ( annotation . desc ( ) ) ; String [ ] values = annotation . values ( ) ; if ( values . length > 0 ) { sb . append ( "\n\nPossible values:\n" ) ; for ( String value : values ) { sb . append ( " - " ) ; sb . append ( value ) ; sb . append ( "\n" ) ; } } propertyNameToComment . put ( constantName , sb . toString ( ) ) ; } // Prepare the content to generate Defaults def = new Defaults ( ) ; StringBuilder sb = new StringBuilder ( ) ; for ( PreferenceKeyCategory cat : PreferenceKeyCategory . values ( ) ) { sb . append ( "\n###\n# " ) ; sb . append ( cat . getDescription ( ) ) ; sb . append ( "\n###\n" ) ; for ( Map . Entry < String , PreferenceKeyCategory > entry : def . keyToCategory . entrySet ( ) ) { if ( cat != entry . getValue ( ) ) continue ; String comment = propertyNameToComment . get ( entry . getKey ( ) ) ; comment = comment . replaceAll ( "^" , "# " ) . replace ( "\n" , "\n# " ) ; sb . append ( "\n" ) ; sb . append ( comment ) ; sb . append ( "\n" ) ; sb . append ( entry . getKey ( ) ) ; sb . append ( " = " ) ; String defaultValue = def . keyToDefaultValue . get ( entry . getKey ( ) ) ; if ( ! Utils . isEmptyOrWhitespaces ( defaultValue ) ) sb . append ( defaultValue . trim ( ) ) ; sb . append ( "\n" ) ; } sb . append ( "\n" ) ; } // Update the file Utils . appendStringInto ( sb . toString ( ) , targetFile ) ; }
The real method that does the job .