feat():测试
parent
25f17a2e83
commit
7df082388a
|
@ -1,6 +1,8 @@
|
||||||
package com.muyu.compile;
|
package com.muyu.compile;
|
||||||
|
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
|
||||||
import javax.tools.*;
|
import javax.tools.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -16,22 +18,40 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class OSSFileCompile{
|
public class OSSFileCompile{
|
||||||
|
|
||||||
public static Map<String, byte[]> compile(String fileName, String source,String extLib) throws IOException {
|
// public static Map<String, byte[]> compile(String fileName, String source,String extLib) throws IOException {
|
||||||
System.out.println("-------");
|
// System.out.println("-------");
|
||||||
|
// JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
|
// StandardJavaFileManager stdManager = compiler.getStandardFileManager(null, null, null);
|
||||||
|
// try (MemoryJavaFileManager manager = new MemoryJavaFileManager(stdManager)) {
|
||||||
|
// JavaFileObject javaFileObject = manager.makeStringSource(fileName, source);
|
||||||
|
// // 传入诊断监听器 size和传入的javaObject相同
|
||||||
|
// DiagnosticCollector diagnosticCollector = new DiagnosticCollector();
|
||||||
|
// List<String> optionList = Arrays.asList("-extdirs",extLib);
|
||||||
|
// JavaCompiler.CompilationTask task = compiler.getTask(null, manager,diagnosticCollector, optionList, null, Arrays.asList(javaFileObject));
|
||||||
|
// Boolean result = task.call();
|
||||||
|
// if (result == null || !result.booleanValue()) {
|
||||||
|
// throw new RuntimeException("Compilation failed.");
|
||||||
|
// }
|
||||||
|
// return manager.getClassBytes();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
public static Result<Object> compile(String source,String extLib){
|
||||||
|
// 获取系统Java编译器
|
||||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
StandardJavaFileManager stdManager = compiler.getStandardFileManager(null, null, null);
|
|
||||||
try (MemoryJavaFileManager manager = new MemoryJavaFileManager(stdManager)) {
|
// 调用编译器的compile方法编译源代码,可以添加编译选项和输出目录等
|
||||||
JavaFileObject javaFileObject = manager.makeStringSource(fileName, source);
|
int result = compiler.run(null, null, null, source);
|
||||||
// 传入诊断监听器 size和传入的javaObject相同
|
|
||||||
DiagnosticCollector diagnosticCollector = new DiagnosticCollector();
|
// 检查编译结果
|
||||||
List<String> optionList = Arrays.asList("-extdirs",extLib);
|
if (result == 0) {
|
||||||
JavaCompiler.CompilationTask task = compiler.getTask(null, manager,diagnosticCollector, optionList, null, Arrays.asList(javaFileObject));
|
System.out.println("编译成功,生成的.class文件位于源代码同目录");
|
||||||
Boolean result = task.call();
|
Result.success("编译成功,生成的.class文件位于源代码同目录");
|
||||||
if (result == null || !result.booleanValue()) {
|
} else {
|
||||||
throw new RuntimeException("Compilation failed.");
|
System.out.println("编译失败");
|
||||||
}
|
Result.error("编译失败");
|
||||||
return manager.getClassBytes();
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,8 +61,9 @@ public class OSSFileLoad {
|
||||||
// 关闭oss
|
// 关闭oss
|
||||||
ossClient.shutdown();
|
ossClient.shutdown();
|
||||||
|
|
||||||
Map<String, byte[]> compile = OSSFileCompile.compile(fileName, source, "target/");
|
// Map<String, byte[]> compile = OSSFileCompile.compile(fileName, source, "target/");
|
||||||
compile.forEach((key, value) -> {});
|
Result<Object> compile = OSSFileCompile.compile(source, "target/");
|
||||||
|
System.out.println(compile.getData());
|
||||||
return Result.success(source);
|
return Result.success(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue