author | sogoel |
Fri, 29 Aug 2014 00:42:42 -0700 (2014-08-29) | |
changeset 26274 | 02a5b23ee21c |
parent 25848 | 3bc09f4676a9 |
child 26391 | c775a09a4faf |
permissions | -rw-r--r-- |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
1 |
/* |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
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. |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
28 |
*/ |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
29 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
30 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
31 |
import java.io.*; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
32 |
import java.nio.file.Files; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
33 |
import java.util.*; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
34 |
import java.util.stream.Collectors; |
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 javax.tools.Diagnostic; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
37 |
import javax.tools.DiagnosticListener; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
38 |
import javax.tools.JavaCompiler; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
39 |
import javax.tools.JavaFileObject; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
40 |
import javax.tools.StandardJavaFileManager; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
41 |
import javax.tools.ToolProvider; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
42 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
43 |
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
|
44 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
45 |
public class RunCodingRules { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
46 |
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
|
47 |
new RunCodingRules().run(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
48 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
49 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
50 |
public void run() throws Exception { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
51 |
File testSrc = new File(System.getProperty("test.src", ".")); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
52 |
File targetDir = new File(System.getProperty("test.classes", ".")); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
53 |
File sourceDir = null; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
54 |
File crulesDir = null; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
55 |
for (File d = testSrc; d != null; d = d.getParentFile()) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
56 |
if (new File(d, "TEST.ROOT").exists()) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
57 |
d = d.getParentFile(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
58 |
File f = new File(d, "src/share/classes"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
59 |
if (f.exists()) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
60 |
sourceDir = f; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
61 |
f = new File(d, "make/tools"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
62 |
if (f.exists()) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
63 |
crulesDir = f; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
64 |
break; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
65 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
66 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
67 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
68 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
69 |
if (sourceDir == null || crulesDir == null) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
70 |
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
|
71 |
return ; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
72 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
73 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
74 |
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
75 |
StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
76 |
DiagnosticListener<JavaFileObject> noErrors = diagnostic -> { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
77 |
Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString()); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
78 |
}; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
79 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
80 |
List<File> crulesFiles = Files.walk(crulesDir.toPath()) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
81 |
.map(entry -> entry.toFile()) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
82 |
.filter(entry -> entry.getName().endsWith(".java")) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
83 |
.filter(entry -> entry.getParentFile().getName().equals("crules")) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
84 |
.collect(Collectors.toList()); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
85 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
86 |
File crulesTarget = new File(targetDir, "crules"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
87 |
crulesTarget.mkdirs(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
88 |
List<String> crulesOptions = Arrays.asList("-d", crulesTarget.getAbsolutePath()); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
89 |
javaCompiler.getTask(null, fm, noErrors, crulesOptions, null, |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
90 |
fm.getJavaFileObjectsFromFiles(crulesFiles)).call(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
91 |
File registration = new File(crulesTarget, "META-INF/services/com.sun.source.util.Plugin"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
92 |
registration.getParentFile().mkdirs(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
93 |
try (Writer metaInfServices = new FileWriter(registration)) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
94 |
metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
95 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
96 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
97 |
List<File> sources = Files.walk(sourceDir.toPath()) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
98 |
.map(entry -> entry.toFile()) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
99 |
.filter(entry -> entry.getName().endsWith(".java")) |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
100 |
.collect(Collectors.toList()); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
101 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
102 |
File sourceTarget = new File(targetDir, "classes"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
103 |
sourceTarget.mkdirs(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
104 |
String processorPath = crulesTarget.getAbsolutePath() + File.pathSeparator + |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
105 |
crulesDir.getAbsolutePath(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
106 |
List<String> options = Arrays.asList("-d", sourceTarget.getAbsolutePath(), |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
107 |
"-processorpath", processorPath, "-Xplugin:coding_rules"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
108 |
javaCompiler.getTask(null, fm, noErrors, options, null, |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
109 |
fm.getJavaFileObjectsFromFiles(sources)).call(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
110 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff
changeset
|
111 |
} |