使用 OSGi 时是否可以进行字节码操作?
我正在制作 应用程序服务器,在其中我需要使用一些字节码操作(例如插入自定义 使用
和 @Entity
注释的类的 equalshashCode
方法)。 现在我给 JVM 一个 Java 代理(-javaagent 选项)使用 ASM 进行字节码转换。
我一直在考虑使用 OSGi,但我不知道它是否允许我这样做必要的字节码操作。
- 使用 OSGi 时是否可以进行字节码操作? 如何?
- 包是否可以声明自己,它需要一些字节码操作才能工作? 例如,应用程序服务器要求操作它自己的一些核心类。
- 是否可以指定其他某个捆绑包需要一些字节码操作,而该捆绑包不知道这一点? 例如,必须操纵服务器上运行的所有应用程序,但应用程序不需要知道这一点。
- 一个包是否可以声明自己,所有依赖它的包都必须进行字节码操作? 这将使我可以轻松地声明所有依赖于包含
@Entity
注释的 API 包的人都必须进行操作。
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.
I've been considering using OSGi, but I don't know whether it allows me to do the necessary bytecode manipulation.
- Is it possible to do bytecode manipulation when using OSGi? How?
- 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.
- 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.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
1) 是的,可以在 OSGi 中进行字节码操作。 与标准 java 的方式有点不同,您需要使用此处描述的扩展器模式 http://www.osgi.org/blog/2007/02/osgi-extender-model.html。
我相信 Eclipse 在他们的春分方面项目中使用了它: http://www.eclipse.org /equinox/incubator/aspects/。 Spring DM 肯定是使用这种模式来自动配置 osgi dm 模块。
2)这取决于您使用的扩展模式。 这就是 spring 扩展器的工作方式,在平台启动配置文件之前,在包的 META-INF/spring 文件夹中查找配置文件。
3)同样,这取决于您定义的扩展器。
4)我相信这是可能的,因为在启动包之前,它必须解析并启动依赖项,然后您的扩展程序可以分析包依赖项并启动字节码操作。
1) Yes, it is possible to do bytecode manipulation in OSGi. The how is a bit different than with standard java, you need to use the extender pattern describe here http://www.osgi.org/blog/2007/02/osgi-extender-model.html.
I believe Eclipse is using that in their equinox aspect project: http://www.eclipse.org/equinox/incubator/aspects/. Spring DM is definitely using this pattern to autoconfigure osgi dm module.
2) that would be up to the extender pattern you use. tha's how the spring extender is working, looking for configuration files in the META-INF/spring folder of bundle before they are started by the platform.
3) Again, this would be up to the extender you define.
4) I believe this would be possible since before a bundle can be started, it has to resolved and dependencies started, your extender could then analyze bundle dependencies and start the byte code manipulation.