- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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)
...
最佳答案
您需要首先检查它是否是 native 方法,因为 native 方法不支持这些方法(insertBefore()、insertAfter()..)。我写了一个小方法来检测 CtMethod 是否是原生的:
public static boolean isNative(CtMethod method) {
return Modifier.isNative(method.getModifiers());
}
我已经测试过它,它有效并解决了您的问题。
注意:无法检测 native 方法,因为它们没有字节码。但是,如果支持 native 方法前缀 ( Transformer.isNativeMethodPrefixSupported() ),那么您可以使用 Transformer.setNativeMethodPrefix() 将 native 方法调用包装在非 native 调用中,然后可以对其进行检测。
关于java - 如何避免 'javassist.CannotCompileException: no method body',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22992546/
Maven Artifact 之间有什么区别 javassist:javassist 和 org.javassist:javassist 在我的项目中,我的第一个依赖项是 3.8 版本,而我引入的第二
我有一个 CtMethod实例,但我不知道如何从中获取参数(不是类型)的名称。我试过 getParameterTypes ,但它似乎只返回类型。 我假设这是可能的,因为我使用的库没有源,只有类文件,我
可以使用 CtMethod.setBody("..") 设置方法主体,但我没有找到任何 API 来获取字符串格式的方法主体。 最佳答案 这不可能。 Javassist 不是反编译器。类文件中的方法以
可以使用CtMethod.setBody("..")设置方法体,但我没有找到任何API来获取字符串格式的方法体。 最佳答案 这是不可能的。 Javassist 不是反编译器。类文件中的方法用 Java
当使用 javassist 检测 com.sun.net.* 类时,我可以成功使用 .insertAfter 方法。但是,当尝试从 Glassfish 中运行的应用程序检测第三方类时,我收到错误: F
我正在使用 javassist 来更改方法体。当该方法在应用程序中定义时我可以做到这一点。但是,当我想更改应用程序使用的 jar 文件中定义的方法时,我会收到以下运行时错误: javassist.Ca
我在模型上使用 javassist:com.project.model.Model我已经尝试过各种组合如何为 ClassPath 和 CtClass 格式化字符串,但无济于事。 ClassPo
我正在处理 SpringMVC、Hibernate 和 JSON,但出现此错误。 HTTP Status 500 - Could not write JSON: No serializer found
我正在使用 javassist 创建一个类并为其添加注释。当我使用 CtClass.writeFile 并且我看到带有 Java 反编译器的类文件时,注释就在那里,但是当我使用 class.getAn
我想读取一个方法的返回值,并且我必须将其传递给使用 method.insertAfter 插入的代码。 示例: public String sayHello(){ return "1"; }
这是原始方法: @GET @Produces({"application/json"}) public Response getTermClouds(@Context SecurityCo
我正在尝试在应用程序启动期间使用附加功能来增强一些代码。整个设置本身工作正常,但有一点我认为 javassist 可能会生成错误的代码。 我正在特定类的特定方法上执行此操作,我之前检查过返回值实际上是
我正在编写一些 Javassist 代码来拦截方法调用并用代理替换它们。为此,我使用 ExprEditor 按以下方式替换调用: public static void main(String[] ar
我目前正在实现一个注释,强制字段通过 javassist 遵守条件。我想检查一个字段在读取时是否已初始化...因此,目前,我通过在虚拟机通过 Translator.onLoad(ClassPool p
我正在尝试使用 javassist 在编译时插入一些代码片段 环境 Java 8 Spring 启动 2.2.6 hibernate 5.4.12.Final Javasist nl.topicu
我对 javassist 有点问题,用方法处理程序装饰类。问题是方法处理程序与 Abc 类中的 method1 一起正常工作,但不会拦截对 Def 内部类中的 method2 的调用。 public
我将代码注入(inject)到 doGet 方法中 val replace = "" + " System.out.println(\"[before] " + className + "\")
我试图捕获分支和循环语句的条件表达式中涉及的变量值,例如: if (a + b < c - 5) { // here, capture value of a, b and c // if bo
我正在开发一个需要类检测的项目。我使用 javassist 因为在我的情况下进行检测非常方便。 我遇到了一个问题,可以使用以下代码片段进行描述: 假设 1 类: public class Class1
我试图在 java 类文件中找到类初始值设定项。我可以找到该方法,但如果在类文件中找不到 main,那么我希望它找到类初始值设定项并在那里注入(inject)代码。 如何使用 Javassist 找到
我是一名优秀的程序员,十分优秀!