{
try
{
final String className = existingClass.getName();
final ClassPool pool = ClassPool.getDefault();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null)
{
pool.insertClassPath(new LoaderClassPath(contextClassLoader));
}
final CtClass ctClass = pool.get(className);
// - make sure the class isn't frozen
ctClass.defrost();
final String scriptWrapperFieldName = "scriptWrapper";
try
{
ctClass.getField(scriptWrapperFieldName);
}
catch (Exception exception)
{
final CtField scriptWrapper =
new CtField(
convert(
pool,
this.scriptWrapperName),
scriptWrapperFieldName,
ctClass);
scriptWrapper.setModifiers(Modifier.PRIVATE + Modifier.FINAL);
ctClass.addField(
scriptWrapper,
getScriptWrapperInitialization(
scriptDirectory,
className));
}
final CtMethod[] existingMethods = ctClass.getDeclaredMethods();
for (int ctr = 0; ctr < existingMethods.length; ctr++)
{
final CtMethod method = existingMethods[ctr];
if (!Modifier.isStatic(method.getModifiers()))
{
final CtClass returnType = method.getReturnType();
String methodBody;
if (returnType.equals(CtClass.voidType))
{
methodBody =
"{" + contructArgumentString(method) + "scriptWrapper.invoke(\"" + method.getName() +
"\", arguments);}";
}
else
{
if (returnType.isPrimitive())
{
methodBody =
"{" + contructArgumentString(method) + " return ((" + getWrapperTypeName(returnType) +
")scriptWrapper.invoke(\"" + method.getName() + "\", arguments))." +
returnType.getName() + "Value();}";
}
else
{
methodBody =
"{" + contructArgumentString(method) + " return (" + method.getReturnType().getName() +
")scriptWrapper.invoke(\"" + method.getName() + "\", arguments);}";
}
}
method.setBody(methodBody);
}
}
final File directory = getClassOutputDirectory(existingClass);
pool.writeFile(className,
directory != null ? directory.getAbsolutePath() : "");
}
catch (final Throwable throwable)
{
throwable.printStackTrace();