RunCodingRules.java revision 2687:56f8be952a5c
1/*
2 * Copyright (c) 2014, 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 */
29
30
31import java.io.*;
32import java.nio.file.Files;
33import java.nio.file.Path;
34import java.nio.file.Paths;
35import java.nio.file.StandardOpenOption;
36import java.util.*;
37import java.util.stream.Collectors;
38import java.util.stream.Stream;
39
40import javax.tools.Diagnostic;
41import javax.tools.DiagnosticListener;
42import javax.tools.JavaCompiler;
43import javax.tools.JavaFileObject;
44import javax.tools.StandardJavaFileManager;
45import javax.tools.ToolProvider;
46
47import com.sun.tools.javac.util.Assert;
48
49public class RunCodingRules {
50    public static void main(String... args) throws Exception {
51        new RunCodingRules().run();
52    }
53
54    public void run() throws Exception {
55        Path testSrc = Paths.get(System.getProperty("test.src", "."));
56        Path targetDir = Paths.get(System.getProperty("test.classes", "."));
57        List<Path> sourceDirs = null;
58        Path crulesDir = null;
59        for (Path d = testSrc; d != null; d = d.getParent()) {
60            if (Files.exists(d.resolve("TEST.ROOT"))) {
61                d = d.getParent();
62                Path toolsPath = d.resolve("make/tools");
63                if (Files.exists(toolsPath)) {
64                    crulesDir = toolsPath;
65                    sourceDirs = Files.walk(d.resolve("src"), 1)
66                                      .map(p -> p.resolve("share/classes"))
67                                      .filter(p -> Files.isDirectory(p))
68                                      .collect(Collectors.toList());
69                    break;
70                }
71            }
72        }
73
74        if (sourceDirs == null || crulesDir == null) {
75            System.err.println("Warning: sources not found, test skipped.");
76            return ;
77        }
78
79        JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
80        try (StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null)) {
81            DiagnosticListener<JavaFileObject> noErrors = diagnostic -> {
82                Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString());
83            };
84
85            List<File> crulesFiles = Files.walk(crulesDir)
86                                          .filter(entry -> entry.getFileName().toString().endsWith(".java"))
87                                          .filter(entry -> entry.getParent().endsWith("crules"))
88                                          .map(entry -> entry.toFile())
89                                          .collect(Collectors.toList());
90
91            Path crulesTarget = targetDir.resolve("crules");
92            Files.createDirectories(crulesTarget);
93            List<String> crulesOptions = Arrays.asList("-d", crulesTarget.toString());
94            javaCompiler.getTask(null, fm, noErrors, crulesOptions, null,
95                    fm.getJavaFileObjectsFromFiles(crulesFiles)).call();
96            Path registration = crulesTarget.resolve("META-INF/services/com.sun.source.util.Plugin");
97            Files.createDirectories(registration.getParent());
98            try (Writer metaInfServices = Files.newBufferedWriter(registration, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
99                metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n");
100            }
101
102            List<File> sources = sourceDirs.stream()
103                                           .flatMap(dir -> silentFilesWalk(dir))
104                                           .filter(entry -> entry.getFileName().toString().endsWith(".java"))
105                                           .map(p -> p.toFile())
106                                           .collect(Collectors.toList());
107
108            Path sourceTarget = targetDir.resolve("classes");
109            Files.createDirectories(sourceTarget);
110            String processorPath = crulesTarget.toString() + File.pathSeparator + crulesDir.toString();
111            List<String> options = Arrays.asList("-d", sourceTarget.toString(),
112                    "-processorpath", processorPath, "-Xplugin:coding_rules");
113            javaCompiler.getTask(null, fm, noErrors, options, null,
114                    fm.getJavaFileObjectsFromFiles(sources)).call();
115        }
116    }
117
118    Stream<Path> silentFilesWalk(Path dir) throws IllegalStateException {
119        try {
120            return Files.walk(dir);
121        } catch (IOException ex) {
122            throw new IllegalStateException(ex);
123        }
124    }
125}
126