*
* The same file manager can be reopened for another compiler task.
* Thus we reduce the overhead of scanning through file system and jar
* files each time
*/
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager( null, Locale.getDefault(), null );
/*
* Prepare a list of compilation units (java source code file objects)
* to input to compilation task
*/
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList( javaFileObjects );
/* Prepare any compilation options to be used during compilation */
// In this example, we are asking the compiler to place the output
// files under bin folder.
String[] compileOptions = new String[] { "-d", "bin" };
Iterable<String> compilationOptionss = Arrays.asList( compileOptions );
/* Create a diagnostic controller, which holds the compilation problems */
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
/*
* Create a compilation task from compiler by passing in the required
* input objects prepared above
*/
CompilationTask compilerTask = compiler.getTask( null, stdFileManager, diagnostics, compilationOptionss, null, compilationUnits );
// Perform the compilation by calling the call method on compilerTask
// object.
boolean status = compilerTask.call();
Object instance = null;
if( !status ) {// If compilation error occurs
/* Iterate through each compilation problem and print it */
for(Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.format( "Error on line %d in %s", diagnostic.getLineNumber(), diagnostic );
}
} else {
// make our object
try {
Class<?> c = Class.forName( root_package + "." + name );
instance = c.newInstance();
// set its fields
System.out.println("all");
for(Field f : c.getDeclaredFields() ) {
System.out.println("Filed: " + f);
}
for(Entry<String, Object> entry : data.entrySet()) {
System.out.println("Field: " + c.getDeclaredField( entry.getKey() ) );
Field f =c.getDeclaredField( entry.getKey() );
f.set( instance, entry.getValue() );
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
stdFileManager.close();// Close the file manager
} catch (IOException e) {
e.printStackTrace();
}
return instance;