Is it possible to have the System ClassLoader load .class files specified at run time?

后端 未结 6 1017
[愿得一人]
[愿得一人] 2021-01-01 00:09

I am writing a static analysis tool for an assignment, it analyses Java bytecode using the ASM library. One of the parts of ASM that we use requires (or at least, appears to

6条回答
  •  情深已故
    2021-01-01 00:59

    I created my own ClassLoader its quite simple.

     /**
     * Used to hold the bytecode for the class to be loaded.
     */
    private final static ThreadLocal BYTE_CODE = new ThreadLocal();
    
    @Override
    protected Class findClass(final String name) throws ClassNotFoundException {
        final byte[] bytes = BYTE_CODE.get();
        if (null == bytes) {
            throw new ClassNotFoundException(name);
        }
        return this.defineClass(null, bytes, 0, bytes.length);
    }
    

提交回复
热议问题