java编程进行动态编译加载代码分享

时间:2021-10-08 22:59:17

简述

该类使用javax.tools.ToolProvider自带的JavaCompiler进行编译,使用IO的File及NIO的Files进行对应的路径创建、读取及拷贝,使用正则表达式进行包名与目录的转换,我只是将这些东西做了个容错整合,没什么技术含量,就为个方便吧。

模块API

?
1
2
3
4
class DynamicReactor://空参构造
public Class<?> dynamicCompile(String srcPath);//输入一个指定的源文件路径,若编译、拷贝成功则返回该类对应的Class类实例
private String changePacketToDic(String packageName);//将一个合法的包名转换为对应JavaClassPath中的路径(我是用的是eclipse 所以需要对应地增加bin这一目录,若使用其他不同编译器,请参考对应的运行上下文设置进行适当修改)
private String getPackage(String srcPath);//由一个合法的java文件路径尝试获得其包名

源代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
/**
 * DynamicReactor 一个动态编译模块,负责编译源文件,复制到对应包下及加载类等过程(JDK 1.7)
 * @author 三向板砖
 * */
public class DynamicReactor {
    JavaCompiler compiler;
    Pattern packagePattern;
    static final String regEx = "(?<=package\\s).*(?=;)";
    public DynamicReactor()
      {
        compiler = ToolProvider.getSystemJavaCompiler();
        packagePattern = Pattern.compile(regEx);
    }
    /**
   * 动态编译给定源文件
   * @param srcPath 源文件路径
   * @return Class
   *   <br>若成功返回对应类的Class实例
   *   <br>若失败返回null
   * */
    public Class<?> dynamicCompile(String srcPath)
      {
        Class<?> result = null;
        //获得给定路径源文件的
        String packName = getPackage(srcPath);
        if(packName == null)
            {
            System.out.println("DynamicRector:Load packageName Error!");
            return null;
        }
        //调用compiler编译指定源文件
        int res = compiler.run(null, null, null,srcPath);
        if(res != 0)
            {
            System.out.println("DynamicRector:Compile Java Source Error!");
            return null;
        }
        //获得包名对应的路径,若路径不存在则创建,若指定class文件存在则覆盖
        String packageDst = changePacketToDic(packName);
        File dstDir = new File(packageDst);
        if(!dstDir.exists())
            {
            dstDir.mkdirs();
        }
        Path pathFrom = Paths.get(srcPath.split("\\.java")[0] + ".class");
        Path pathTo = Paths.get(packageDst,pathFrom.getFileName().toString());
        try {
            Files.move(pathFrom, pathTo, StandardCopyOption.REPLACE_EXISTING);
        }
        catch (IOException e) {
            System.out.println("DynamicRector:Move File Fail!");
            e.printStackTrace();
        }
        try {
            result = Class.forName(packName+"."+pathFrom.getFileName().toString().split("\\.class")[0]);
        }
        catch (ClassNotFoundException e) {
            System.out.println("DynamicRector:Class Not found in Final!");
        }
        return result;
    }
    //该方法将一个合法包名转化为对应路径
    private String changePacketToDic(String packageName)
      {
        String[] dirs = packageName.split("\\.");
        String res = ".\\bin";
        for (int i = 0;i < dirs.length;i++)
            {
            res += "\\"+dirs[i];
        }
        return res;
    }
    //该方法从给定的路径源文件中获得包名
    private String getPackage(String srcPath)
      {
        String result = null;
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(srcPath));
            String data = br.readLine();
            while(data != null)
                  {
                if(data.indexOf("package") != -1)
                        {
                    Matcher m = packagePattern.matcher(data);
                    if(m.find())
                              {
                        result = m.group();
                    }
                    break;
                }
                data = br.readLine();
            }
            br.close();
        }
        catch (IOException e) {
            System.out.println("DynamicRector:Error in open file "+srcPath);
        }
        return result;
    }
}

总结

以上就是本文关于java编程进行动态编译加载代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/shuzhe66/article/details/26017217