如何避免“javassist.CannotCompileException:没有方法体”

Dun*_*nes 3 java instrumentation javassist

我正在使用 Java 检测和Javassist将打印语句插入到方法中。这通常不会出现错误,但对于某些类方法(例如java.util.TimeZone.getSystemTimeZoneID),我得到以下异常:

javassist.CannotCompileException: no method body
        at javassist.CtBehavior.insertBefore(CtBehavior.java:695)
        at javassist.CtBehavior.insertBefore(CtBehavior.java:685)
Run Code Online (Sandbox Code Playgroud)

我的代码尝试通过检查来避免此问题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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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

public class Main {

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

输出示例(缩写):

javassist.CannotCompileException: no method body
        at javassist.CtBehavior.insertBefore(CtBehavior.java:695)
        at javassist.CtBehavior.insertBefore(CtBehavior.java:685)
Run Code Online (Sandbox Code Playgroud)

Ang*_*rty 5

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

public static boolean isNative(CtMethod method) {
    return Modifier.isNative(method.getModifiers());
}
Run Code Online (Sandbox Code Playgroud)

我已经对其进行了测试,它有效并解决了您的问题。

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

  • [Halil](http://stackoverflow.com/users/1623342/halil)建议您也不能修改抽象方法。要识别抽象方法,请使用以下方法:`Modifier.isAbstract(method.getModifiers())`。 (2认同)