bytecode instrumentation using ClassFileTransformer.transform

会有一股神秘感。 提交于 2019-12-05 09:24:37
VonC

Possible causes

.

Incorrect MANIFEST

Did you follow all the steps required to define an Instrumentation class ?

Especially the "packaging" step, which involve specifing a slightly different set of attributes in the JAR’s manifest:

  • Instead of Main-Class, you must specify a Premain-Class attribute that gives the full class-name of the class that implements premain() in your agent.
Premain-Class: my.package.MyAgentClass
  • If your agent uses any class-libraries, then you should specify the Boot-Class-Path attribute, since your instrumentation agent will need its libraries to be visible from the bootstrap classloader.
    If you don’t do this, you will probably have to use the wacky -Xbootclasspath/a:... argument to the JVM.
    The command-line argument is a decent way to get things going, but you want to use this attribute in the long-run, because the command-line is already growing from having to specify the Java instrumentation agent. Might as well keep it as simple as possible.

  • Finally, there is the Can-Redefine-Classes attribute.
    If it is set to true, then the Java instrumentation agent is able to redefine the classes that the agent itself uses.
    This is a pretty odd circumstance, and certainly won’t arise much.

.

Silent Exception

(Rejeev Divakaran got that one).

I was using classBeingRedefined.getName() to print the class name.
classBeingRedefined is null when it is loaded first time.

The bottom line is if there is uncaught exception in transform method.
It will be silently eaten up.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!