爪哇助手。主要思想是什么以及实际用途是什么?
我知道 Javassist 是一个 Java 库,提供了一种操作应用程序的 Java 字节码的方法。
好的,但是为什么我们需要操作字节码呢?
有什么真实的例子吗? 有没有真正的应用程序,其中使用了 javassist?
I know that Javassist is a Java library providing a means to manipulate the Java bytecode of an application.
Ok, but why we need manipulate bytecode?
Any real example?
Any real app, where javassist used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
常见的应用是在运行时生成代理类,即在运行时创建一个拦截所有方法调用的子类。示例:
Hibernate 使用代理来拦截实体上的方法调用,以实现延迟加载,即在第一次访问对象时从数据库中获取对象。
Spring框架 使用代理来实现其 AOP 支持,这尤其增强了对声明式事务的支持。它还使用代理来强制执行适当的范围界定。
EJB 使用代理来实现容器管理的事务、授权检查以及应用用户定义的拦截器。
CDI 实现还必须代理托管 bean,以确保正确的作用域。我怀疑他们也使用字节码工程库。
我最近使用 Javassist 来实现方法返回值的透明缓存,方法是拦截所有方法调用并仅在第一次调用时委托给超级实现。
注意,
java.lang.reflect.Proxy
可以在运行时生成代理类,但只能实现接口,不能扩展类。上述所有用例都需要类的代理。A common application is to generate proxy classes at runtime, i.e. to create a subclass at runtime that intercepts all method invocations. Examples:
Hibernate uses Proxies to intercept method invocations on entities to implement lazy loading, i.e. fetching the object from the database when it is first accessed.
The Spring Framework uses Proxies to implement its AOP support, which among other things powers its support for declarative transactions. It also uses proxies to enforce proper scoping.
EJB uses proxies to implement container managed transactions, authorization checking, and to apply user-defined interceptors.
CDI implementations must also proxy the managed beans to ensure proper scoping. I suspect they use a byte code engineering library, too.
I recently 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.
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.字节码操作是有用且必要的,特别是当您没有某些项目的源代码时。假设您只有某个项目的字节码(如 jar 文件),但您希望以某种方式更改代码的行为,字节码操作库可以在这种情况下提供帮助。字节码操作的优点是不需要重新编译代码,操作后可以直接执行。
我使用字节码操作来进行一些程序分析。给定一个库,我想知道在运行时调用了库中的哪些方法。我可以使用字节码操作在方法的开头插入
System.out.println("method_name");
语句。所以在运行时,它会打印出调用了哪些方法。一些字节码操作库是:
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.
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.Some bytecode manipulation libraries are:
为了扩展 Meriton 答案并提供真实的使用示例:
Hibernate-core (5.2.8.Final) 使用 javaassit (3.20.0-GA):
https://mvnrepository.com/artifact/org.hibernate/hibernate-core/5.2.8.Final
To extend Meriton answer and to provide a real example of use :
Hibernate-core (5.2.8.Final) use javaassit (3.20.0-GA):
https://mvnrepository.com/artifact/org.hibernate/hibernate-core/5.2.8.Final
ASM 项目的用户页面列出了数十个广泛使用的 Java 项目和框架,它们使用 ASM 进行字节码分析和操作。 http://asm.ow2.org/users.html
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