Following my article on Dynamic and Parsable Properties, I decided to write a utility class that would parse a properties file and generate the class for it. This way, if you have a particularly large properties file, you don’t have to spend too much time on the class to read the properties.
Usage
Assuming you have some properties file called test.properties, this will generate the class for it. Since it works in conjunction with my ParsableProperties, this will “guess” at the data type and create appropriate getters. Currently it only supports Boolean, Integer, Double, String, Date, and URL. Below, I just pass in the properties file and the output path where the generated class should be saved. I also specify the class name, package name, and in this case (for demonstration purposes) I’ve overridden the default list delimiter to use a comma rather than a tilde. (In my ParsableProperties, the default list delimiter is a tilde because you might use commas in number data types).
package net.chiappone.properties.generator.test;
import java.io.File;
import net.chiappone.properties.generator.PropertyClassGenerator;
public class TestPropertyClassGenerator {
public static void main( String[] args ) throws Exception {
File propertyFile = new File( "test.properties" );
File outputPath = new File( "src/net/chiappone/properties/generator/test/generated" );
PropertyClassGenerator generator = new PropertyClassGenerator( propertyFile, outputPath );
generator.setPackageName( "net.chiappone.properties.generator.test.generated" );
generator.setClassName( "TestProperties" );
generator.setListDelimiter( "," );
generator.run();
}
}
- JAR files – for using in your project (49.7 KB)
- Source Code – includes source, JAR, and tests (74.3 KB)
