APIDeps.java revision 3376:4c740bddc648
1/* 2 * Copyright (c) 2012, 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 8015912 8029216 8048063 8050804 27 * @summary Test -apionly and -jdkinternals options 28 * @modules java.base/sun.security.x509 29 * java.management 30 * jdk.jdeps/com.sun.tools.classfile 31 * jdk.jdeps/com.sun.tools.jdeps 32 * @run main APIDeps 33 */ 34 35import java.io.File; 36import java.io.IOException; 37import java.io.PrintWriter; 38import java.io.StringWriter; 39import java.nio.file.*; 40import java.util.*; 41import java.util.regex.*; 42import java.util.stream.Collectors; 43 44public class APIDeps { 45 public static void main(String... args) throws Exception { 46 int errors = 0; 47 errors += new APIDeps().run(); 48 if (errors > 0) 49 throw new Exception(errors + " errors found"); 50 } 51 52 private static final Path dest = Paths.get(System.getProperty("test.classes", "."), "tmp"); 53 private static final String[] srcDirs = new String[] { 54 "m", "b", "c", "d", "e", "f", "g" 55 }; 56 void setup(Path dest) throws IOException { 57 CompilerUtils.cleanDir(dest); 58 Files.createDirectories(dest); 59 Path testsrc = Paths.get(System.getProperty("test.src")); 60 List<String> options = new ArrayList<>(); 61 62 // add -XaddExports 63 String testModules = System.getProperty("test.modules", ""); 64 List<String> addExports = new ArrayList<>(); 65 for (String s : testModules.split("\\s+")) { 66 if (s.isEmpty()) continue; 67 if (s.indexOf('/') != -1) 68 addExports.add("-XaddExports:" + s.trim() + "=ALL-UNNAMED"); 69 } 70 options.addAll(addExports); 71 72 for (String dir : srcDirs) { 73 Path source = testsrc.resolve(dir); 74 boolean ok = CompilerUtils.compile(source, dest, options.toArray(new String[0])); 75 if (!ok) { 76 throw new RuntimeException("compilation fails"); 77 } 78 } 79 } 80 81 int run() throws IOException { 82 // compile classes in a separate directory for analysis 83 setup(dest); 84 85 File testDir = dest.toFile(); 86 String testDirBasename = testDir.toPath().getFileName().toString(); 87 File mDir = new File(testDir, "m"); 88 // all dependencies 89 test(new File(mDir, "Bar.class"), 90 new String[] {"java.lang.Object", "java.lang.String", 91 "java.util.Set", "java.util.HashSet", 92 "java.lang.management.ManagementFactory", 93 "java.lang.management.RuntimeMXBean", 94 "b.B", "c.C", "d.D", "f.F", "g.G"}, 95 new String[] {"compact1", "compact3", testDirBasename}, 96 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"}); 97 test(new File(mDir, "Foo.class"), 98 new String[] {"c.I", "e.E", "f.F"}, 99 new String[] {testDirBasename}, 100 new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P"}); 101 test(new File(mDir, "Foo.class"), 102 new String[] {"c.I", "e.E", "f.F", "m.Bar"}, 103 new String[] {testDirBasename}, 104 new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-filter:none", "-P"}); 105 test(new File(mDir, "Gee.class"), 106 new String[] {"g.G", "sun.security.x509.X509CertInfo", "com.sun.tools.classfile.ClassFile", 107 "com.sun.management.ThreadMXBean", "com.sun.source.tree.BinaryTree"}, 108 new String[] {testDirBasename, "JDK internal API", "compact3", ""}, 109 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"}); 110 111 // -jdkinternals 112 test(new File(mDir, "Gee.class"), 113 new String[] {"sun.security.x509.X509CertInfo", "com.sun.tools.classfile.ClassFile"}, 114 new String[] {"JDK internal API"}, 115 new String[] {"-jdkinternals", "-quiet"}); 116 // -jdkinternals parses all classes on -classpath and the input arguments 117 test(new File(mDir, "Gee.class"), 118 new String[] {"com.sun.tools.classfile.ClassFile", 119 "sun.security.x509.X509CertInfo"}, 120 new String[] {"JDK internal API"}, 121 // use -classpath tmp/a with no use of JDK internal API 122 new String[] {"-classpath", dest.resolve("a").toString(), "-jdkinternals", "-quiet"}); 123 124 // parse only APIs 125 test(mDir, 126 new String[] {"java.lang.Object", "java.lang.String", 127 "java.util.Set", 128 "c.C", "d.D", "c.I", "e.E"}, 129 new String[] {"compact1", testDirBasename}, 130 new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P", "-apionly"}); 131 132 test(mDir, 133 new String[] {"java.lang.Object", "java.lang.String", 134 "java.util.Set", 135 "c.C", "d.D", "c.I", "e.E", "m.Bar"}, 136 new String[] {"compact1", testDirBasename, mDir.getName()}, 137 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"}); 138 return errors; 139 } 140 141 void test(File file, String[] expect, String[] profiles) { 142 test(file, expect, profiles, new String[0]); 143 } 144 145 void test(File file, String[] expect, String[] profiles, String[] options) { 146 List<String> args = new ArrayList<>(Arrays.asList(options)); 147 if (file != null) { 148 args.add(file.getPath()); 149 } 150 checkResult("api-dependencies", expect, profiles, 151 jdeps(args.toArray(new String[0]))); 152 } 153 154 Map<String,String> jdeps(String... args) { 155 StringWriter sw = new StringWriter(); 156 PrintWriter pw = new PrintWriter(sw); 157 System.err.println("jdeps " + Arrays.toString(args)); 158 int rc = com.sun.tools.jdeps.Main.run(args, pw); 159 pw.close(); 160 String out = sw.toString(); 161 if (!out.isEmpty()) 162 System.err.println(out); 163 if (rc != 0) 164 throw new Error("jdeps failed: rc=" + rc); 165 return findDeps(out); 166 } 167 168 // Pattern used to parse lines 169 private static Pattern linePattern = Pattern.compile(".*\r?\n"); 170 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); 171 172 // Use the linePattern to break the given String into lines, applying 173 // the pattern to each line to see if we have a match 174 private static Map<String,String> findDeps(String out) { 175 Map<String,String> result = new HashMap<>(); 176 Matcher lm = linePattern.matcher(out); // Line matcher 177 Matcher pm = null; // Pattern matcher 178 int lines = 0; 179 while (lm.find()) { 180 lines++; 181 CharSequence cs = lm.group(); // The current line 182 if (pm == null) 183 pm = pattern.matcher(cs); 184 else 185 pm.reset(cs); 186 if (pm.find()) 187 result.put(pm.group(1), pm.group(2).trim()); 188 if (lm.end() == out.length()) 189 break; 190 } 191 return result; 192 } 193 194 void checkResult(String label, String[] expect, Collection<String> found) { 195 List<String> list = Arrays.asList(expect); 196 if (!isEqual(list, found)) 197 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'"); 198 } 199 200 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) { 201 // check the dependencies 202 checkResult(label, expect, result.keySet()); 203 // check profile information 204 Set<String> values = new TreeSet<>(); 205 String internal = "JDK internal API"; 206 for (String s: result.values()) { 207 if (s.startsWith(internal)){ 208 values.add(internal); 209 } else { 210 values.add(s); 211 } 212 } 213 checkResult(label, profiles, values); 214 } 215 216 boolean isEqual(List<String> expected, Collection<String> found) { 217 if (expected.size() != found.size()) 218 return false; 219 220 List<String> list = new ArrayList<>(found); 221 list.removeAll(expected); 222 return list.isEmpty(); 223 } 224 225 void error(String msg) { 226 System.err.println("Error: " + msg); 227 errors++; 228 } 229 230 int errors; 231 232} 233