RunCodingRules.java revision 3376:4c740bddc648
1/* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8043643 27 * @summary Run the langtools coding rules over the langtools source code. 28 * @modules jdk.compiler/com.sun.tools.javac.util 29 */ 30 31 32import java.io.*; 33import java.nio.file.Files; 34import java.nio.file.Path; 35import java.nio.file.Paths; 36import java.nio.file.StandardOpenOption; 37import java.util.*; 38import java.util.stream.Collectors; 39import java.util.stream.Stream; 40 41import javax.tools.Diagnostic; 42import javax.tools.DiagnosticListener; 43import javax.tools.JavaCompiler; 44import javax.tools.JavaFileObject; 45import javax.tools.StandardJavaFileManager; 46import javax.tools.ToolProvider; 47 48import com.sun.tools.javac.util.Assert; 49 50public class RunCodingRules { 51 public static void main(String... args) throws Exception { 52 new RunCodingRules().run(); 53 } 54 55 public void run() throws Exception { 56 Path testSrc = Paths.get(System.getProperty("test.src", ".")); 57 Path targetDir = Paths.get("."); 58 List<Path> sourceDirs = null; 59 Path crulesDir = null; 60 Path mainSrcDir = null; 61 List<Path> genSrcDirs = null; 62 for (Path d = testSrc; d != null; d = d.getParent()) { 63 if (Files.exists(d.resolve("TEST.ROOT"))) { 64 d = d.getParent(); 65 Path toolsPath = d.resolve("make/tools"); 66 Path buildDir = d.getParent().resolve("build"); 67 if (Files.exists(toolsPath) && Files.exists(buildDir)) { 68 mainSrcDir = d.resolve("src"); 69 crulesDir = toolsPath; 70 sourceDirs = Files.walk(mainSrcDir, 1) 71 .map(p -> p.resolve("share/classes")) 72 .filter(p -> Files.isDirectory(p)) 73 .collect(Collectors.toList()); 74 genSrcDirs = Files.walk(buildDir, 1) 75 .map(p -> p.resolve("support/gensrc")) 76 .filter(p -> Files.isDirectory(p.resolve("jdk.compiler"))) 77 .collect(Collectors.toList()); 78 break; 79 } 80 } 81 } 82 83 if (sourceDirs == null || crulesDir == null || genSrcDirs == null) { 84 System.err.println("Warning: sources not found, test skipped."); 85 return ; 86 } 87 88 JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); 89 try (StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null)) { 90 DiagnosticListener<JavaFileObject> noErrors = diagnostic -> { 91 Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString()); 92 }; 93 94 List<File> crulesFiles = Files.walk(crulesDir) 95 .filter(entry -> entry.getFileName().toString().endsWith(".java")) 96 .filter(entry -> entry.getParent().endsWith("crules")) 97 .map(entry -> entry.toFile()) 98 .collect(Collectors.toList()); 99 100 Path crulesTarget = targetDir.resolve("crules"); 101 Files.createDirectories(crulesTarget); 102 List<String> crulesOptions = Arrays.asList( 103 "-XaddExports:jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", 104 "-XaddExports:jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", 105 "-XaddExports:jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED", 106 "-XaddExports:jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", 107 "-XaddExports:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", 108 "-d", crulesTarget.toString()); 109 javaCompiler.getTask(null, fm, noErrors, crulesOptions, null, 110 fm.getJavaFileObjectsFromFiles(crulesFiles)).call(); 111 Path registration = crulesTarget.resolve("META-INF/services/com.sun.source.util.Plugin"); 112 Files.createDirectories(registration.getParent()); 113 try (Writer metaInfServices = Files.newBufferedWriter(registration, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { 114 metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n"); 115 } 116 117 List<File> sources = sourceDirs.stream() 118 .flatMap(dir -> silentFilesWalk(dir)) 119 .filter(entry -> entry.getFileName().toString().endsWith(".java")) 120 .map(p -> p.toFile()) 121 .collect(Collectors.toList()); 122 123 String FS = File.separator; 124 String PS = File.pathSeparator; 125 126 Path genSrcTarget = targetDir.resolve("gensrc"); 127 List<String> genSrcFiles = Arrays.asList( 128 "jdk.compiler/com/sun/tools/javac/resources/CompilerProperties.java" 129 ); 130 for (String f : genSrcFiles) { 131 for (Path dir : genSrcDirs) { 132 Path from = dir.resolve(f.replace("/", FS)); 133 if (Files.exists(from)) { 134 try { 135 Path to = genSrcTarget.resolve(f.replace("/", FS)); 136 Files.createDirectories(to.getParent()); 137 Files.copy(from, to); 138 } catch (IOException e) { 139 e.printStackTrace(); 140 } 141 } 142 } 143 } 144 145 Path sourceTarget = targetDir.resolve("classes"); 146 Files.createDirectories(sourceTarget); 147 String processorPath = crulesTarget + PS + crulesDir; 148 149 List<String> options = Arrays.asList( 150 "-d", sourceTarget.toString(), 151 "-modulesourcepath", mainSrcDir + FS + "*" + FS + "share" + FS + "classes" + PS + genSrcTarget, 152 "-XDaccessInternalAPI", 153 "-processorpath", processorPath, 154 "-Xplugin:coding_rules"); 155 javaCompiler.getTask(null, fm, noErrors, options, null, 156 fm.getJavaFileObjectsFromFiles(sources)).call(); 157 } 158 } 159 160 Stream<Path> silentFilesWalk(Path dir) throws IllegalStateException { 161 try { 162 return Files.walk(dir); 163 } catch (IOException ex) { 164 throw new IllegalStateException(ex); 165 } 166 } 167} 168