如何用程序验证Java代码



给定源代码和Java版本,我需要能够验证代码是否会编译。如果代码没有编译,我需要能够返回源代码中的错误。

以下解决方案有效,但仅适用于您机器上当前使用的Java版本。

import javax.tools.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class Validator {
private final JavaCompiler COMPILER = ToolProvider.getSystemJavaCompiler();

//Assume srcFiles are java files that can be read
public final boolean compiles(Set<File> srcFiles) throws IOException {
//Convert files to JavaCompiler API compatible files
List<JavaFileObject> compilationUnits = new ArrayList<>();
for (File file : srcFiles) {
CompilableFile compilableFile = new CompilableFile(file.getName(), Files.readString(file.toPath()));
compilationUnits.add(compilableFile);
}
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
JavaCompiler.CompilationTask task = COMPILER.getTask(null, null, diagnosticCollector, null, null, compilationUnits);
boolean success = task.call();
//Displaying the info of each warning, error, etc
diagnosticCollector.getDiagnostics().forEach(Validator::printDiagnostic);
return success;
}
private static void printDiagnostic(Diagnostic<?> diagnostic) {
System.out.println(diagnostic.getCode());
System.out.println(diagnostic.getKind());
System.out.println(diagnostic.getPosition());
System.out.println(diagnostic.getStartPosition());
System.out.println(diagnostic.getEndPosition());
System.out.println(diagnostic.getSource());
System.out.println(diagnostic.getMessage(null));
}
/**
* Instances of this class can be compiled with the JavaCompiler API
*/
private static final class CompilableFile extends SimpleJavaFileObject {
final String code;
CompilableFile(String name, String code) {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
}

我是否可以实现以下功能?

public final boolean compiles(Set<File> srcFiles, SourceVersion version) {...}

此解决方案的版本范围似乎是编译器特定的,但对于OpenJDK 11和15,我注意到此解决方案适用于[7,SYSTEM_compiler_version]

JavaCompiler API允许您在调用方法JavaCompiler.CompilationTask::getTask时将命令行选项作为Iterable传递,因此您可以传递List.of("--release", "<version>"),其中<version>被正在验证的版本所替换

考虑到这一点,解决方案就变成了

import javax.tools.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class Validator {
private final JavaCompiler COMPILER = ToolProvider.getSystemJavaCompiler();

//Assume srcFiles are java files that can be read
public final boolean compiles(Set<File> srcFiles, String javaVersion) throws IOException {
//Convert files to JavaCompiler API compatible files
List<JavaFileObject> compilationUnits = new ArrayList<>();
for (File file : srcFiles) {
CompilableFile compilableFile = new CompilableFile(file.getName(), Files.readString(file.toPath()));
compilationUnits.add(compilableFile);
}
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
JavaCompiler.CompilationTask task = COMPILER.getTask(null, null, diagnosticCollector, List.of("--release", javaVersion), null, compilationUnits);
boolean success = task.call();
//Displaying the info of each warning, error, etc
diagnosticCollector.getDiagnostics().forEach(Validator::printDiagnostic);
return success;
}
private static void printDiagnostic(Diagnostic<?> diagnostic) {
System.out.println(diagnostic.getCode());
System.out.println(diagnostic.getKind());
System.out.println(diagnostic.getPosition());
System.out.println(diagnostic.getStartPosition());
System.out.println(diagnostic.getEndPosition());
System.out.println(diagnostic.getSource());
System.out.println(diagnostic.getMessage(null));
}
/**
* Instances of this class can be compiled with the JavaCompiler API
*/
private static final class CompilableFile extends SimpleJavaFileObject {
final String code;
CompilableFile(String name, String code) {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
}

相关内容

  • 没有找到相关文章

最新更新