# HG changeset patch # User jlahoda # Date 1462374415 -7200 # Node ID 24e70308f4ead7c358cba3931aac723ec573817c # Parent 3c7998b21c27305b038839eca80f4e0a000afc8b 8156000: tools/all/RunCodingRules.java fails if build dir exists, but generated sources do not Summary: RunCodingRules test compiles and runs PropertiesParser to get the generated CompilerProperties.java Reviewed-by: mcimadamore, jjg diff -r 3c7998b21c27 -r 24e70308f4ea langtools/make/tools/propertiesparser/PropertiesParser.java --- a/langtools/make/tools/propertiesparser/PropertiesParser.java Tue May 03 11:38:13 2016 +0100 +++ b/langtools/make/tools/propertiesparser/PropertiesParser.java Wed May 04 17:06:55 2016 +0200 @@ -28,24 +28,10 @@ import propertiesparser.parser.MessageFile; import propertiesparser.gen.ClassGenerator; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.lang.RuntimeException; -import java.lang.Throwable; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Collections; +import java.io.PrintStream; import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; -import java.util.Properties; /** Translates a .properties file into a .java file containing an enum-like Java class * which defines static factory methods for all resource keys in a given resource file.

@@ -64,13 +50,17 @@ } public static void main(String[] args) { - PropertiesParser pp = new PropertiesParser(msg -> System.out.println(msg)); - boolean ok = pp.run(args); + boolean ok = run(args, System.out); if ( !ok ) { System.exit(1); } } + public static boolean run(String[] args, PrintStream out) { + PropertiesParser pp = new PropertiesParser(msg -> out.println(msg)); + return pp.run(args); + } + public static interface Logger { void info(String msg); } diff -r 3c7998b21c27 -r 24e70308f4ea langtools/test/tools/all/RunCodingRules.java --- a/langtools/test/tools/all/RunCodingRules.java Tue May 03 11:38:13 2016 +0100 +++ b/langtools/test/tools/all/RunCodingRules.java Wed May 04 17:06:55 2016 +0200 @@ -30,6 +30,9 @@ import java.io.*; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -58,29 +61,23 @@ List sourceDirs = null; Path crulesDir = null; Path mainSrcDir = null; - List genSrcDirs = null; for (Path d = testSrc; d != null; d = d.getParent()) { if (Files.exists(d.resolve("TEST.ROOT"))) { d = d.getParent(); Path toolsPath = d.resolve("make/tools"); - Path buildDir = d.getParent().resolve("build"); - if (Files.exists(toolsPath) && Files.exists(buildDir)) { + if (Files.exists(toolsPath)) { mainSrcDir = d.resolve("src"); crulesDir = toolsPath; sourceDirs = Files.walk(mainSrcDir, 1) .map(p -> p.resolve("share/classes")) .filter(p -> Files.isDirectory(p)) .collect(Collectors.toList()); - genSrcDirs = Files.walk(buildDir, 1) - .map(p -> p.resolve("support/gensrc")) - .filter(p -> Files.isDirectory(p.resolve("jdk.compiler"))) - .collect(Collectors.toList()); break; } } } - if (sourceDirs == null || crulesDir == null || genSrcDirs == null) { + if (sourceDirs == null || crulesDir == null) { System.err.println("Warning: sources not found, test skipped."); return ; } @@ -90,7 +87,10 @@ DiagnosticListener noErrors = diagnostic -> { Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString()); }; + String FS = File.separator; + String PS = File.pathSeparator; + //compile crules: List crulesFiles = Files.walk(crulesDir) .filter(entry -> entry.getFileName().toString().endsWith(".java")) .filter(entry -> entry.getParent().endsWith("crules")) @@ -114,41 +114,61 @@ metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n"); } + //generate CompilerProperties.java: + List propertiesParserFiles = + Files.walk(crulesDir.resolve("propertiesparser")) + .filter(entry -> entry.getFileName().toString().endsWith(".java")) + .map(entry -> entry.toFile()) + .collect(Collectors.toList()); + + Path propertiesParserTarget = targetDir.resolve("propertiesParser"); + Files.createDirectories(propertiesParserTarget); + List propertiesParserOptions = Arrays.asList( + "-d", propertiesParserTarget.toString()); + javaCompiler.getTask(null, fm, noErrors, propertiesParserOptions, null, + fm.getJavaFileObjectsFromFiles(propertiesParserFiles)).call(); + + Path genSrcTarget = targetDir.resolve("gensrc"); + + ClassLoader propertiesParserLoader = new URLClassLoader(new URL[] { + propertiesParserTarget.toUri().toURL(), + crulesDir.toUri().toURL() + }); + Class propertiesParserClass = + Class.forName("propertiesparser.PropertiesParser", false, propertiesParserLoader); + Method propertiesParserRun = + propertiesParserClass.getDeclaredMethod("run", String[].class, PrintStream.class); + String compilerProperties = + "jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties"; + Path propertiesPath = mainSrcDir.resolve(compilerProperties.replace("/", FS)); + Path genSrcTargetDir = genSrcTarget.resolve(mainSrcDir.relativize(propertiesPath.getParent())); + + Files.createDirectories(genSrcTargetDir); + String[] propertiesParserRunOptions = new String[] { + "-compile", propertiesPath.toString(), genSrcTargetDir.toString() + }; + + Object result = propertiesParserRun.invoke(null, propertiesParserRunOptions, System.err); + + if (!(result instanceof Boolean) || !(Boolean) result) { + throw new AssertionError("Cannot parse properties: " + result); + } + + //compile langtools sources with crules enabled: List sources = sourceDirs.stream() .flatMap(dir -> silentFilesWalk(dir)) .filter(entry -> entry.getFileName().toString().endsWith(".java")) .map(p -> p.toFile()) .collect(Collectors.toList()); - String FS = File.separator; - String PS = File.pathSeparator; - - Path genSrcTarget = targetDir.resolve("gensrc"); - List genSrcFiles = Arrays.asList( - "jdk.compiler/com/sun/tools/javac/resources/CompilerProperties.java" - ); - for (String f : genSrcFiles) { - for (Path dir : genSrcDirs) { - Path from = dir.resolve(f.replace("/", FS)); - if (Files.exists(from)) { - try { - Path to = genSrcTarget.resolve(f.replace("/", FS)); - Files.createDirectories(to.getParent()); - Files.copy(from, to); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - Path sourceTarget = targetDir.resolve("classes"); Files.createDirectories(sourceTarget); String processorPath = crulesTarget + PS + crulesDir; List options = Arrays.asList( "-d", sourceTarget.toString(), - "-modulesourcepath", mainSrcDir + FS + "*" + FS + "share" + FS + "classes" + PS + genSrcTarget, + "-modulesourcepath", mainSrcDir + FS + "*" + FS + "share" + FS + "classes" + PS + + genSrcTarget + FS + "*" + FS + "share" + FS + "classes", "-XDaccessInternalAPI", "-processorpath", processorPath, "-Xplugin:coding_rules");