我最近是Javassist
的参与者,希望在方法中捕获this
对象,向this
类添加新方法并对{的实例执行新的字节码{1}}。
我从一个简单的this
类
Test
}
我正在尝试将public class Test
{
public String eventId;
public String getEventId()
{
DynamicBehaviorControl.executeBehaviorForEventTest("{ eventId = \"163\"; }", "Test", this);
return eventId;
}
修改为this
Test test = new Test()
正在执行test
,其中getEventId() DynamicBehaviorControl.executeBehaviorForEventTest("{ eventId = \"163\"; }", "Test", this);
的定义是
executeBehaviorForEventTest
和public static void executeBehaviorForEventTest(String behavior, String classname, Object object)
{
dynamicBehaviorModification(behavior, classname, object);
}
是
dynamicBehaviorModification
我假设一个对象可以修改另一个提供 protected static void dynamicBehaviorModification(String methodBehavior, String className, Object otherobject) {
classPool = ClassPool.getDefault();
classLoader = new Loader(classPool);
try
{
//compileTimeClass = classPool.makeClass(className);
compileTimeClass = classPool.get(className);
compileTimeMethod = CtNewMethod.make("public void executeOnBehalfOfEvents()" + methodBehavior, compileTimeClass);
//compileTimeMethod = compileTimeClass.getDeclaredMethod(methodName);
//compileTimeMethod.setBody(methodBehavior);
compileTimeClass.addMethod(compileTimeMethod);
compileTimeClass.writeFile();
//classLoader.loadClass(className);
//compileTimeClass = classPool.get(className);
Class<?> clazz = classLoader.loadClass(className);
//Object anotherObject = clazz.newInstance();
Object object = clazz.newInstance();
method = otherobject.getClass().getDeclaredMethod(methodName);
method.invoke(otherobject, new Object[]{});
purgeAndKill();
}
catch (CannotCompileException cannotCompileException)
{
logger.debug(cannotCompileException.getMessage() + " | " + cannotCompileException .getCause() + " | " +
"ctMethod.setBody(\"{\" + behavior + \"}\"); or ctClass.toClass();");
}
catch (SecurityException securityException)
{
logger.debug(securityException.getMessage() + " | " + securityException .getCause() + " | " +
"method = clazz.getMethod(methodName, null);");
}
catch (NoSuchMethodException noSuchMethodException)
{
logger.debug(noSuchMethodException.getMessage() + " | " + noSuchMethodException .getCause() + " | " +
"method = clazz.getMethod(methodName, null);");
}
catch (IllegalArgumentException illegalArgumentException)
{
logger.debug(illegalArgumentException.getMessage() + " | " + illegalArgumentException .getCause() + " | " +
"method.invoke(invocationObject);");
}
catch (IllegalAccessException illegalAccessException)
{
logger.debug(illegalAccessException.getMessage() + " | " + illegalAccessException .getCause() + " | " +
"method.invoke(invocationObject);");
}
catch (InvocationTargetException illegalInvocationTargetException)
{
logger.debug(illegalInvocationTargetException.getMessage() + " | " + illegalInvocationTargetException .getCause() + " | " +
"method.invoke(invocationObject);");
}
catch (InstantiationException instantiationException)
{
logger.debug(instantiationException.getMessage() + " | " + instantiationException .getCause() + " | " +
"Object object = clazz.newInstance();");
}
catch (ClassNotFoundException classNotFoundException)
{
logger.debug(classNotFoundException.getMessage() + " | " + classNotFoundException .getCause() + " | " +
"classLoader.loadClass(className);");
}
catch (NotFoundException notfoundException)
{
logger.debug(notfoundException.getMessage() + " | " + notfoundException .getCause() + " | " +
"Object object = clazz.newInstance();");
}catch (IOException ioException) {
logger.debug(ioException.getMessage() + " | " + ioException .getCause() + " | " +
"compileTimeClass.writeFile();");
}
}
对象的实例。我不理解的部分是如何获取this
然后注入新的this
,因此bytecode
不会返回method = object.getClass().getDeclaredMethod(methodName);
。