您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Java:如何将存储为byte []的Class加载到JVM中?

Java:如何将存储为byte []的Class加载到JVM中?

我现在在测试中实际上正在使用类似的方法,将一组类定义作为byte []给予ClassLoader:

  public static class ByteClassLoader extends urlclassloader {
    private final Map<String, byte[]> extraClassDefs;

    public ByteClassLoader(URL[] urls, ClassLoader parent, Map<String, byte[]> extraClassDefs) {
      super(urls, parent);
      this.extraClassDefs = new HashMap<String, byte[]>(extraClassDefs);
    }

    @Override
    protected Class<?> findClass(final String name) throws ClassNotFoundException {
      byte[] classBytes = this.extraClassDefs.remove(name);
      if (classBytes != null) {
        return defineClass(name, classBytes, 0, classBytes.length); 
      }
      return super.findClass(name);
    }

  }
java 2022/1/1 18:14:10 有636人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶