gpt4 book ai didi

Java 仪器

转载 作者:行者123 更新时间:2023-12-01 15:13:07 26 4
gpt4 key购买 nike

我有一个 javaagent,它可以打印出所有已加载类的名称及其来源(它们来自哪里)。

public byte[] transform(ClassLoader loader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) throws IllegalClassFormatException {
System.out.print("Loading class: " + className + "\t");
if (domain != null) {
final CodeSource cs = domain.getCodeSource();
if (cs != null) {
System.out.println(cs.getLocation());
}
}
System.out.println();
return bytes;
}

对于某些类,它会打印出“null”(意味着 cs.getLocation() 为 null)。为什么会这样?有办法查看这些类来自哪里吗?请注意,我不是在自己的 Java 应用程序上执行此操作,因此我不知道它使用的任何自定义类加载器。

最佳答案

根据Jetty 7.0.0RC4 ,似乎 null CodeSource 反射(reflect)该类具有全局权限。

// 1) if protection domain codesource is null, it is the global permissions (grant {})

此行为记录在 ClassLoader.defineClass 的规范中,所以它不是任意的;-)

This method assigns a default ProtectionDomain to the newly defined class. The ProtectionDomain is effectively granted the same set of permissions returned when Policy.getPolicy().getPermissions(new CodeSource(null, null)) is invoked. The default domain is created on the first invocation of defineClass, and re-used on subsequent invocations.

<小时/>

其实仔细一看,我相信这是SecureClassLoader.defineClass的作品。 ...

If a non-null CodeSource is supplied a ProtectionDomain is constructed and associated with the class being defined.

关于Java 仪器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12030013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com