ConstraintsGenerator extends AbstractLogEnabled implements Generator { public void generate( GenerationRequest request ) throws GenerationException { for ( String classname : request.getClasses().keySet() ) { Class<?> clazz; try { clazz = request.getClassLoader().loadClass( classname ); } catch ( ClassNotFoundException e ) { throw new GenerationException( e.getMessage(), e ); } List<Field> fieldsToGenerate = new ArrayList<Field>(); Field[] fields = clazz.getFields(); for ( Field field : fields ) { if ( isPublic( field.getModifiers() ) && isStatic( field.getModifiers() ) && isFinal( field.getModifiers() ) && isPrimitive( field.getType() ) ) { fieldsToGenerate.add( field ); } } if ( fieldsToGenerate.isEmpty() ) { continue; } File outDir = request.getTransientOutputFolder(); outDir = new File( outDir, clazz.getPackage().getName().replace( '.', '/' ) ); outDir.mkdirs(); File outFile = new File( outDir, clazz.getSimpleName() + ".as" ); FileWriter fw; try { fw = new FileWriter( outFile ); fw.append( "package " ).append( clazz.getPackage().getName() ).append( '{' ).append( '\n' ); fw.append( "public class " ).append( clazz.getSimpleName() ).append( '{' ).append( '\n' ); for ( Field field : fieldsToGenerate ) { fw.append( "public static const " ).append( field.getName() ).append( ':' ); fw.append( getAsType( field.getType() ) ).append( '=' ); fw.append( toString( field.get( clazz.newInstance() ) ) ).append( ';' ).append( '\n' ); } fw.append( '}' ).append( '\n' ); fw.append( '}' ).append( '\n' ); fw.flush(); fw.close(); } catch ( Exception e ) { throw new GenerationException( "Error generating " + clazz.getName(), e ); } } }  void generate( GenerationRequest request );  }
@Test public void testGenerate() throws GenerationException { TestGenerationRequest request = new TestGenerationRequest(); request.setTransientOutputFolder( new File( "./target/files" ) ); request.addClass( "net.flexmojos.oss.generator.contraints.ConstraintDemo", null ); request.setClassLoader( Thread.currentThread().getContextClassLoader() ); generator.generate( request ); }