8054563: Update RunCodingRules.java for source code reorg

Tests RunCodingRules and MessageInfo fixed to work in the new source code structure.

Reviewed-by: jjg
This commit is contained in:
Jan Lahoda 2014-09-04 08:48:29 +02:00
parent f0b4f3d282
commit 83abeb739b
2 changed files with 46 additions and 33 deletions

View file

@ -30,8 +30,12 @@
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener; import javax.tools.DiagnosticListener;
@ -48,25 +52,26 @@ public class RunCodingRules {
} }
public void run() throws Exception { public void run() throws Exception {
File testSrc = new File(System.getProperty("test.src", ".")); Path testSrc = Paths.get(System.getProperty("test.src", "."));
File targetDir = new File(System.getProperty("test.classes", ".")); Path targetDir = Paths.get(System.getProperty("test.classes", "."));
File sourceDir = null; List<Path> sourceDirs = null;
File crulesDir = null; Path crulesDir = null;
for (File d = testSrc; d != null; d = d.getParentFile()) { for (Path d = testSrc; d != null; d = d.getParent()) {
if (new File(d, "TEST.ROOT").exists()) { if (Files.exists(d.resolve("TEST.ROOT"))) {
d = d.getParentFile(); d = d.getParent();
File f = new File(d, "src/share/classes"); Path toolsPath = d.resolve("make/tools");
if (f.exists()) { if (Files.exists(toolsPath)) {
sourceDir = f; crulesDir = toolsPath;
f = new File(d, "make/tools"); sourceDirs = Files.walk(d.resolve("src"), 1)
if (f.exists()) .map(p -> p.resolve("share/classes"))
crulesDir = f; .filter(p -> Files.isDirectory(p))
.collect(Collectors.toList());
break; break;
} }
} }
} }
if (sourceDir == null || crulesDir == null) { if (sourceDirs == null || crulesDir == null) {
System.err.println("Warning: sources not found, test skipped."); System.err.println("Warning: sources not found, test skipped.");
return ; return ;
} }
@ -77,35 +82,43 @@ public class RunCodingRules {
Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString()); Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString());
}; };
List<File> crulesFiles = Files.walk(crulesDir.toPath()) List<File> crulesFiles = Files.walk(crulesDir)
.filter(entry -> entry.getFileName().toString().endsWith(".java"))
.filter(entry -> entry.getParent().endsWith("crules"))
.map(entry -> entry.toFile()) .map(entry -> entry.toFile())
.filter(entry -> entry.getName().endsWith(".java"))
.filter(entry -> entry.getParentFile().getName().equals("crules"))
.collect(Collectors.toList()); .collect(Collectors.toList());
File crulesTarget = new File(targetDir, "crules"); Path crulesTarget = targetDir.resolve("crules");
crulesTarget.mkdirs(); Files.createDirectories(crulesTarget);
List<String> crulesOptions = Arrays.asList("-d", crulesTarget.getAbsolutePath()); List<String> crulesOptions = Arrays.asList("-d", crulesTarget.toString());
javaCompiler.getTask(null, fm, noErrors, crulesOptions, null, javaCompiler.getTask(null, fm, noErrors, crulesOptions, null,
fm.getJavaFileObjectsFromFiles(crulesFiles)).call(); fm.getJavaFileObjectsFromFiles(crulesFiles)).call();
File registration = new File(crulesTarget, "META-INF/services/com.sun.source.util.Plugin"); Path registration = crulesTarget.resolve("META-INF/services/com.sun.source.util.Plugin");
registration.getParentFile().mkdirs(); Files.createDirectories(registration.getParent());
try (Writer metaInfServices = new FileWriter(registration)) { try (Writer metaInfServices = Files.newBufferedWriter(registration, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n"); metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n");
} }
List<File> sources = Files.walk(sourceDir.toPath()) List<File> sources = sourceDirs.stream()
.map(entry -> entry.toFile()) .flatMap(dir -> silentFilesWalk(dir))
.filter(entry -> entry.getName().endsWith(".java")) .filter(entry -> entry.getFileName().toString().endsWith(".java"))
.map(p -> p.toFile())
.collect(Collectors.toList()); .collect(Collectors.toList());
File sourceTarget = new File(targetDir, "classes"); Path sourceTarget = targetDir.resolve("classes");
sourceTarget.mkdirs(); Files.createDirectories(sourceTarget);
String processorPath = crulesTarget.getAbsolutePath() + File.pathSeparator + String processorPath = crulesTarget.toString() + File.pathSeparator + crulesDir.toString();
crulesDir.getAbsolutePath(); List<String> options = Arrays.asList("-d", sourceTarget.toString(),
List<String> options = Arrays.asList("-d", sourceTarget.getAbsolutePath(),
"-processorpath", processorPath, "-Xplugin:coding_rules"); "-processorpath", processorPath, "-Xplugin:coding_rules");
javaCompiler.getTask(null, fm, noErrors, options, null, javaCompiler.getTask(null, fm, noErrors, options, null,
fm.getJavaFileObjectsFromFiles(sources)).call(); fm.getJavaFileObjectsFromFiles(sources)).call();
} }
Stream<Path> silentFilesWalk(Path dir) throws IllegalStateException {
try {
return Files.walk(dir);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
} }

View file

@ -168,7 +168,7 @@ public class MessageInfo {
for (File d = testSrc; d != null; d = d.getParentFile()) { for (File d = testSrc; d != null; d = d.getParentFile()) {
if (new File(d, "TEST.ROOT").exists()) { if (new File(d, "TEST.ROOT").exists()) {
d = d.getParentFile(); d = d.getParentFile();
File f = new File(d, "src/share/classes/com/sun/tools/javac/resources/compiler.properties"); File f = new File(d, "src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties");
if (f.exists()) { if (f.exists()) {
msgFile = f; msgFile = f;
break; break;