java运行时创建或修改类-JavaCompiler

在Java中,你可以使用Java Compiler API动态地创建类,它可以用来编译Java源代码,这里记录下自己在实践过程中的一些知识点。

根据字符串动态编译(本地保存编译后的class文件)

// 1. StringJavaFileObject.java 创建StringJavaFileObject
import javax.tools.SimpleJavaFileObject;
import java.io.IOException;
import java.net.URI;

/**
 * StringJavaFileObject
 */
public class StringJavaFileObject extends SimpleJavaFileObject {
    private String name;

    private String content;

    public StrJavaFileObject(String name, String content) {
        super(URI.create("string:///" + name.replace(".", "/") + Kind.SOURCE.extension), Kind.SOURCE);
        this.content = content;
    }

    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
        return content;
    }
}

// 2. TestJavaCompiler.java 通过java compile api生成class
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;

/**
 * TestJavaCompiler
 */
public class TestJavaCompiler {
    private static final String className = "Test";
    private static final String methodName = "hello";
    private static final String code = "public class Test {\n" +
            "    public static void hello() {\n" +
            "        System.out.println(\"Hello World!\");\n" +
            "    }\n" +
            "}";

    public static void main(String[] args) throws Exception {
        // 获取当前平台编译器
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        // 获取java文件管理器,默认会将编译后的class保存到当前目录下
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

        // 创建需要编译的java文件对象
        StringJavaFileObject strJavaFileObject = new StringJavaFileObject(className, code);

        // 构建需要编译的java文件集合
        Iterable<? extends JavaFileObject> fileObjects = Collections.singletonList(strJavaFileObject);

        // 创建编译任务
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);

        // 执行编译任务
        Boolean result = task.call();

        // 判断当前编译任务是否执行成功
        if (result == null || !result) {
            throw new Exception("Compilation failed");
        }
        
        // Notice: 创建类加载器,并且设置类加载路径为当前目录
        URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{new File("./").toURI().toURL()});

        // 通过自定义类加载器加载编译后的类文件
        Class<?> cls = Class.forName(className, true, classLoader);

        // 通过反射创建类实例,需要无餐构造方法
        Object instance = cls.newInstance();

        // 通过反射回调hello方法
        cls.getMethod(methodName).invoke(instance);
    }
}

根据字符串动态编译(class文件直接保存在内存中)

// 1. ClassJavaFileObject.java 保存编译后的class字节码信息
import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

/**
 * ClassJavaFileObject
 */
public class ClassJavaFileObject extends SimpleJavaFileObject {
    private ByteArrayOutputStream outputStream;

    public ClassJavaFileObject(String className, Kind kind) {
        super(URI.create(className + kind.extension), kind);
        this.outputStream = new ByteArrayOutputStream();
    }

    public OutputStream openOutputStream() throws IOException {
        return this.outputStream;
    }

    public byte[] getBytes() throws IOException {
        return this.outputStream.toByteArray();
    }
}

// 2. MemoryClassLoader.java 自定义类加载器,支持byte加载
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * MemoryClassLoader
 */
public class MemoryClassLoader extends URLClassLoader {
    private Map<String, byte[]> classMap = new ConcurrentHashMap<>();
    public MemoryClassLoader(Map<String, byte[]> classData) {
        super(new URL[0], MemoryClassLoader.class.getClassLoader());
        classMap.putAll(classData);
    }

    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] buf = classMap.get(name);
        if (buf == null) {
            return super.findClass(name);
        }
        // 移除加载过的类,避免重复处理
        classMap.remove(name);
        
        // 返回类定义
        return defineClass(name,  buf,0, buf.length);
    }
}

// 3. TestJavaCompilerMemory.java 测试
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.util.Collections;
import java.util.HashMap;

/**
 * TestJavaCompilerMemory
 */
public class TestJavaCompilerMemory {
    private static final String className = "TestMemory";
    private static final String methodName = "hello";
    private static final String code = "public class TestMemory {\n" +
            "    public static void hello() {\n" +
            "        System.out.println(\"Hello TestMemory!\");\n" +
            "    }\n" +
            "}";

    public static void main(String[] args) throws Exception {
        // 获取当前平台编译器
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        // 获取java文件管理器,默认会将编译后的class保存到当前目录下
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

        // 自定义class文件管理器,用于在内存中保存字节码信息
        ClassJavaFileManager classJavaFileManager = new ClassJavaFileManager(fileManager);

        // 创建需要编译的java文件对象
        StringJavaFileObject strJavaFileObject = new StringJavaFileObject(className, code);

        // 构建需要编译的java文件集合
        Iterable<? extends JavaFileObject> fileObjects = Collections.singletonList(strJavaFileObject);

        // 创建编译任务
        JavaCompiler.CompilationTask task = compiler.getTask(null, classJavaFileManager, null, null, null, fileObjects);

        // 执行编译任务
        Boolean result = task.call();

        // 判断当前编译任务是否执行成功
        if (result == null || !result) {
            throw new Exception("Compilation failed");
        }

        // Notice: 构建class字节码-类名映射
        HashMap<String, byte[]> classMap = new HashMap<>();
        classMap.put(className, classJavaFileManager.getClsJavaFileObject().getBytes());
        
        // Notice: 创建自定义类加载器,将映射关系注入
        MemoryClassLoader classLoader = new MemoryClassLoader(classMap);

        // 通过自定义类加载器加载编译后的类文件
        Class<?> cls = Class.forName(className, true, classLoader);

        // 通过反射创建类实例,需要无餐构造方法
        Object instance = cls.newInstance();

        // 通过反射回调hello方法
        cls.getMethod(methodName).invoke(instance);
    }
}

根据字符串动态编译(class文件直接保存在内存中,并将class加载到指定classloader中)

// 3. TestJavaCompilerMemory.java 测试
import sun.misc.Unsafe;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.lang.reflect.Field;
import java.util.Collections;

/**
 * TestCp
 *
 * @author : dg
 * @version 1.0.0
 * @since : 2023/9/20
 */
public class TestJavaCompilerAppClassLoader {
    private static final String className = "TestAppClassLoader";
    private static final String methodName = "hello";
    private static final String code = "public class TestAppClassLoader {\n" +
            "    public static void hello() {\n" +
            "        System.out.println(\"Hello TestAppClassLoader!\");\n" +
            "    }\n" +
            "}";

    public static void main(String[] args) throws Exception {
        // 获取当前平台编译器
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        // 获取java文件管理器,默认会将编译后的class保存到当前目录下
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

        // 自定义class文件管理器,用于在内存中保存字节码信息
        ClassJavaFileManager classJavaFileManager = new ClassJavaFileManager(fileManager);

        // 创建需要编译的java文件对象
        StringJavaFileObject strJavaFileObject = new StringJavaFileObject(className, code);

        // 构建需要编译的java文件集合
        Iterable<? extends JavaFileObject> fileObjects = Collections.singletonList(strJavaFileObject);

        // 创建编译任务
        JavaCompiler.CompilationTask task = compiler.getTask(null, classJavaFileManager, null, null, null, fileObjects);

        // 执行编译任务
        Boolean result = task.call();

        // 判断当前编译任务是否执行成功
        if (result == null || !result) {
            throw new Exception("Compilation failed");
        }

        // Notice: 通过反射获取Unsafe实例,Unsafe只能在通过bootstrapClassLoader加载的类中直接获取
        Field field = Unsafe.class.getDeclaredField("theUnsafe");
        field.setAccessible(true);
        Unsafe unsafe = (Unsafe) field.get(null);

        // Notice: 获取编译后的字节码
        byte[] classByte = classJavaFileManager.getClsJavaFileObject().getBytes();

        // Notice: 获取当前应用类加载器
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();

        // Notice: 将该类动态加入应用类加载器中
        unsafe.defineClass(className, classByte, 0, classByte.length, classLoader, null);

        // 通过自定义类加载器加载编译后的类文件
        Class<?> cls = Class.forName(className, true, classLoader);

        // 通过反射创建类实例,需要无餐构造方法
        Object instance = cls.newInstance();

        // 通过反射回调hello方法
        cls.getMethod(methodName).invoke(instance);
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QuietJiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值