使用 OSGi 时是否可以进行字节码操作? [英] Is it possible to do bytecode manipulation when using OSGi?

查看:25
本文介绍了使用 OSGi 时是否可以进行字节码操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用服务器,我需要在其中使用一些字节码操作(例如插入自定义equalshashCode 方法到用 @Entity 注释的类).现在我给 JVM 一个 Java 代理(-javaagent 选项)它使用 ASM 进行字节码转换.

I'm making an application server and in it I need to use some bytecode manipulation (e.g. inserting custom equals and hashCode methods to classes annotated with @Entity). Now I give the JVM a Java Agent (the -javaagent option) which does bytecode transformations using ASM.

我一直在考虑使用 OSGi,但我不知道它是否允许我进行必要的字节码操作.

I've been considering using OSGi, but I don't know whether it allows me to do the necessary bytecode manipulation.

  1. 在使用 OSGi 时是否可以进行字节码操作?怎么样?
  2. bundle 是否可以声明自己,它需要一些字节码操作才能工作?例如,应用服务器要求操作它自己的一些核心类.
  3. 是否可以指定某个其他包需要一些字节码操作,而该包不知道呢?例如,在服务器上运行的所有应用程序都必须被操纵,但应用程序不需要知道它.
  4. bundle 是否可以声明自己,所有依赖它的 bundle 都必须是字节码操作?这将使我很容易声明所有依赖包含 @Entity 注释的 API 包的人都必须被操纵.
  1. Is it possible to do bytecode manipulation when using OSGi? How?
  2. Is it possible for a bundle to declare itself, that it requires some bytecode manipulation for it to work? For example the application server requires that some of its own core classes are manipulated.
  3. Is it possible to specify that some other bundle requires some bytecode manipulation, without that bundle knowing about it? For example all applications which run on the server must be manipulated, but the applications don't need to know about it.
  4. Is it possible for a bundle to declare itself, that all bundles which depend on it must be bytecode manipulated? This would make it easy for me to declare that all who depend on the API bundle which contains the @Entity annotation, must be manipulated.

推荐答案

OSGI 4.3 添加了 WeavingHook 允许您使用您选择的字节码操作器进行字节码操作.我在我的一个项目中将它与 JavaAssist 一起使用,效果很好.

OSGI 4.3 added the WeavingHook which allows you to do byte code manipulation with a byte code manipulator of your choice. I use it with JavaAssist on one of my projects and it works great.

这篇关于使用 OSGi 时是否可以进行字节码操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆