changeset 2843:6b43535fb9f8

8068937: jdeps shows "not found" if target class has no reference other than its own package Reviewed-by: alanb
author mchung
date Wed, 20 May 2015 17:11:28 -0700
parents 31ceef045272
children 9538418d25b9
files src/share/classes/com/sun/tools/jdeps/Analyzer.java src/share/classes/com/sun/tools/jdeps/JdepsTask.java test/tools/jdeps/Basic.java test/tools/jdeps/p/C.java test/tools/jdeps/p/SubClass.java test/tools/jdeps/q/Gee.java
diffstat 6 files changed, 137 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/jdeps/Analyzer.java	Mon May 18 09:27:09 2015 +0200
+++ b/src/share/classes/com/sun/tools/jdeps/Analyzer.java	Wed May 20 17:11:28 2015 -0700
@@ -223,7 +223,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/src/share/classes/com/sun/tools/jdeps/JdepsTask.java	Mon May 18 09:27:09 2015 +0200
+++ b/src/share/classes/com/sun/tools/jdeps/JdepsTask.java	Wed May 20 17:11:28 2015 -0700
@@ -489,9 +489,11 @@
 
         List<Archive> archives = new ArrayList<>();
         Deque<String> roots = new LinkedList<>();
+        List<Path> paths = new ArrayList<>();
         for (String s : classes) {
             Path p = Paths.get(s);
             if (Files.exists(p)) {
+                paths.add(p);
                 archives.add(Archive.getInstance(p));
             } else {
                 if (isValidClassName(s)) {
@@ -504,7 +506,7 @@
         sourceLocations.addAll(archives);
 
         List<Archive> classpaths = new ArrayList<>(); // for class file lookup
-        classpaths.addAll(getClassPathArchives(options.classpath));
+        classpaths.addAll(getClassPathArchives(options.classpath, paths));
         if (options.includePattern != null) {
             archives.addAll(classpaths);
         }
@@ -545,6 +547,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()) {
@@ -592,6 +597,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());
                                 }
                             }
                         }
@@ -743,36 +751,52 @@
         }
     }
 
-    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, List<Path> initialArchives)
+            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 p : paths) {
+            if (Files.exists(p) && !hasSameFile(initialArchives, p)) {
+                result.add(Archive.getInstance(p));
+            }
+        }
         return result;
     }
 
+    private boolean hasSameFile(List<Path> paths, Path p2) throws IOException {
+        for (Path p1 : paths) {
+            if (Files.isSameFile(p1, p2)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     class RawOutputFormatter implements Analyzer.Visitor {
         private final PrintWriter writer;
         private String pkg = "";
--- a/test/tools/jdeps/Basic.java	Mon May 18 09:27:09 2015 +0200
+++ b/test/tools/jdeps/Basic.java	Wed May 20 17:11:28 2015 -0700
@@ -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
  */
 
@@ -111,6 +111,19 @@
              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/test/tools/jdeps/p/C.java	Wed May 20 17:11:28 2015 -0700
@@ -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/test/tools/jdeps/p/SubClass.java	Wed May 20 17:11:28 2015 -0700
@@ -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/test/tools/jdeps/q/Gee.java	Wed May 20 17:11:28 2015 -0700
@@ -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 {
+}