8068937: jdeps shows "not found" if target class has no reference other than its own package
authormchung
Tue, 27 Jan 2015 19:50:41 -0800
changeset 28706 a724585645ce
parent 28705 675cb37e74a8
child 28707 2ec91c67b30b
8068937: jdeps shows "not found" if target class has no reference other than its own package Reviewed-by: alanb
langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Analyzer.java
langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Archive.java
langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/JdepsTask.java
langtools/test/tools/jdeps/Basic.java
langtools/test/tools/jdeps/p/C.java
langtools/test/tools/jdeps/p/SubClass.java
langtools/test/tools/jdeps/q/Gee.java
--- a/langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Analyzer.java	Tue Jan 27 15:11:57 2015 -0800
+++ b/langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Analyzer.java	Tue Jan 27 19:50:41 2015 -0800
@@ -218,7 +218,7 @@
             Archive targetArchive = findArchive(t);
             if (filter.accepts(o, archive, t, targetArchive)) {
                 addDep(o, t);
-                if (!requires.contains(targetArchive)) {
+                if (archive != targetArchive && !requires.contains(targetArchive)) {
                     requires.add(targetArchive);
                 }
             }
--- a/langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Archive.java	Tue Jan 27 15:11:57 2015 -0800
+++ b/langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Archive.java	Tue Jan 27 19:50:41 2015 -0800
@@ -75,20 +75,11 @@
     }
 
     public void addClass(Location origin) {
-        Set<Location> set = deps.get(origin);
-        if (set == null) {
-            set = new HashSet<>();
-            deps.put(origin, set);
-        }
+        deps.computeIfAbsent(origin, _k -> new HashSet<>());
     }
 
     public void addClass(Location origin, Location target) {
-        Set<Location> set = deps.get(origin);
-        if (set == null) {
-            set = new HashSet<>();
-            deps.put(origin, set);
-        }
-        set.add(target);
+        deps.computeIfAbsent(origin, _k -> new HashSet<>()).add(target);
     }
 
     public Set<Location> getClasses() {
@@ -115,6 +106,10 @@
         return filename;
     }
 
+    public Path path() {
+        return path;
+    }
+
     interface Visitor {
         void visit(Location origin, Location target);
     }
--- a/langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/JdepsTask.java	Tue Jan 27 15:11:57 2015 -0800
+++ b/langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/JdepsTask.java	Tue Jan 27 19:50:41 2015 -0800
@@ -611,6 +611,9 @@
                             deque.add(cn);
                         }
                         a.addClass(d.getOrigin(), d.getTarget());
+                    } else {
+                        // ensure that the parsed class is added the archive
+                        a.addClass(d.getOrigin());
                     }
                 }
                 for (String name : a.reader().skippedEntries()) {
@@ -643,6 +646,7 @@
                             // if name is a fully-qualified class name specified
                             // from command-line, this class might already be parsed
                             doneClasses.add(classFileName);
+
                             for (Dependency d : finder.findDependencies(cf)) {
                                 if (depth == 0) {
                                     // ignore the dependency
@@ -654,6 +658,9 @@
                                     if (!doneClasses.contains(cn) && !deque.contains(cn)) {
                                         deque.add(cn);
                                     }
+                                } else {
+                                    // ensure that the parsed class is added the archive
+                                    a.addClass(d.getOrigin());
                                 }
                             }
                         }
@@ -809,36 +816,53 @@
         }
     }
 
-    private List<Archive> getClassPathArchives(String paths) throws IOException {
+    /*
+     * Returns the list of Archive specified in cpaths and not included
+     * initialArchives
+     */
+    private List<Archive> getClassPathArchives(String cpaths)
+            throws IOException
+    {
         List<Archive> result = new ArrayList<>();
-        if (paths.isEmpty()) {
+        if (cpaths.isEmpty()) {
             return result;
         }
-        for (String p : paths.split(File.pathSeparator)) {
+        List<Path> paths = new ArrayList<>();
+        for (String p : cpaths.split(File.pathSeparator)) {
             if (p.length() > 0) {
-                List<Path> files = new ArrayList<>();
                 // wildcard to parse all JAR files e.g. -classpath dir/*
                 int i = p.lastIndexOf(".*");
                 if (i > 0) {
                     Path dir = Paths.get(p.substring(0, i));
                     try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
                         for (Path entry : stream) {
-                            files.add(entry);
+                            paths.add(entry);
                         }
                     }
                 } else {
-                    files.add(Paths.get(p));
-                }
-                for (Path f : files) {
-                    if (Files.exists(f)) {
-                        result.add(Archive.getInstance(f));
-                    }
+                    paths.add(Paths.get(p));
                 }
             }
         }
+        for (Path path : paths) {
+            boolean found = initialArchives.stream()
+                                           .map(Archive::path)
+                                           .anyMatch(p -> isSameFile(path, p));
+            if (!found && Files.exists(path)) {
+                result.add(Archive.getInstance(path));
+            }
+        }
         return result;
     }
 
+    private boolean isSameFile(Path p1, Path p2) {
+        try {
+            return Files.isSameFile(p1, p2);
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
     class RawOutputFormatter implements Analyzer.Visitor {
         private final PrintWriter writer;
         private String pkg = "";
--- a/langtools/test/tools/jdeps/Basic.java	Tue Jan 27 15:11:57 2015 -0800
+++ b/langtools/test/tools/jdeps/Basic.java	Tue Jan 27 19:50:41 2015 -0800
@@ -23,9 +23,9 @@
 
 /*
  * @test
- * @bug 8003562 8005428 8015912 8027481 8048063
+ * @bug 8003562 8005428 8015912 8027481 8048063 8068937
  * @summary Basic tests for jdeps tool
- * @build Test p.Foo p.Bar javax.activity.NotCompactProfile
+ * @build Test p.Foo p.Bar p.C p.SubClass q.Gee javax.activity.NotCompactProfile
  * @run main Basic
  */
 
@@ -90,6 +90,18 @@
              new String[] {"compact1"},
              new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});
 
+        // parse p.C, p.SubClass and q.*
+        // p.SubClass have no dependency other than p.C
+        // q.Gee depends on p.SubClass that should be found
+        test(testDir,
+             new String[] {"java.lang", "p"},
+             new String[] {"compact1", testDir.getName()},
+             new String[] {"-include", "p.C|p.SubClass|q\\..*"});
+        test(testDir,
+             new String[] {"java.lang", "p"},
+             new String[] {"compact1", testDir.getName()},
+             new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"});
+
         // test -classpath and -include options
         test(null,
              new String[] {"java.lang", "java.util", "java.lang.management",
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/jdeps/p/C.java	Tue Jan 27 19:50:41 2015 -0800
@@ -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 p;
+
+public class C {
+    public String name() {
+        return "C";
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/jdeps/p/SubClass.java	Tue Jan 27 19:50:41 2015 -0800
@@ -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 p;
+
+// SubClass only references types in package p
+public class SubClass extends C {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/jdeps/q/Gee.java	Tue Jan 27 19:50:41 2015 -0800
@@ -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 q;
+
+public class Gee extends p.SubClass {
+}