java Javassist。主要思想是什么,真正用在什么地方?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7297565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javassist. What is the main idea and where real use?
提问by user471011
I know that Javassist is a Java library providing a means to manipulate the Java bytecode of an application.
我知道 Javassist 是一个 Java 库,它提供了一种操作应用程序的 Java 字节码的方法。
Ok, but why we need manipulate bytecode?
好的,但是为什么我们需要操作字节码?
Any real example? Any real app, where javassist used?
有什么真实的例子吗?任何真正的应用程序,在哪里使用 javassist?
回答by meriton
A common application is to generate proxy classes at runtime, i.e. to create a subclass at runtime that intercepts all method invocations. Examples:
一个常见的应用是在运行时生成代理类,即在运行时创建一个拦截所有方法调用的子类。例子:
Hibernateuses Proxies to intercept method invocations on entities to implement lazy loading, i.e. fetching the object from the database when it is first accessed.
Hibernate使用代理来拦截实体上的方法调用以实现延迟加载,即在第一次访问时从数据库中获取对象。
The Spring Frameworkuses Proxies to implement its AOP support, which among other things powers its support for declarative transactions. It also uses proxies to enforce proper scoping.
在Spring框架使用代理服务器来实现其AOP的支持,其中除其他事项外大国的声明性事务的支持。它还使用代理来强制执行适当的范围。
EJBuses proxies to implement container managed transactions, authorization checking, and to apply user-defined interceptors.
EJB使用代理来实现容器管理的事务、授权检查和应用用户定义的拦截器。
CDIimplementations must also proxy the managed beans to ensure proper scoping. I suspect they use a byte code engineering library, too.
CDI实现还必须代理托管 bean 以确保正确的作用域。我怀疑他们也使用字节码工程库。
Irecently used Javassist to implement a transparent cache for method return values, by intercepting all method invocations and only delegating to the super implementation on the first invocation.
我最近使用 Javassist 通过拦截所有方法调用并仅在第一次调用时委托给超级实现来实现方法返回值的透明缓存。
Note that java.lang.reflect.Proxy
can generate proxy classes at runtime, but can only implement interfaces, not extend a class. All of the above use cases require the proxying of classes.
注意java.lang.reflect.Proxy
可以在运行时生成代理类,但只能实现接口,不能扩展类。以上所有用例都需要类的代理。
回答by ausgoo
Bytecode manipulation is useful and necessary, especially when you do not have source code for certain projects. Say you only have the bytecode (like a jar file) for some project, but you want somehow change the behavior of the code, the bytecode manipulation library can help in such cases. The advantage of bytecode manipulation is that you don't need to recompile your code and can directly execute it after manipulation.
字节码操作非常有用且必要,尤其是当您没有某些项目的源代码时。假设您只有某个项目的字节码(如 jar 文件),但您想以某种方式更改代码的行为,字节码操作库可以在这种情况下提供帮助。字节码操作的优点是不需要重新编译代码,操作后可以直接执行。
I have used bytecode manipulation to do some program analysis. Given a library, I want to know during the runtime what methods in the library have been invoked. I can use bytecode manipulation to insert a System.out.println("method_name");
statement in the beginning of a method. So during the runtime, it will print out what methods have been invoked.
我使用字节码操作来做一些程序分析。给定一个库,我想知道在运行时调用了库中的哪些方法。我可以使用字节码操作System.out.println("method_name");
在方法的开头插入一条语句。所以在运行时,它会打印出调用了哪些方法。
Some bytecode manipulation libraries are:
一些字节码操作库是:
回答by ebret
To extend Meriton answer and to provide a real example of use :
扩展 Meriton 的答案并提供一个真实的使用示例:
Hibernate-core (5.2.8.Final) use javaassit (3.20.0-GA):
Hibernate-core (5.2.8.Final) 使用 javaassit (3.20.0-GA):
https://mvnrepository.com/artifact/org.hibernate/hibernate-core/5.2.8.Final
https://mvnrepository.com/artifact/org.hibernate/hibernate-core/5.2.8.Final
回答by Eugene Kuleshov
Users page of the ASM project lists several dozen widely used Java projects and frameworks using ASM for bytecode analysis and manipulation. http://asm.ow2.org/users.html
ASM 项目的用户页面列出了几十个使用 ASM 进行字节码分析和操作的广泛使用的 Java 项目和框架。http://asm.ow2.org/users.html