如何避免'javassist.CannotCompileException:no method body'

时间:2014-04-10 15:35:14

标签: java instrumentation javassist

我正在使用Java工具和Javassist将print语句插入到方法中。这主要是没有错误的,但是对于某些类方法(例如java.util.TimeZone.getSystemTimeZoneID),我得到以下异常:

javassist.CannotCompileException: no method body
        at javassist.CtBehavior.insertBefore(CtBehavior.java:695)
        at javassist.CtBehavior.insertBefore(CtBehavior.java:685)

我的代码尝试通过检查CtBehaviour.isEmpty()来避免此问题,但这没有任何区别。关于如何避免这种情况的任何建议?

这是一个最小的例子:

public class ExceptionExample implements ClassFileTransformer {

  private static final ClassPool pool = ClassPool.getDefault();

  public static void premain(String agentArgument,
      Instrumentation instrumentation) {
    instrumentation.addTransformer(new ExceptionExample());
  }

  @Override
  public byte[] transform(final ClassLoader loader, final String className,
      final Class<?> classBeingRedefined,
      final ProtectionDomain protectionDomain, final byte[] classfileBuffer)
      throws IllegalClassFormatException {

    String dottedClassName = className.replace('/', '.');

    if (dottedClassName.startsWith("java.lang")
        || dottedClassName.startsWith("java.util")) {

      try {
        System.out.println("Instrumenting: " + dottedClassName);
        return adjustClass(dottedClassName, classBeingRedefined,
            classfileBuffer);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return classfileBuffer;
  }

  private byte[] adjustClass(final String className,
      final Class<?> classBeingRedefined, final byte[] classfileBuffer)
      throws IOException, RuntimeException, CannotCompileException {

    CtClass cl = null;

    try {
      cl = pool.makeClass(new java.io.ByteArrayInputStream(classfileBuffer));
      if (!cl.isInterface()) {
        CtBehavior[] methods = cl.getDeclaredBehaviors();
        for (CtBehavior method : methods) {
          if (!method.isEmpty()) {
            try {
              method
                  .insertBefore(String.format(
                      "System.out.println(\"CALLING: %s\");",
                      method.getLongName()));
            } catch (Throwable t) {
              System.out.println("Error instrumenting " + className + "."
                  + method.getName());
              t.printStackTrace();
            }
          }
        }
        return cl.toBytecode();
      }
    } finally {
      if (cl != null) {
        cl.detach();
      }
    }
    return classfileBuffer;
  }
}

这是我正在测试的一个小班:

public class Main {

  public static void main(String[] args) throws Exception {
    Calendar c = Calendar.getInstance();
    System.out.println(c.get(Calendar.YEAR));
  }
}

示例输出(缩写):

Instrumenting: java.util.Calendar
CALLING: java.util.Calendar.<clinit>()
CALLING: java.util.Calendar.getInstance()
Instrumenting: java.util.TimeZone
Error instrumenting java.util.TimeZone.getSystemTimeZoneID
javassist.CannotCompileException: no method body
        at javassist.CtBehavior.insertBefore(CtBehavior.java:695)
        at javassist.CtBehavior.insertBefore(CtBehavior.java:685)
        at com.cryptomathic.test.instrument.ExceptionExample.adjustClass(ExceptionExample.java:56)
        at com.cryptomathic.test.instrument.ExceptionExample.transform(ExceptionExample.java:34)
        at sun.instrument.TransformerManager.transform(Unknown Source)
        at sun.instrument.InstrumentationImpl.transform(Unknown Source)
        at java.util.Calendar.getInstance(Unknown Source)
        at test.Main.main(Main.java:8)
Error instrumenting java.util.TimeZone.getSystemGMTOffsetID
javassist.CannotCompileException: no method body
        at javassist.CtBehavior.insertBefore(CtBehavior.java:695)
        at javassist.CtBehavior.insertBefore(CtBehavior.java:685)
        at com.cryptomathic.test.instrument.ExceptionExample.adjustClass(ExceptionExample.java:56)
        at com.cryptomathic.test.instrument.ExceptionExample.transform(ExceptionExample.java:34)
        at sun.instrument.TransformerManager.transform(Unknown Source)
        at sun.instrument.InstrumentationImpl.transform(Unknown Source)
        at java.util.Calendar.getInstance(Unknown Source)
        at test.Main.main(Main.java:8)
CALLING: java.util.TimeZone.<clinit>()
CALLING: java.util.TimeZone.getDefaultRef()
CALLING: java.util.TimeZone.getDefaultInAppContext()
CALLING: java.util.TimeZone.setDefaultZone()
CALLING: java.util.TimeZone.getTimeZone(java.lang.String,boolean)
CALLING: java.util.TimeZone.setID(java.lang.String)
...

2 个答案:

答案 0 :(得分:4)

您需要首先检查它是否是本机方法,因为本机方法不支持这些方法(insertBefore(),insertAfter()..)。我写了一个小方法来检测CtMethod是否是原生的:

public static boolean isNative(CtMethod method) {
    return Modifier.isNative(method.getModifiers());
}

我测试了它,它可以解决你的问题。

注意:无法检测本机方法,因为它们没有字节码。但是,如果支持本机方法前缀(Transformer.isNativeMethodPrefixSupported()),则可以使用Transformer.setNativeMethodPrefix()将本机方法调用包装在非本机调用中,然后可以对其进行检测。

答案 1 :(得分:2)

TimeZone.getSystemTimeZoneID()是一种原生方法,我并不感到惊讶,这不起作用(我认为你根本无法做到这一点,但我还没有找到这个信息在Javassist doc中。) 要跳过本机方法,请使用Modifier.isNative()