问题
I am working in IntelliJ Idea, with JDK and JRE 8. The project makes use of lombok, and I would like to have code generation and runtime compiling. My aim is to produce a code that does the same task as some complicated runtime generated lambda function, to speed up the evaluation. The project makes heavy use of lombok. And I have the same issue as the one discussed here, so I know that one way around this problem is to add particular dependencies.
My question is: did there appear any other, cleaner ways around this issue, without, say, editing pom.xml
? For I am in a large project developed by a team, and I don't want to interfere on such a ground level.
Now, more specifically, my code looks as a usual code for runtime compilation one finds in examples like that one:
//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");
I suppress all try
s and catch
s for readability. Now, when I run the code, an exception is thrown on the operator Class clazz = Class.forName("bar);
. Meanwhile, the diagnostics
variable shows the following message:
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)
...
Will be thankful for any suggestions.
来源:https://stackoverflow.com/questions/46904774/revisiting-the-conflict-of-lombok-with-java-runtime-compilation