我是java代理的新手。我创建了一个简单的HotswapAgent类(从Play嗅探!框架):
public class HotswapAgent {
static Instrumentation instrumentation;
public static boolean enabled = false;
public static void premain(String agentArgs, Instrumentation instrumentation)
{
HotswapAgent.instrumentation = instrumentation;
HotswapAgent.enabled = true;
}
public static void reload(ClassDefinition... definitions)
throws UnmodifiableClassException, ClassNotFoundException
{
instrumentation.redefineClasses(definitions);
}
} 有了这个清单:
Manifest-Version: 1.0
Premain-Class: path.to.HotswapAgent
Can-Redefine-Classes: true我尝试以这种方式重新加载一个新的类定义:
CtClass modelClass = ....
...
byte [] bcode = modelClass.toBytecode();
Class c = modelClass.toClass();
modelClass.defrost();
ClassDefinition cdef = new ClassDefinition(c, bcode);
HotswapAgent.reload(cdef);所有这些类都在一个jar中,最后我得到了这个错误(在调用reload()时):
redefineClasses is not supported in this environment但是在Manifest中声明为Can-Redefine-Classes: true。
JVM是标准的MacOS X Java1.6VM。这个JVM可以很好地与使用相同代理机制的JRebel一起工作。
怎么了?
发布于 2012-10-15 17:05:35
根据documentation的说法
Optional Functionality: might not be implemented for all virtual machines. The following capability (as returned by GetCapabilities) must be true to use this function.您可以尝试使用addCapability来检查清单声明是否有问题。
这是一个example of addCapability in runtime。
https://stackoverflow.com/questions/12844186
复制相似问题