- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Javassist ProxyFactory 和以下代码创建一个 Proxy 类:
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(entity.getClass());
factory.setInterfaces(new Class[] { MyCustomInterface.class });
.....
Class clazz = factory.createClass();
Object result = clazz.newInstance();
问题是我还需要向类添加一个字段。但是如果我这样做 CtClass proxy = ClassPool.getDefault().get(clazz.getName());
它会给出 NotFoundException
如何添加使用 createClass 创建的类的字段?有没有更好的方法来完成我想做的事情?
最佳答案
这是基于您对我的评论的回复。
您确实可以使用 MyCustomInterface
和您的 proxyClass 来创建某种 mixin在 java 。但您仍然需要从代理类转换为 MyCustomInterface
才能调用这些方法。
让我们开始吧。
首先创建代理,您已经在这样做:
// this is the code you've already posted
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(entity.getClass());
factory.setInterfaces(new Class[] { MyCustomInterface.class });
Javassist 代理允许您添加 MethodHandler 。它基本上在常规 Java 代理中具有 InvocableHandler 的作用,这意味着它作为方法拦截器工作。
方法处理程序将是你的 mixin!首先,您使用您实际想要添加到类中的自定义字段以及您已开始代理的实体对象创建一个新的 MethodHandler:
public class CustomMethodHandler implements MethodHandler {
private MyEntity objectBeingProxied;
private MyFieldType myCustomField;
public CustomMethodHandler(MyEntity entity) {
this.objectBeingProxied = entity;
}
// code here with the implementation of MyCustomInterface
// handling the entity and your customField
public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable {
String methodName = method.getName();
if(methodNameFromMyCustomInterface(methodName)) {
// handle methodCall internally:
// you can either do it by reflection
// or if needed if/then/else to dispatch
// to the correct method (*)
}else {
// it's just a method from entity let them
// go. Notice we're using proceed not method!
proceed.invoke(objectBeingProxied,args);
}
}
}
(*) 请注意,即使我在注释中说要在内部处理调用,您也可以在不是您的方法处理程序的另一个地方实现接口(interface)实现,然后从这里调用它。
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(entity.getClass());
factory.setInterfaces(new Class[] { MyCustomInterface.class });
Class cls = factory.createClass();
// bind your newly methodHandler to your proxy
((javassist.util.proxy.Proxy) cls).setHandler(new CustomMethodHandler(entity));
EntityClass proxyEntity = cls.newInstance();
您现在应该能够执行 ((MyCustomInterface)proxyEntity).someMethodFromTheInterface()
并让它由您的 CustomMethodHandler
处理
请记住,这些方法并不完美,这是实体类中的代码无法引用接口(interface)的缺点之一,除非您首先创建代理。
如果您有任何不清楚的地方,请发表评论,我会尽力为您澄清。
关于java - 将字段添加到使用 Javassist 创建的 Proxy 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20460262/
Maven Artifact 之间有什么区别 javassist:javassist 和 org.javassist:javassist 在我的项目中,我的第一个依赖项是 3.8 版本,而我引入的第二
我有一个 CtMethod实例,但我不知道如何从中获取参数(不是类型)的名称。我试过 getParameterTypes ,但它似乎只返回类型。 我假设这是可能的,因为我使用的库没有源,只有类文件,我
可以使用 CtMethod.setBody("..") 设置方法主体,但我没有找到任何 API 来获取字符串格式的方法主体。 最佳答案 这不可能。 Javassist 不是反编译器。类文件中的方法以
可以使用CtMethod.setBody("..")设置方法体,但我没有找到任何API来获取字符串格式的方法体。 最佳答案 这是不可能的。 Javassist 不是反编译器。类文件中的方法用 Java
当使用 javassist 检测 com.sun.net.* 类时,我可以成功使用 .insertAfter 方法。但是,当尝试从 Glassfish 中运行的应用程序检测第三方类时,我收到错误: F
我正在使用 javassist 来更改方法体。当该方法在应用程序中定义时我可以做到这一点。但是,当我想更改应用程序使用的 jar 文件中定义的方法时,我会收到以下运行时错误: javassist.Ca
我在模型上使用 javassist:com.project.model.Model我已经尝试过各种组合如何为 ClassPath 和 CtClass 格式化字符串,但无济于事。 ClassPo
我正在处理 SpringMVC、Hibernate 和 JSON,但出现此错误。 HTTP Status 500 - Could not write JSON: No serializer found
我正在使用 javassist 创建一个类并为其添加注释。当我使用 CtClass.writeFile 并且我看到带有 Java 反编译器的类文件时,注释就在那里,但是当我使用 class.getAn
我想读取一个方法的返回值,并且我必须将其传递给使用 method.insertAfter 插入的代码。 示例: public String sayHello(){ return "1"; }
这是原始方法: @GET @Produces({"application/json"}) public Response getTermClouds(@Context SecurityCo
我正在尝试在应用程序启动期间使用附加功能来增强一些代码。整个设置本身工作正常,但有一点我认为 javassist 可能会生成错误的代码。 我正在特定类的特定方法上执行此操作,我之前检查过返回值实际上是
我正在编写一些 Javassist 代码来拦截方法调用并用代理替换它们。为此,我使用 ExprEditor 按以下方式替换调用: public static void main(String[] ar
我目前正在实现一个注释,强制字段通过 javassist 遵守条件。我想检查一个字段在读取时是否已初始化...因此,目前,我通过在虚拟机通过 Translator.onLoad(ClassPool p
我正在尝试使用 javassist 在编译时插入一些代码片段 环境 Java 8 Spring 启动 2.2.6 hibernate 5.4.12.Final Javasist nl.topicu
我对 javassist 有点问题,用方法处理程序装饰类。问题是方法处理程序与 Abc 类中的 method1 一起正常工作,但不会拦截对 Def 内部类中的 method2 的调用。 public
我将代码注入(inject)到 doGet 方法中 val replace = "" + " System.out.println(\"[before] " + className + "\")
我试图捕获分支和循环语句的条件表达式中涉及的变量值,例如: if (a + b < c - 5) { // here, capture value of a, b and c // if bo
我正在开发一个需要类检测的项目。我使用 javassist 因为在我的情况下进行检测非常方便。 我遇到了一个问题,可以使用以下代码片段进行描述: 假设 1 类: public class Class1
我试图在 java 类文件中找到类初始值设定项。我可以找到该方法,但如果在类文件中找不到 main,那么我希望它找到类初始值设定项并在那里注入(inject)代码。 如何使用 Javassist 找到
我是一名优秀的程序员,十分优秀!