使用 jdk 的 JavaCompiler 遇到编译错误

问题描述

使用 jdk 的 JavaCompiler 时, 遇到注释处理器问题, 不知道去哪里加载 lombok 的处理器

问题出现的环境背景及自己尝试过哪些方法

无从下手, 暂时没有尝试过其它方法

我如果把 location 的 SOURCE_PATH 去掉, lombok 的处理器是加载成功的, 只会把一个类找不到

相关代码

@Test

public void test3() throws IOException {

String classpath = "";

final List<File> classpathFiles = Arrays.stream(classpath.split(";")).map(File::new).collect(Collectors.toList());

final List<File> lombok = classpathFiles.stream().filter(classpathFile -> classpathFile.getName().contains("lombok")).collect(Collectors.toList());

List<File> sources = Arrays.asList(new File("d:/test/sanri-tools-maven/src/main/java"));

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

final StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);

standardFileManager.setLocation(StandardLocation.SOURCE_PATH,sources);

standardFileManager.setLocation(StandardLocation.SOURCE_OUTPUT,Arrays.asList(new File("d:/test/generatejava")));

standardFileManager.setLocation(StandardLocation.CLASS_PATH,classpathFiles);

standardFileManager.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,lombok);

standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT,Arrays.asList(new File("d:/test")));

final Iterable<? extends JavaFileObject> javaFileObjects = standardFileManager.getJavaFileObjects(new File("d:/test/sanri-tools-maven/src/main/java/com/sanri/service/xxservice.java"));

List<String> options = Arrays.asList("-encoding", "utf-8","-source","1.8");

JavaCompiler.CompilationTask task = compiler.getTask(null, standardFileManager, null, options, null, javaFileObjects);

final Boolean call = task.call();

standardFileManager.close();

assert call ;

}

你期待的结果是什么?实际看到的错误信息又是什么?

期待编译成功, 能够使用 lombok 编译

报错信息如下, 基本上是 get set 错误, 即 lombok 没有加载成功


回答:

解决了, classpath 中不能包含本身项目的包, 如果使用源码目录的话


回答:

参考这里

import com.sun.tools.javac.processing.PrintingProcessor;

import fi.jumi.actors.generator.JavaSourceFromString;

import org.junit.*;

import org.junit.rules.TemporaryFolder;

import javax.annotation.processing.Processor;

import javax.tools.*;

import javax.tools.JavaCompiler.CompilationTask;

import java.io.IOException;

import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;

import static org.hamcrest.Matchers.is;

public class ExampleTest {

@Rule

public final TemporaryFolder tempDir = new TemporaryFolder();

@Test

public void example() throws IOException {

JavaFileObject src = new JavaSourceFromString(

"com.example.GuineaPig",

"package com.example;\n" +

"public interface GuineaPig {\n" +

" void foo();\n" +

"}"

);

compile(new PrintingProcessor(), src);

}

private void compile(Processor processor, JavaFileObject... compilationUnits) throws IOException {

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tempDir.getRoot()));

CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, Arrays.asList(compilationUnits));

task.setProcessors(Arrays.asList(

processor

));

boolean success = task.call();

for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {

System.err.println(diagnostic);

}

assertThat("compile succeeded", success, is(true));

}

}

以上是 使用 jdk 的 JavaCompiler 遇到编译错误 的全部内容, 来源链接: utcz.com/p/944466.html

回到顶部