langtools/test/tools/all/RunCodingRules.java
author hannesw
Tue, 22 Mar 2016 14:23:16 +0100
changeset 36690 06b714373aa4
parent 35359 f04501964016
child 36526 3b41f1c69604
permissions -rw-r--r--
8151810: for-in iteration does not provide per-iteration scope Reviewed-by: attila, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     4
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     8
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    14
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    18
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    21
 * questions.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    22
 */
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    23
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    24
/*
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    25
 * @test
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    26
 * @bug 8043643
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    27
 * @summary Run the langtools coding rules over the langtools source code.
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
 * @modules java.base/sun.reflect.annotation
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
 *          java.logging
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    30
 *          java.xml
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.resources
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 30730
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.util
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    33
 */
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    34
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    35
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    36
import java.io.*;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    37
import java.nio.file.Files;
26391
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    38
import java.nio.file.Path;
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    39
import java.nio.file.Paths;
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    40
import java.nio.file.StandardOpenOption;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    41
import java.util.*;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    42
import java.util.stream.Collectors;
26391
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    43
import java.util.stream.Stream;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    44
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    45
import javax.tools.Diagnostic;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    46
import javax.tools.DiagnosticListener;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    47
import javax.tools.JavaCompiler;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    48
import javax.tools.JavaFileObject;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    49
import javax.tools.StandardJavaFileManager;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    50
import javax.tools.ToolProvider;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    51
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    52
import com.sun.tools.javac.util.Assert;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    53
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    54
public class RunCodingRules {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    55
    public static void main(String... args) throws Exception {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    56
        new RunCodingRules().run();
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    57
    }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    58
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    59
    public void run() throws Exception {
26391
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    60
        Path testSrc = Paths.get(System.getProperty("test.src", "."));
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    61
        Path targetDir = Paths.get(System.getProperty("test.classes", "."));
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    62
        List<Path> sourceDirs = null;
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    63
        Path crulesDir = null;
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    64
        for (Path d = testSrc; d != null; d = d.getParent()) {
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    65
            if (Files.exists(d.resolve("TEST.ROOT"))) {
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    66
                d = d.getParent();
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    67
                Path toolsPath = d.resolve("make/tools");
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    68
                if (Files.exists(toolsPath)) {
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    69
                    crulesDir = toolsPath;
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    70
                    sourceDirs = Files.walk(d.resolve("src"), 1)
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    71
                                      .map(p -> p.resolve("share/classes"))
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    72
                                      .filter(p -> Files.isDirectory(p))
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    73
                                      .collect(Collectors.toList());
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    74
                    break;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    75
                }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    76
            }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    77
        }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    78
26391
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
    79
        if (sourceDirs == null || crulesDir == null) {
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    80
            System.err.println("Warning: sources not found, test skipped.");
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    81
            return ;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    82
        }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    83
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    84
        JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    85
        try (StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    86
            DiagnosticListener<JavaFileObject> noErrors = diagnostic -> {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    87
                Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    88
            };
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    89
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    90
            List<File> crulesFiles = Files.walk(crulesDir)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    91
                                          .filter(entry -> entry.getFileName().toString().endsWith(".java"))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    92
                                          .filter(entry -> entry.getParent().endsWith("crules"))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    93
                                          .map(entry -> entry.toFile())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    94
                                          .collect(Collectors.toList());
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    95
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    96
            Path crulesTarget = targetDir.resolve("crules");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    97
            Files.createDirectories(crulesTarget);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    98
            List<String> crulesOptions = Arrays.asList("-d", crulesTarget.toString());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
    99
            javaCompiler.getTask(null, fm, noErrors, crulesOptions, null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   100
                    fm.getJavaFileObjectsFromFiles(crulesFiles)).call();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   101
            Path registration = crulesTarget.resolve("META-INF/services/com.sun.source.util.Plugin");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   102
            Files.createDirectories(registration.getParent());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   103
            try (Writer metaInfServices = Files.newBufferedWriter(registration, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   104
                metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   105
            }
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   106
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   107
            List<File> sources = sourceDirs.stream()
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   108
                                           .flatMap(dir -> silentFilesWalk(dir))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   109
                                           .filter(entry -> entry.getFileName().toString().endsWith(".java"))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   110
                                           .map(p -> p.toFile())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   111
                                           .collect(Collectors.toList());
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   112
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   113
            Path sourceTarget = targetDir.resolve("classes");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   114
            Files.createDirectories(sourceTarget);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   115
            String processorPath = crulesTarget.toString() + File.pathSeparator + crulesDir.toString();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   116
            List<String> options = Arrays.asList("-d", sourceTarget.toString(),
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   117
                    "-processorpath", processorPath, "-Xplugin:coding_rules");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   118
            javaCompiler.getTask(null, fm, noErrors, options, null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   119
                    fm.getJavaFileObjectsFromFiles(sources)).call();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26391
diff changeset
   120
        }
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   121
    }
26391
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   122
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   123
    Stream<Path> silentFilesWalk(Path dir) throws IllegalStateException {
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   124
        try {
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   125
            return Files.walk(dir);
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   126
        } catch (IOException ex) {
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   127
            throw new IllegalStateException(ex);
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   128
        }
c775a09a4faf 8054563: Update RunCodingRules.java for source code reorg
jlahoda
parents: 25848
diff changeset
   129
    }
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   130
}