我在 IntelliJ Idea 中工作,使用 JDK 和 JRE 8。该项目使用 lombok,我想进行代码生成和运行时编译。我的目标是生成与某些复杂的运行时生成的 lambda 函数执行相同任务的代码,以加快评估速度。该项目大量使用了lombok。而且我的问题与此处讨论的问题相同,因此我知道解决此问题的一种方法是添加特定的依赖项。
我的问题是:在没有编辑的情况下,是否有任何其他更简洁的方法可以解决这个问题pom.xml
?因为我在一个团队开发的大型项目中,我不想在这样的基础上进行干预。
现在,更具体地说,我的代码看起来就像在类似示例中找到的运行时编译的常用代码:
//Create class code
StringBuilder sb = new StringBuilder();
sb.append("package foo{\n");
sb.append(" public class bar{\n");
sb.append(" public Function<Object,Object> tee {return x -> x;}\n");
sb.append(" }\n");
sb.append("}");
String code = sb.toString();
//Creating a temporary file
File flie = File.createTempFile("bar", ".java");
Writer writer = new Filewriter(file);
writer.write(code);
writer.flush();
writer.close();
//Creating compilation environment
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUtils =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(file));
JavaCompiler.CompilationTask task = compiler.getTask(
null, fileManager, diagnostics, null, null, compilationUtils);
//Compiling the file
boolean success = task.call();
//Trying to extract the classes
Class clazz = Class.forName("bar");
为了便于阅读,我压制了所有try
的 s 和s。catch
现在,当我运行代码时,在 operator 上引发了异常 Class clazz = Class.forName("bar);
。同时,diagnostics
变量显示以下消息:
C:\Users\Kolya\AppData\Local\Temp\bar4319793429862999020.java:6: warning: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment
class bar
^
at lombok.javac.apt.LombokProcessor.init(LombokProcessor.java:83)
at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)
at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:140)
at lombok.launch.AnnotationProcessorHider$AnnotationProcessor.init(AnnotationProcessor.java:69)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:500)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
at com.megasoft.superproj.hyperpackage.AwesomeCodeGen.generateAwesomeCode(AwesomeCodeGen.java:100500)
...
将感谢任何建议。