- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Java 的 sun.misc.Unsafe.defineClass(String name, byte[] code, int off, int len, ClassLoader classLoader, ProtectionDomain protectionDomain) 动态加载类,但由于某种原因我创建的对象通过调用 Class.newInstance()(或 Class.getConstructor().newInstance())不是我在该类中实现的接口(interface)的实例(以及 Class.getInterfaces() 返回的接口(interface))。
我定义的加载给定字节码类的方法是这样的:
public static Class<?> loadClass(String name, URL url, byte[] code) {
Unsafe u = UnsafeKey.getUnsafe();
java.lang.ClassLoader loader = java.lang.ClassLoader.getSystemClassLoader();
Permissions perms=new Permissions();
perms.add(new RuntimePermission("accessDeclaredMembers"));
ProtectionDomain protection = new ProtectionDomain(new CodeSource(url,(Certificate[])null),perms);
return u.defineClass(name, code, 0, code.length, loader, protection);
}
在我的案例中,字节码是使用以下方法直接从 .class 文件(由我的 jdk 编译)加载的:
public static Class<?> loadClass(String name, File f) throws IOException {
byte[] data = new byte[(int) f.length()];
InputStream i = new FileInputStream(f);
i.read(data);
return loadClass(name, f.toURI().toURL(), data);
}
它将加载的数据发送到第一个方法。
我用于测试的类实现了一个名为 PixelArtist 的接口(interface):
import com.ralitski.art.api.PixelArtist;
public class PixelArtistTest implements PixelArtist {
public PixelArtistTest() {}
@Override
public int getWidth() {
return 100;
}
@Override
public int getHeight() {
return 100;
}
@Override
public int getColor(int x, int y) {
int z = x * y;
return x + (y * 255) + z;
}
}
从文件中加载此类、定义它并创建一个新实例后,我尝试通过此方法使用它(传递的此对象是通过调用 Class.newInstance() 创建的):
private static Artist getArtist(Object o) {
System.out.println(o);
Class c = o.getClass();
System.out.println(c);
System.out.println(c.getSuperclass());
for(Class c2 : c.getInterfaces()) {
System.out.println(" " + c2);
}
System.out.println(PixelArtist.class.isInstance(o));
if(o instanceof Artist) {
System.out.println(1);
return (Artist)o;
} else if(o instanceof PixelArtist) {
System.out.println(2);
return new PixelArtistFeed((PixelArtist)o);
} else {
System.out.println(3);
return null;
}
}
该代码的输出是这样的:
PixelArtistTest@1947c6b
class PixelArtistTest
class java.lang.Object
interface com.ralitski.art.api.PixelArtist
false
3
如您所见,该类将 PixelArtist 列为接口(interface),但从该类创建的对象不是 PixelArtist 类型的实例。
我尝试在定义类时向该类添加不同的权限(甚至是 AllPermission),并且我尝试使用 Class.getConstructor().newInstance() 实例化对象,但此错误仍然存在。
最佳答案
问题是您正在将类加载到系统类加载器中。该类加载器没有您的接口(interface),或者该接口(interface)的另一个副本具有不同的类加载器,很可能是您的类加载器。
我建议您改用默认的类加载器,例如与 PixelArtist.class.getClassLoader()
关于java - 使用 Java 的 Unsafe 在动态类中实现接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27462043/
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
在我正在开发的 crate 中,我有几个 unsafe 函数,由于某些原因它们被标记为 explained in this answer .在 unsafe 函数中,我可以执行 unsafe 操作,就
我一直在尝试在我的网络应用程序中设置文本编辑器。在哪里访问文本编辑器我必须允许 script-src 'self' 'unsafe-inline' 'unsafe-eval' 和 style-src
我是后端开发人员,正在帮助前端团队部署 Web 服务器,同时我正在研究遇到内容安全策略的漏洞,如果我将 CSP header 设置为“内容安全策略:默认源代码‘自我’数据: {own_domain_1
Unsafe.putAddress(long address, long x) 之间有什么区别?方法和Unsafe.putLong(long address, long x)方法? 最佳答案 Java
考虑以下代码: const defaultState = () => { return { profile: { id: '', displayName: '',
上面有一条评论 public native void unpark(对象线程); “取消阻塞在park上阻塞的给定线程,或者,如果它没有阻塞,则导致后续对park的调用不阻塞。注意:此操作“不安全”,
我已将 unsafe.cpp 和 Unsafe.java 克隆到自定义版本。我要构建新的 JVM,但似乎我的 UnsafeNew.java 内联了 unsafe.cpp 方法,而不是新的 unsafe
为什么会出现以下错误? Unsafe code may only appear if compiling with /unsafe"? 我使用 C# 和 Visual Studio 2008 在 Wi
我正在用 C# 做一个项目,它可以从线性代数包中获益。我看过外面的那些,但我真的不想付钱,或者我发现它们不是很好。所以我决定自己写。 我读到 C++ 数组比 C# 数组快得多,但在 C# 中使用指针数
在 Java 中,java.lang.unsafe 包中有 Unsafe 类,它提供对操作的低级访问。 现在在我看来,JVM 需要支持Unsafe 类中可用的所有 方法,以便与 JLS 兼容,示例方法
我正在尝试将选项卡存储在本地存储中并在刷新页面上获取前面的选项卡,数据存储在本地但是在控制台上的时候我得到错误 Error: [$sce:unsafe] Attempting to use an un
导入类型化函数时,我收到 no-unsafe-call 和 no-unsafe-assignment eslint 错误。如果函数在同一个文件中声明,错误就会消失。 eslint 似乎无法获取导入函数
我正在尝试在 Visual Studio App Center 中构建 Xamarin iOS 应用程序。该解决方案包含两个项目。一个是 Xamarin iOS 项目。另一个是绑定(bind)库项目。
Unsafe Fileupload 1.client check 标题叫客户端check,文件校验应该是在客户端进行的。 可以先把一句话木马改成图片格式,然后再抓包修改回PHP格式。 一句话木马内容:
我想问一下第一个例子是否比第二个例子慢。 例子1:for, unsafe, unsafe, unsafe, etc for (var i = 0; i ' { } // end of class .
我尝试通过在 Controller 中生成链接将书签按钮添加到我的网站。 模板部分: + Add Controller 部分: $scope.getCode = fun
我注意到在 Java 7 中,集合类(在我的例子中是 ConcurrentLinkedQueue)使用 UNSAFE 类进行交换和查找操作。 偏移量似乎是在编译时声明中计算的: itemOffset
我有一张图片: 在 html 上我得到的结果是: unsafe:c:/var/vci/images/fleetImages/IMG_20150912_091552.jpg 我已经在app.js中添加
这是实现原生pinvok代码的类 虽然我无法验证它是否正确使用了 unsafe,但它似乎可以工作 不安全的签名 struct IO_COUNTERS { public
我是一名优秀的程序员,十分优秀!