|
| 1 | +package com.threedr3am.bug.compile.javac; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.net.MalformedURLException; |
| 6 | +import java.net.URI; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import java.net.URL; |
| 9 | +import java.net.URLClassLoader; |
| 10 | +import java.nio.charset.Charset; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Locale; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Random; |
| 18 | +import javax.tools.Diagnostic; |
| 19 | +import javax.tools.DiagnosticCollector; |
| 20 | +import javax.tools.JavaCompiler; |
| 21 | +import javax.tools.JavaCompiler.CompilationTask; |
| 22 | +import javax.tools.JavaFileObject; |
| 23 | +import javax.tools.SimpleJavaFileObject; |
| 24 | +import javax.tools.StandardJavaFileManager; |
| 25 | +import javax.tools.ToolProvider; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author threedr3am |
| 29 | + */ |
| 30 | +public class RuntimeMakeClass { |
| 31 | + |
| 32 | + //使用了自定义JavaFileObject实现的动态编译 |
| 33 | + public void e() throws IOException { |
| 34 | + JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
| 35 | + DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector(); |
| 36 | + StandardJavaFileManager standardJavaFileManager = javaCompiler |
| 37 | + .getStandardFileManager(diagnostics, Locale.CHINA, Charset.forName("utf-8")); |
| 38 | + int id = new Random().nextInt(10000000); |
| 39 | + StringBuilder stringBuilder = new StringBuilder() |
| 40 | + .append("import java.io.BufferedReader;\n") |
| 41 | + .append("import java.io.IOException;\n") |
| 42 | + .append("import java.io.InputStream;\n") |
| 43 | + .append("import java.io.InputStreamReader;\n") |
| 44 | + .append("public class Threedr3am" + id + " {\n") |
| 45 | + .append(" public static String result = \"\";\n") |
| 46 | + .append(" static {\n") |
| 47 | + .append(" try {") |
| 48 | + .append(" StringBuilder stringBuilder = new StringBuilder();\n") |
| 49 | + .append(" BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(\"" + "ls" + "\").getInputStream()));\n") |
| 50 | + .append(" String line;\n") |
| 51 | + .append(" while((line = bufferedReader.readLine()) != null) {\n") |
| 52 | + .append(" stringBuilder.append(line).append(\"\\n\");\n") |
| 53 | + .append(" }\n") |
| 54 | + .append(" result = stringBuilder.toString();\n") |
| 55 | + .append(" } catch (Exception e) {\n") |
| 56 | + .append(" e.printStackTrace();\n") |
| 57 | + .append(" }\n") |
| 58 | + .append(" }\n") |
| 59 | + .append("}"); |
| 60 | + String classPath = "/tmp/"; |
| 61 | + Files.write(Paths.get(classPath + "Threedr3am" +id + ".java"), stringBuilder.toString().getBytes()); |
| 62 | + Iterable fileObject = standardJavaFileManager.getJavaFileObjects(classPath + "Threedr3am" +id + ".java"); |
| 63 | + javaCompiler.getTask(null, standardJavaFileManager, diagnostics, null, null, fileObject).call(); |
| 64 | + try { |
| 65 | + System.out.println(new URLClassLoader(new URL[]{new URL("file:" + classPath)}).loadClass("Threedr3am" + id).getField("result").get(null)); |
| 66 | + } catch (ClassNotFoundException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } catch (NoSuchFieldException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } catch (IllegalAccessException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static void main(String[] args) throws IOException { |
| 76 | + new RuntimeMakeClass().e(); |
| 77 | + } |
| 78 | +} |
0 commit comments