private static Pattern ClassDeclPattern = Pattern.compile("\\s*class\\s+(\\w+)\\s+extends\\s+([\\w\\.]+)\\s*");
private static Pattern ImportDeclPattern = Pattern.compile("(\\s*import\\s+[\\w\\.]+\\*?[\\s\\;]*)+");
protected Class groovyClassForBody (String packageName, String name, String parentClassName, String bodyString)
{
final FastStringBuffer buf = new FastStringBuffer();
String className = uniqueName(name, _classesByName);
String classString;
// support for scripts where we don't wrap it -- i.e. where script sets own parent class, imports, etc
// and can include multiple classes in single file.
Matcher m = ClassDeclPattern.matcher(bodyString);
if (m.find()) {
String theirName = m.group(1);
String theirSuper = m.group(2);
classString = m.replaceFirst(Fmt.S("\n\nclass %s extends %s ", className, theirSuper));
} else {
String extraImports = "";
m = ImportDeclPattern.matcher(bodyString);
if (m.find() && m.start() < 2) {
extraImports = m.group(0);
bodyString = m.replaceFirst("");
}
classString = Fmt.S("package %s\n%s%s\nclass %s extends %s {\n %s \n}",
packageName,
"import ariba.ui.aribaweb.core.*;import ariba.ui.widgets.*;import ariba.ui.table.*;import ariba.ui.outline.*;import ariba.util.core.*;",
extraImports, className, parentClassName, bodyString);
}
// use groovy class loader to load class as necessary...
GroovyClassLoader gcl = AWGroovyLoader.gcl();
Log.aribaweb.debug("--- Generating class: %s", className);
// classString += "\n\n class C2 { def test () { return \"Yeah!\" }; } \n";
Class cls;
try {
cls = gcl.parseClass(classString, className+".groovy");
} catch (Exception e) {
FastStringBuffer lineBuf = new FastStringBuffer();
String[] lines = classString.split("\n");
for (int i=0; i < lines.length; i++) {
lineBuf.append(Integer.toString(i+1));
lineBuf.append(": ");
lineBuf.append(lines[i]);
lineBuf.append("\n");
}
throw new AWGenericException(Fmt.S("Exception parsing groovy for class %s: %s\n%s", className,
e.getMessage(), lineBuf.toString()));
}
_classesByName.put(className, cls);
return cls;
}