feat():测试
parent
25f17a2e83
commit
7df082388a
|
@ -1,6 +1,8 @@
|
|||
package com.muyu.compile;
|
||||
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
|
||||
import javax.tools.*;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
@ -16,22 +18,40 @@ import java.util.Map;
|
|||
*/
|
||||
public class OSSFileCompile{
|
||||
|
||||
public static Map<String, byte[]> compile(String fileName, String source,String extLib) throws IOException {
|
||||
System.out.println("-------");
|
||||
// public static Map<String, byte[]> compile(String fileName, String source,String extLib) throws IOException {
|
||||
// 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();
|
||||
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();
|
||||
|
||||
// 调用编译器的compile方法编译源代码,可以添加编译选项和输出目录等
|
||||
int result = compiler.run(null, null, null, source);
|
||||
|
||||
// 检查编译结果
|
||||
if (result == 0) {
|
||||
System.out.println("编译成功,生成的.class文件位于源代码同目录");
|
||||
Result.success("编译成功,生成的.class文件位于源代码同目录");
|
||||
} else {
|
||||
System.out.println("编译失败");
|
||||
Result.error("编译失败");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -61,8 +61,9 @@ public class OSSFileLoad {
|
|||
// 关闭oss
|
||||
ossClient.shutdown();
|
||||
|
||||
Map<String, byte[]> compile = OSSFileCompile.compile(fileName, source, "target/");
|
||||
compile.forEach((key, value) -> {});
|
||||
// Map<String, byte[]> compile = OSSFileCompile.compile(fileName, source, "target/");
|
||||
Result<Object> compile = OSSFileCompile.compile(source, "target/");
|
||||
System.out.println(compile.getData());
|
||||
return Result.success(source);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue