Is it possible to do bytecode manipulation when us

2019-06-16 03:06发布

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.

  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.

2条回答
\"骚年 ilove
2楼-- · 2019-06-16 03:50

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-06-16 03:54

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.

查看更多
登录 后发表回答