可以使用java工具在运行时更改静态方法吗?
我需要更改第三方库中的静态方法。我已经转换了实例方法,但不确定同样的事情是否适用于静态方法可以使用java工具在运行时更改静态方法吗?,java,instrumentation,Java,Instrumentation,我需要更改第三方库中的静态方法。我已经转换了实例方法,但不确定同样的事情是否适用于静态方法 @Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws Il
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException {
}
我在MyClass.java中有几个静态方法,称为myMethod。我需要在运行时更改myMethod的实现。
基本上,我扩展了ClassFileTransformer并重写了byte[]转换方法,类似于bellow。这对实例方法有效,但我不确定是否可以对静态方法也有效
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException {
}
@覆盖
公共字节[]转换(类加载器、字符串类名、类被重新定义、,
ProtectionDomain ProtectionDomain,字节[]classfileBuffer)
抛出IllegalClassFormatException{
}
是的,当然这是可能的。您可以更改整个类文件。对于大多数VM,您只能更改方法的实现,而不能更改其签名。方法是否是静态的并不重要