line. Parses the line to extract the archive
+ // name and dependent class name, and record them in the map...
+ void parse(String line1, String line2) {
+ String archive = line1.substring(0, line1.indexOf(" -> "));
+ int l2ArrowIndex = line2.indexOf(" -> ");
+ String className = line2.substring(2, l2ArrowIndex).replace(" ", "");
+ String depdescr = line2.substring(l2ArrowIndex + 4);
+ String depclass = depdescr.substring(0, depdescr.indexOf(" "));
+ deps.computeIfAbsent(archive, (k) -> new HashSet<>());
+ deps.get(archive).add(className);
+ if (VERBOSE) {
+ System.out.println(archive+": "+className+" depends on "+depclass);
+ }
+ }
+
+ }
+
+ /**
+ * The main method.
+ *
+ * Can be run in two modes:
+ *
+ * - From jtreg: expects 1 argument in the form {@code --test:}
+ * - From command line: expected syntax is {@code -e -v jar [jars..]}
+ *
+ * When called from the command line this method will call jdeps recursively
+ * to build a closure of the dependencies on {@code } and print a summary.
+ * When called from jtreg - it will call jdeps either once only or
+ * recursively depending on the pattern.
+ * @param args either {@code --test:} or {@code -e -v jar [jars..]}.
+ */
+ public static void main(String[] args) {
+ runWithLocale(Locale.ENGLISH, TestCaseData.valueOf(args)::test);
+ }
+
+ private static void runWithLocale(Locale loc, Runnable run) {
+ final Locale defaultLocale = Locale.getDefault();
+ Locale.setDefault(loc);
+ try {
+ run.run();
+ } finally {
+ Locale.setDefault(defaultLocale);
+ }
+ }
+
+
+ public static void test(String[] args, Map> expected,
+ String expectedText, boolean closure) {
+ try {
+ doTest(args, expected, expectedText, closure);
+ } catch (Throwable t) {
+ try {
+ printDiagnostic(args, expectedText, t, closure);
+ } catch(Throwable tt) {
+ throw t;
+ }
+ throw t;
+ }
+ }
+
+ static class TextFormatException extends RuntimeException {
+ final String expected;
+ final String actual;
+ TextFormatException(String message, String expected, String actual) {
+ super(message);
+ this.expected = expected;
+ this.actual = actual;
+ }
+ }
+
+ public static void printDiagnostic(String[] args, String expectedText,
+ Throwable t, boolean closure) {
+ if (expectedText != null || t instanceof TextFormatException) {
+ System.err.println("===== TEST FAILED =======");
+ System.err.println("command: " + Stream.of(args)
+ .reduce("jdeps", (s1,s2) -> s1.concat(" ").concat(s2)));
+ System.err.println("===== Expected Output =======");
+ System.err.append(expectedText);
+ System.err.println("===== Command Output =======");
+ if (t instanceof TextFormatException) {
+ System.err.print(((TextFormatException)t).actual);
+ } else {
+ com.sun.tools.jdeps.Main.run(args, new PrintWriter(System.err));
+ if (closure) System.err.println("... (closure not available) ...");
+ }
+ System.err.println("=============================");
+ }
+ }
+
+ public static void doTest(String[] args, Map> expected,
+ String expectedText, boolean closure) {
+ if (args.length < 3 || !"-e".equals(args[0]) || !"-v".equals(args[2])) {
+ System.err.println("Syntax: -e -v [list of jars or directories]");
+ return;
+ }
+ Map>> alldeps = new HashMap<>();
+ String depName = args[1];
+ List search = new ArrayList<>();
+ search.add(depName);
+ Set searched = new LinkedHashSet<>();
+ StringBuilder text = new StringBuilder();
+ while(!search.isEmpty()) {
+ args[1] = search.remove(0);
+ if (VERBOSE) {
+ System.out.println("Looking for " + args[1]);
+ }
+ searched.add(args[1]);
+ Map> deps =
+ alldeps.computeIfAbsent(args[1], (k) -> new HashMap<>());
+ OutputStreamParser parser = new OutputStreamParser(deps);
+ PrintWriter writer = new PrintWriter(parser);
+ com.sun.tools.jdeps.Main.run(args, writer);
+ if (VERBOSE) {
+ System.out.println("Found: " + deps.values().stream()
+ .flatMap(s -> s.stream()).collect(Collectors.toSet()));
+ }
+ if (expectedText != null) {
+ text.append(parser.text.toString());
+ }
+ search.addAll(deps.values().stream()
+ .flatMap(s -> s.stream())
+ .filter(k -> !searched.contains(k))
+ .collect(Collectors.toSet()));
+ if (!closure) break;
+ }
+
+ // Print summary...
+ final Set classes = alldeps.values().stream()
+ .flatMap((m) -> m.values().stream())
+ .flatMap(s -> s.stream()).collect(Collectors.toSet());
+ Map> result = new HashMap<>();
+ for (String c : classes) {
+ Set archives = new HashSet<>();
+ Set dependencies = new HashSet<>();
+ for (String d : alldeps.keySet()) {
+ Map> m = alldeps.get(d);
+ for (String a : m.keySet()) {
+ Set s = m.get(a);
+ if (s.contains(c)) {
+ archives.add(a);
+ dependencies.add(d);
+ }
+ }
+ }
+ result.put(c, dependencies);
+ System.out.println(c + " " + archives + " depends on " + dependencies);
+ }
+
+ // If we're in jtreg, then check result (expectedText != null)
+ if (expectedText != null && COMPARE_TEXT) {
+ //text.append(String.format("%n"));
+ if (text.toString().equals(expectedText)) {
+ System.out.println("SUCCESS - got expected text");
+ } else {
+ throw new TextFormatException("jdeps output is not as expected",
+ expectedText, text.toString());
+ }
+ }
+ if (expected != null) {
+ if (expected.equals(result)) {
+ System.out.println("SUCCESS - found expected dependencies");
+ } else if (expectedText == null) {
+ throw new RuntimeException("Bad dependencies: Expected " + expected
+ + " but found " + result);
+ } else {
+ throw new TextFormatException("Bad dependencies: Expected "
+ + expected
+ + " but found " + result,
+ expectedText, text.toString());
+ }
+ }
+ }
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/indirect/DontUseUnsafe2.java b/langtools/test/tools/jdeps/VerboseFormat/use/indirect/DontUseUnsafe2.java
new file mode 100644
index 00000000000..4a89a5cfb53
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/indirect/DontUseUnsafe2.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package use.indirect;
+
+import use.unsafe.*;
+
+public class DontUseUnsafe2 {
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/indirect/UseUnsafeIndirectly.java b/langtools/test/tools/jdeps/VerboseFormat/use/indirect/UseUnsafeIndirectly.java
new file mode 100644
index 00000000000..add3363b5a7
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/indirect/UseUnsafeIndirectly.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package use.indirect;
+
+import use.unsafe.UseClassWithUnsafe;
+
+public class UseUnsafeIndirectly {
+ static UseClassWithUnsafe use = new UseClassWithUnsafe();
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/indirect2/DontUseUnsafe3.java b/langtools/test/tools/jdeps/VerboseFormat/use/indirect2/DontUseUnsafe3.java
new file mode 100644
index 00000000000..b8dbe276b9e
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/indirect2/DontUseUnsafe3.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package use.indirect2;
+
+import use.unsafe.*;
+
+public class DontUseUnsafe3 {
+
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/indirect2/UseUnsafeIndirectly2.java b/langtools/test/tools/jdeps/VerboseFormat/use/indirect2/UseUnsafeIndirectly2.java
new file mode 100644
index 00000000000..df21bd5737f
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/indirect2/UseUnsafeIndirectly2.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package use.indirect2;
+
+import use.unsafe.UseUnsafeClass;
+
+public class UseUnsafeIndirectly2 {
+ static UseUnsafeClass use = new UseUnsafeClass();
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/DontUseUnsafe.java b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/DontUseUnsafe.java
new file mode 100644
index 00000000000..d3a65ef1b7d
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/DontUseUnsafe.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package use.unsafe;
+
+public class DontUseUnsafe {
+
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseClassWithUnsafe.java b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseClassWithUnsafe.java
new file mode 100644
index 00000000000..f07787a1f58
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseClassWithUnsafe.java
@@ -0,0 +1,7 @@
+package use.unsafe;
+
+public class UseClassWithUnsafe {
+
+ static UseUnsafeClass use = new UseUnsafeClass();
+
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseUnsafeClass.java b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseUnsafeClass.java
new file mode 100644
index 00000000000..a57749acde4
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseUnsafeClass.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package use.unsafe;
+
+import sun.misc.Unsafe;
+
+public class UseUnsafeClass {
+ static Unsafe unsafe = Unsafe.getUnsafe();
+
+}
diff --git a/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseUnsafeClass2.java b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseUnsafeClass2.java
new file mode 100644
index 00000000000..fa4997768b9
--- /dev/null
+++ b/langtools/test/tools/jdeps/VerboseFormat/use/unsafe/UseUnsafeClass2.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package use.unsafe;
+
+import sun.misc.Unsafe;
+
+public class UseUnsafeClass2 {
+ static Unsafe unsafe = Unsafe.getUnsafe();
+
+}