Javassist,java
Javassist, java
我正在执行创建 java 应用程序的任务。任务是在不重新编译应用程序的情况下更改方法(计算公式)。在 Google 中搜索后,我发现您可以在 javassist 的帮助下解决问题。到目前为止,我正在尝试一个简单的例子。
因此,class 包含我想要更改的唯一方法
package ru.testScandJavaCafee.service;
public class Helper {
public String createList()
{
System.out.println("++++");
return "1000";
}
}
以及我从中更改方法的class
package ru.testScandJavaCafee.controller;
import javassist.*;
import ru.testScandJavaCafee.service.Helper;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by 15 on 06.05.2017.
*/
public class CoffeeTypeController {
public String createMethodHelper() throws NotFoundException, CannotCompileException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, InstantiationException, ClassNotFoundException {
ClassPool pool = ClassPool.getDefault();
ClassPool.getDefault().insertClassPath(new ClassClassPath(ru.testScandJavaCafee.service.Helper2.class));
CtClass cc = pool.get("ru.testScandJavaCafee.service.Helper");
CtMethod cm = cc.getMethod("createList","()Ljava/lang/String;" );
cm.setBody( "{ return \"200 \" ;}" );
cc.toClass();
Helper helper = new Helper();
Method method1 = helper.getClass().getMethod("createList");
String sum = (String)method1.invoke(helper);
return sum;
}
启动后得到输出
javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of org/apache/catalina/loader/WebappClassLoader): attempted duplicate class definition for name: "ru/testScandJavaCafee/service/Helper2"
at javassist.ClassPool.toClass(ClassPool.java:1085)
at javassist.ClassPool.toClass(ClassPool.java:1028)
at javassist.ClassPool.toClass(ClassPool.java:986)
at javassist.CtClass.toClass(CtClass.java:1110)
at ru.testScandJavaCafee.controller.CoffeeTypeController.createMethodHelper(CoffeeTypeController.java:111)
at ru.testScandJavaCafee.controller.CoffeeTypeController.doPost(CoffeeTypeController.java:56)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.LinkageError: loader (instance of org/apache/catalina/loader/WebappClassLoader): attempted duplicate class definition for name: "ru/testScandJavaCafee/service/Helper2"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javassist.ClassPool.toClass2(ClassPool.java:1098)
at javassist.ClassPool.toClass(ClassPool.java:1079)
... 27 more
帮助了解如何修复错误
你的错误在这一行:
ClassPool.getDefault().insertClassPath(new ClassClassPath(ru.testScandJavaCafee.service.Helper2.class));
首先,您已经创建了池,因此应将 class 路径插入该池。其次,class 路径应该是代码所在 class 的 class 名称。
pool.insertClassPath(new ClassClassPath(this.getClass()));
查看 documentation。关于 Class search path
的部分
使用
cc.getClass();
而不是:
cc.toClass();
根据我的经验,下面这行看起来是导致问题的原因:
cm.setBody( "{ return \"200 \" ;}" );
cc.toClass(); <<<<<<<<<<<<< // duplicate class definition here.
.toClass 本质上试图转换为 class,但它已经存在,这就是为什么您得到重复的 class 定义。
CtClass 寻找“.toClass()”
我也不明白你为什么要这样做,因为你没有在该行之外使用 cc
。
我正在执行创建 java 应用程序的任务。任务是在不重新编译应用程序的情况下更改方法(计算公式)。在 Google 中搜索后,我发现您可以在 javassist 的帮助下解决问题。到目前为止,我正在尝试一个简单的例子。 因此,class 包含我想要更改的唯一方法
package ru.testScandJavaCafee.service;
public class Helper {
public String createList()
{
System.out.println("++++");
return "1000";
}
}
以及我从中更改方法的class
package ru.testScandJavaCafee.controller;
import javassist.*;
import ru.testScandJavaCafee.service.Helper;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by 15 on 06.05.2017.
*/
public class CoffeeTypeController {
public String createMethodHelper() throws NotFoundException, CannotCompileException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, InstantiationException, ClassNotFoundException {
ClassPool pool = ClassPool.getDefault();
ClassPool.getDefault().insertClassPath(new ClassClassPath(ru.testScandJavaCafee.service.Helper2.class));
CtClass cc = pool.get("ru.testScandJavaCafee.service.Helper");
CtMethod cm = cc.getMethod("createList","()Ljava/lang/String;" );
cm.setBody( "{ return \"200 \" ;}" );
cc.toClass();
Helper helper = new Helper();
Method method1 = helper.getClass().getMethod("createList");
String sum = (String)method1.invoke(helper);
return sum;
}
启动后得到输出
javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of org/apache/catalina/loader/WebappClassLoader): attempted duplicate class definition for name: "ru/testScandJavaCafee/service/Helper2"
at javassist.ClassPool.toClass(ClassPool.java:1085)
at javassist.ClassPool.toClass(ClassPool.java:1028)
at javassist.ClassPool.toClass(ClassPool.java:986)
at javassist.CtClass.toClass(CtClass.java:1110)
at ru.testScandJavaCafee.controller.CoffeeTypeController.createMethodHelper(CoffeeTypeController.java:111)
at ru.testScandJavaCafee.controller.CoffeeTypeController.doPost(CoffeeTypeController.java:56)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.LinkageError: loader (instance of org/apache/catalina/loader/WebappClassLoader): attempted duplicate class definition for name: "ru/testScandJavaCafee/service/Helper2"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javassist.ClassPool.toClass2(ClassPool.java:1098)
at javassist.ClassPool.toClass(ClassPool.java:1079)
... 27 more
帮助了解如何修复错误
你的错误在这一行:
ClassPool.getDefault().insertClassPath(new ClassClassPath(ru.testScandJavaCafee.service.Helper2.class));
首先,您已经创建了池,因此应将 class 路径插入该池。其次,class 路径应该是代码所在 class 的 class 名称。
pool.insertClassPath(new ClassClassPath(this.getClass()));
查看 documentation。关于 Class search path
使用
cc.getClass();
而不是:
cc.toClass();
根据我的经验,下面这行看起来是导致问题的原因:
cm.setBody( "{ return \"200 \" ;}" );
cc.toClass(); <<<<<<<<<<<<< // duplicate class definition here.
.toClass 本质上试图转换为 class,但它已经存在,这就是为什么您得到重复的 class 定义。
CtClass 寻找“.toClass()”
我也不明白你为什么要这样做,因为你没有在该行之外使用 cc
。