use of org.codehaus.janino.SimpleCompiler in project JWildfire by thargor6.
the class CustomFullVariationWrapperFunc method compile.
/*
* compile "code" String,
* then take first class in compiled code that is a subclass of VariationFunc,
* create a new instance of this VariationFunc, and assign it to full_variation
*
* Still need to figure out a way to trigger TinaController.refreshParamCmb(TinaNonlinearControlsRow pRow, XForm pXForm, Variation pVar)
* in order to update TinaNonlinearControlsRow to reflect changed param names and values when code changes
*/
public void compile() {
if (DEBUG) {
System.out.println("called compile()");
}
// if there was a previous full_variation, keep it to try and copy shared params
VariationFunc prev_variation = full_variation;
try {
SimpleCompiler compiler = new SimpleCompiler();
compiler.cook(filtered_code);
ClassLoader cloader = compiler.getClassLoader();
Class varClass = null;
// a bunch of mucking about to find all classes compiled by compiler.cook(filtered_code)
// based on suggestion in
// https://stolenkid.wordpress.com/2009/03/11/browse-classloader/
// and
// http://stackoverflow.com/questions/2681459/how-can-i-list-all-classes-loaded-in-a-specific-class-loader
// but expanded to catch many possibilities for the "classes" field in ClassLoader,
// since above links only work if classesField.get(classloader) is a Vector,
// and it can be other Objects instead -- with current version of Janino it is a HashMap with class names as the keys
Field classesField = cloader.getClass().getDeclaredField("classes");
classesField.setAccessible(true);
Object classesLoaded = classesField.get(cloader);
Iterator classIter = null;
if (classesLoaded instanceof AbstractMap) {
classIter = ((AbstractMap) classesLoaded).keySet().iterator();
} else if (classesLoaded instanceof AbstractCollection) {
classIter = ((AbstractCollection) classesLoaded).iterator();
} else {
throw new IllegalArgumentException("unknown class " + String.valueOf(classesLoaded));
}
// construct full_variation as instance of first Class from classloader that is a subclass of VariationFunc
while (classIter.hasNext()) {
Object val = classIter.next();
String varClassName = null;
if (val instanceof String) {
varClassName = (String) val;
} else if (val instanceof Class) {
varClassName = ((Class) val).getName();
} else if (val instanceof Map.Entry) {
Map.Entry me = (Map.Entry) val;
if (me.getKey() instanceof String) {
varClassName = (String) me.getKey();
} else if (me.getValue() instanceof String) {
varClassName = (String) me.getValue();
} else if (me.getKey() instanceof Class) {
varClassName = ((Class) me.getKey()).getName();
} else if (me.getValue() instanceof Class) {
varClassName = ((Class) me.getValue()).getName();
} else {
varClassName = me.getValue().toString();
}
} else {
varClassName = val.toString();
}
varClass = Class.forName(varClassName, true, cloader);
if (DEBUG) {
System.out.println("className: " + varClassName);
System.out.println("instance of VariationFunc: " + VariationFunc.class.isAssignableFrom(varClass));
}
if (VariationFunc.class.isAssignableFrom(varClass)) {
full_variation = null;
full_variation = (VariationFunc) varClass.newInstance();
String[] inner_resource_names = full_variation.getRessourceNames();
// redo ressourceNames to include any resources of the inner full_variation (added after the RESSOURCE_CODE name)
if (inner_resource_names == null) {
ressourceNames = new String[1];
ressourceNames[0] = RESSOURCE_CODE;
} else {
ressourceNames = new String[inner_resource_names.length + 1];
ressourceNames[0] = RESSOURCE_CODE;
for (int k = 0; k < inner_resource_names.length; k++) {
ressourceNames[k + 1] = inner_resource_names[k];
}
if (DEBUG) {
System.out.println("new ressourceNames: " + Arrays.toString(ressourceNames));
}
}
if (DEBUG) {
System.out.println("full_variation: " + full_variation);
System.out.println("variation name: " + full_variation.getName());
}
break;
}
}
if (full_variation != null && prev_variation != null) {
// copy shared params from prev_variation
if (full_variation.getClass().getName().equals(prev_variation.getClass().getName())) {
if (DEBUG) {
System.out.println("variations compatible, copying params: " + full_variation.getClass().getName());
}
String[] prev_params = prev_variation.getParameterNames();
for (String prev_param : prev_params) {
Object prev_val = prev_variation.getParameter(prev_param);
Object cur_val = full_variation.getParameter(prev_param);
if (prev_val != null && cur_val != null) {
if (prev_val instanceof Number) {
full_variation.setParameter(prev_param, ((Number) prev_val).doubleValue());
if (DEBUG) {
System.out.println("param: " + prev_param + ", value: " + (Number) full_variation.getParameter(prev_param));
}
} else {
if (DEBUG) {
System.out.println("prev_val not a number: " + prev_val + ", " + prev_val.getClass().getName());
}
}
}
}
} else {
if (DEBUG) {
System.out.println("variations not compatible: " + full_variation.getClass().getName() + ", " + prev_variation.getClass().getName());
}
}
// should also copy shared resources??
}
} catch (Throwable ex) {
System.out.println("##############################################################");
System.out.println(ex.getMessage());
ex.printStackTrace();
System.out.println("##############################################################");
// full_variation = null;
}
}
use of org.codehaus.janino.SimpleCompiler in project incubator-systemml by apache.
the class CodegenUtils method compileClassJanino.
// //////////////////////////
// JANINO-specific methods (used for spark environments)
private static Class<?> compileClassJanino(String name, String src) {
try {
// compile source code
SimpleCompiler compiler = new SimpleCompiler();
compiler.cook(src);
// keep source code for later re-construction
_src.put(name, src);
// load compile class
return compiler.getClassLoader().loadClass(name);
} catch (Exception ex) {
LOG.error("Failed to compile class " + name + ": \n" + src);
throw new DMLRuntimeException("Failed to compile class " + name + ".", ex);
}
}
use of org.codehaus.janino.SimpleCompiler in project systemml by apache.
the class CodegenUtils method compileClassJanino.
// //////////////////////////
// JANINO-specific methods (used for spark environments)
private static Class<?> compileClassJanino(String name, String src) {
try {
// compile source code
SimpleCompiler compiler = new SimpleCompiler();
compiler.cook(src);
// keep source code for later re-construction
_src.put(name, src);
// load compile class
return compiler.getClassLoader().loadClass(name);
} catch (Exception ex) {
LOG.error("Failed to compile class " + name + ": \n" + src);
throw new DMLRuntimeException("Failed to compile class " + name + ".", ex);
}
}
Aggregations