java 热编译,热加载

一. 热编译

package com.lucain.dynamic;

import sun.applet.AppletClassLoader;
import sun.misc.Launcher;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Created by luxian.zhang on 2017/7/27.
 */
public class Test {
    /**
     * 创建java文件
     * @author Lucian
     */
    public static File generateJava() throws Exception{
        // 1.创建需要动态编译的代码字符串
        String nr = "\r\n"; //回车
        String source = "package com.lucian; " + nr +
                " public class  Hello{" + nr +
                " public void hello(){" + nr +
                " System.out.println(\"HelloWorld! 1\");" + nr +
                " }" + nr +
                " }";
        File dir = new File(CustomizationClassLoader.sourcePath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        File file = new File(dir,"Hello.java");
        Writer writer = new FileWriter(file);
        try {
            writer.write(source);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.flush();
            writer.close();
        }
        return file;
    }

    /**
     * 编译java文件
     */
    public static void compilerJava(File file) throws Exception{
        // 取得当前系统的编译器
        JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
        //获取一个文件管理器
        StandardJavaFileManager javaFileManager = javaCompiler.getStandardFileManager(null, null, null);
        try {
            //文件管理器与文件连接起来
            Iterable it = javaFileManager.getJavaFileObjects(file);
            File dir = new File(CustomizationClassLoader.classPath);
            if(!dir.exists()){
                dir.mkdirs();
            }
            //创建编译任务
            JavaCompiler.CompilationTask  task = javaCompiler.getTask(null, javaFileManager, null, Arrays.asList("-d", CustomizationClassLoader.classPath), null, it);
            //执行编译
            task.call();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            javaFileManager.close();
        }
    }

    public static void main(String[] args) throws Exception{
        compilerJava(generateJava());
        CustomizationClassLoader classLoader = new CustomizationClassLoader();
        Class cl = classLoader.loadClass("com.lucian.Hello");
        Object obj = cl.newInstance();

        Method helloMethod = cl.getMethod("hello", null);
        helloMethod.invoke(obj, null);
    }
}

二. 热加载

自定义类加载器,实现热加载

package com.lucain.dynamic;

import java.io.FileInputStream;

/**
 * Created by luxian.zhang on 2017/7/27.
 */
public class CustomizationClassLoader extends ClassLoader{
    public static String classPath;
    public static String sourcePath;
    static {
        classPath = System.getProperty("user.dir") + "/temp/class";
        sourcePath = System.getProperty("user.dir")+"/temp/source";
    }
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte[] data = loadByte(name);
            return defineClass(name, data, 0, data.length);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ClassNotFoundException();
        }
    }

    /**
     * 加载class文件,转换为字节数组
     */
    private byte[] loadByte(String name) throws Exception {
        name = name.replaceAll("\\.", "/");
        FileInputStream fis = new FileInputStream(classPath + "/" + name
                + ".class");
        int len = fis.available();
        byte[] data = new byte[len];
        fis.read(data);
        fis.close();
        return data;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值