解析javac编译错误

解析javac编译错误,java,parsing,javac,Java,Parsing,Javac,我做了大量的调查,似乎找不到一个现有的库来解析javac编译错误。我正在考虑自己实现一个“javac错误解析器”,但我可以预见它可能不会是直接的(需要考虑很多位) 我不使用ant进行构建(有很多原因),但我可以像这样收集javac的输出: Something.java:1: error: Something is not abstract and does not override abstract method foo(boolean) in InterfaceSomething public

我做了大量的调查,似乎找不到一个现有的库来解析javac编译错误。我正在考虑自己实现一个“javac错误解析器”,但我可以预见它可能不会是直接的(需要考虑很多位)

我不使用ant进行构建(有很多原因),但我可以像这样收集javac的输出:

Something.java:1: error: Something is not abstract and does not override abstract method foo(boolean) in InterfaceSomething
public class Something implements InterfaceSomething{
       ^
Something.java:49: error: incompatible types: int cannot be converted to Something[]
        return baz;
               ^
Something.java:55: error: incompatible types: int cannot be converted to Something[]
        return baz;
               ^
Note: Something.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
理想情况下,我想要一个包含“错误”列表的对象。每个“错误”都有一个文件名、行号、描述和代码段

例如,在这种情况下,错误0将保持:

A String filename = "Something.java"
An int lineNum = 1
A String description = "Something is not abstract and....."
A String codeSnippet ="public class Something implements InterfaceSomething{"
在过去尝试过很多东西的解析后,我意识到这通常是一项相当大的工作,因为总会有一些输出略有不同或不符合预期的情况(因此我正在寻找一个库,其中有人花了相当多的时间考虑这些情况,以避免我重新发明轮子)

我找不到这个似乎很奇怪。有什么想法吗

编辑:重要-我不是要求任何人为我做这件事,我完全有能力自己去做,我只是想要任何关于已经存在的任何事情的想法或建议。我不想要包括代码在内的答案,但可能是建议此输出格式规则的答案,或者是指向我无法找到的某个现有库的答案。

我认为和类是您想要检查的内容

下面是我早期项目中的一些代码

public String compile(String source, Object... options) {
    String className = getMainClassName(source);

    if (className == null) {
        throw new WrongSourceStructureException("No public class which implements 'Solution'");
    }

    javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    if (compiler == null)
        throw new BadEnvironmentException("You should specify path to JDK in your JDK_HOME and JAVA_HOME");

    Writer err = new StringWriter();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    List<String> compilerOptions = new ArrayList<String>();

    String[] opts = new String[options.length - 1];
    System.arraycopy(options, 1, opts, 0, opts.length);

    if (options != null) {
        compilerOptions.addAll(Arrays.asList(opts));
    }

    compilerOptions.add("-d");
    compilerOptions.add("./tmp/classes/");

    JavaFileObject codeSolution = new JavaSourceFromString(className, source);

    Iterable<? extends JavaFileObject> units = Arrays.asList(codeSolution);
    javax.tools.JavaCompiler.CompilationTask task = compiler.getTask(err, null, diagnostics, compilerOptions, null, units);

    boolean success = task.call();

    if (!success) {
        StringBuilder errorMessages = new StringBuilder();
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            errorMessages.append(diagnostic.getMessage(null)).append("\n");
        }
        throw new NotCompiledException(errorMessages.toString());
    }

    String aPackage = getPackage(source);

    return ".tmp/classes/" + options[0] + aPackage.replace(".", "/") + "/" + className + ".class";
}
公共字符串编译(字符串源、对象…选项){
字符串className=getMainClassName(源);
if(className==null){
抛出新的ErrorSourceStructureException(“没有实现“解决方案”的公共类”);
}
javax.tools.JavaCompiler=ToolProvider.getSystemJavaCompiler();
if(编译器==null)
抛出新的BadEnvironmentException(“您应该在JDK_主页和JAVA_主页中指定JDK的路径”);
编写器错误=新建StringWriter();
DiagnosticCollector diagnostics=新建DiagnosticCollector();
列表编译器选项=新建ArrayList();
String[]opts=新字符串[options.length-1];
System.arraycopy(选项1,选项0,选项长度);
如果(选项!=null){
addAll(Arrays.asList(opts));
}
编译器选项。添加(“-d”);
compilerOptions.add(“./tmp/classes/”);
JavaFileObject codeSolution=新的JavaSourceFromString(类名称,源);

IterableI我在编译方面没有问题,这个问题是关于解析javac的输出。上面的编译错误是故意作为javac输出的一个例子,当有错误导致编译失败时。Something.java和interfacemething.java是由不存在的类组成的,就像baz和foo一样。(此评论是对另一条评论的回应,该评论现在似乎已被删除)我非常困惑这个问题如何被视为“过于宽泛”。它非常具体,我想要一些东西(最好是java)对于解析javac输出,它已经存在了吗?请一位亲密的投票者解释一下问题是什么?太棒了,谢谢。我知道在某处会有这样一个小宝石。我将阅读javadoc和示例,看看我能做些什么。我所需要的只是指向正确的方向。谢谢!这看起来非常奇怪:"如果提供了诊断侦听器,诊断将提供给侦听器。如果没有提供侦听器,诊断将以未指定的格式格式化,并写入默认输出,即System.err,除非另有规定。即使提供了诊断侦听器,某些诊断也可能不适合诊断侦听器nd将被写入默认输出。”@ThePerson,是的,您可能需要将自己的
PrintStream
设置为
System.err
System.out
。我已经用代码示例更新了答案。