String getClassForParameter(String object, String field, boolean type) { for (KVP def : comps.entries) { if (object.equals(def.getKey())) { String clname = def.getValue().toString(); clname = getComponentClassName(clname); Class c; try { c = getClassLoader().loadClass(clname); } catch (ClassNotFoundException ex) { throw new ComponentException("Class not found: '" + clname + "'"); } try { if (type) { String canName = c.getField(field).getType().getCanonicalName(); if (canName == null) { throw new ComponentException("No canonical type name for : " + field); } return canName; } else { Role r = c.getField(field).getAnnotation(Role.class); if (r != null) { return r.value(); } return Role.VARIABLE; } } catch (NoSuchFieldException ex) { throw new ComponentException("No such field: " + field); } catch (SecurityException ex) { throw new ComponentException("Cannot access : " + field); } } } throw new ComponentException( "Cannot find component '" + object + "'. in '" + object + "." + field + "'"); }
void kvpExpand(StringBuilder b, List<KVP> l, String method) { for (KVP c : l) { String[] from = c.getKey().split("\\."); String tos = c.getValue().toString().trim(); String[] t = tos.split("\\s+"); // multiple @In for (String kvp : t) { String to_obj = kvp; // default target is just object name String to_field = from[1]; // same as @Out if (kvp.indexOf('.') > 0) { String[] to = kvp.split("\\."); to_obj = to[0]; to_field = to[1]; } b.append( " " + method + "(" + from[0] + ", \"" + from[1] + "\", " + to_obj + ", \"" + to_field + "\");\n"); } } }
private StringBuilder generateSource() throws Exception { if (control != null) { if (control.indexOf('.') == -1) { throw new IllegalArgumentException( "Not a valid control reference (object.field): '" + control + "'"); } } StringBuilder b = new StringBuilder(); b.append("import java.util.*;\n"); b.append("import oms3.*;\n"); b.append("import oms3.annotations.*;\n"); b.append("public class " + "$$$" + " extends " + controlClass + " {\n"); b.append("\n"); // Fields for (Param param : getParam()) { String p = param.getName(); if (p.indexOf('.') == -1) { throw new IllegalArgumentException( "Not a valid parameter reference (object.field): '" + p + "'"); } String[] name = p.split("\\."); String type = getClassForParameter(name[0], name[1], true); String role = getClassForParameter(name[0], name[1], false); b.append(" // " + p + "\n"); b.append(" @Role(\"" + role + "\")\n"); b.append(" @In public " + type + " " + name[0] + "_" + name[1] + ";\n"); b.append("\n"); } // Components for (KVP def : comps.entries) { String compClass = def.getValue().toString(); compClass = getComponentClassName(compClass); // name -> class b.append(" public " + compClass + " " + def.getKey() + " = new " + compClass + "();\n"); } b.append("\n\n"); // init version. b.append(" @Initialize\n"); b.append(" public void init() {\n"); if (control != null) { String[] it = control.split("\\."); b.append(" conditional(" + it[0] + ", \"" + it[1] + "\");\n"); } // in2in for (Param param : getParam()) { String[] name = param.getName().split("\\."); b.append( " in2in(\"" + name[0] + '_' + name[1] + "\", " + name[0] + ", \"" + name[1] + "\");\n"); } // out2in kvpExpand(b, out2in.entries, "out2in"); // feedback kvpExpand(b, feedback.entries, "feedback"); b.append(" initializeComponents();\n"); b.append(" }\n"); b.append("}\n"); return b; }