英文:
Agent from a static nested class
问题
为什么无法从静态嵌套类中创建代理?
我没有收到任何错误信息,程序也没有运行,所以很难说出了什么问题。
package sfjl;
import java.lang.instrument.Instrumentation;
public final class SFJL_Profiler {
private SFJL_Profiler() {}
static public final class SFJL_Profiler_Agent {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long sizeof(Object o) {
return instrumentation.getObjectSize(o);
}
}
static public final class SFJL_Profiler_Console_Printer {
}
}
清单文件:
Manifest-Version: 1.0
Premain-Class: sfjl.SFJL_Profiler.SFJL_Profiler_Agent
Agent-Class: sfjl.SFJL_Profiler.SFJL_Profiler_Agent
Can-Redefine-Classes: true
Can-Retransform-Classes: true
这是一个可正常工作的非嵌套代理:
https://stackoverflow.com/a/43296164/1022707
英文:
Why is not possible to create a agent from a static nested class?
I don't get an error or anything, the program just doesn't run, so it's hard to say what goes wrong.
package sfjl;
import java.lang.instrument.Instrumentation;
public final class SFJL_Profiler {
private SFJL_Profiler() {}
//
static public final class SFJL_Profiler_Agent {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long sizeof(Object o) {
return instrumentation.getObjectSize(o);
}
}
//
static public final class SFJL_Profiler_Console_Printer {
}
}
manifest:
Manifest-Version: 1.0
Premain-Class: sfjl.SFJL_Profiler.SFJL_Profiler_Agent
Agent-Class: sfjl.SFJL_Profiler.SFJL_Profiler_Agent
Can-Redefine-Classes: true
Can-Retransform-Classes: true
This is a non nested agent that is working:
答案1
得分: 1
因为它是一个嵌套类,分隔符是 $ 而不是 .
sfjl.SFJL_Profiler$SFJL_Profiler_Agent
英文:
Since it is a nested class, the separator is $ not .
sfjl.SFJL_Profiler$SFJL_Profiler_Agent
答案2
得分: 1
这段代码的翻译如下:
它正常工作,但是`Agent-Class`的值(以及同样的`Main-Class`和`Premain-Class`)是以JVM格式表示的。因此,请尝试:
Agent-Class: sfjl.SFJL_Profiler$SFJL_Profiler_Agent
在JVM语法中,$符号用于分隔内部类,而不是使用点。
英文:
It works fine, but the value of the Agent-Class
(and for that matter, Main-Class
and Premain-Class
too) are in JVM format. So, try:
Agent-Class: sfjl.SFJL_Profiler$SFJL_Profiler_Agent
$ separates inner classes, not dots, in JVM syntax.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论