1 /*
   2  * Copyright (c) 2012, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.tools.jdeps;
  26 
  27 import com.sun.tools.classfile.Dependency;
  28 import com.sun.tools.classfile.Dependency.Location;
  29 import java.io.File;
  30 import java.util.Comparator;
  31 import java.util.HashMap;
  32 import java.util.HashSet;
  33 import java.util.Map;
  34 import java.util.Set;
  35 import java.util.SortedMap;
  36 import java.util.SortedSet;
  37 import java.util.TreeMap;
  38 import java.util.TreeSet;
  39 
  40 /**
  41  * Represents the source of the class files.
  42  */
  43 public class Archive {
  44     private static Map<String, Archive> archiveForClass = new HashMap<>();
  45     public static Archive find(String classname) {
  46         return archiveForClass.get(classname);
  47     }
  48 
  49     private final File file;
  50     private final String filename;
  51     private final DependencyRecorder recorder;
  52     private final ClassFileReader reader;
  53     public Archive(File f, ClassFileReader reader) {
  54         this.file = f;
  55         this.filename = f != null ? f.getName() : "not found";
  56         this.recorder = new DependencyRecorder();
  57         this.reader = reader;
  58     }
  59 
  60     public ClassFileReader reader() {
  61         return reader;
  62     }
  63 
  64     public String getFileName() {
  65         return filename;
  66     }
  67 
  68     public void addClass(String cn) {
  69         Archive a = archiveForClass.get(cn);
  70         assert(a != null && a != this); // ## issue warning?
  71         if (!archiveForClass.containsKey(cn)) {
  72             archiveForClass.put(cn, this);
  73         }
  74     }
  75 
  76     public void addDependency(Dependency d) {
  77         recorder.addDependency(d);
  78     }
  79 
  80     public SortedMap<Location, SortedSet<Location>> getDependencies() {
  81         DependencyRecorder.Filter filter = new DependencyRecorder.Filter() {
  82             public boolean accept(Location origin, Location target) {
  83                  String o = origin.getClassName();
  84                  String t = target.getClassName();
  85                  return archiveForClass.get(o) != archiveForClass.get(t);
  86         }};
  87 
  88         SortedMap<Location, SortedSet<Location>> result = new TreeMap<>(locationComparator);
  89         for (Map.Entry<Location, Set<Location>> e : recorder.dependencies().entrySet()) {
  90             Location o = e.getKey();
  91             for (Location t : e.getValue()) {
  92                 if (filter.accept(o, t)) {
  93                     SortedSet<Location> odeps = result.get(o);
  94                     if (odeps == null) {
  95                         result.put(o, odeps = new TreeSet<>(locationComparator));
  96                     }
  97                     odeps.add(t);
  98                 }
  99             }
 100         }
 101         return result;
 102     }
 103 
 104     public Set<Archive> getRequiredArchives() {
 105         SortedSet<Archive> deps = new TreeSet<>(new Comparator<Archive>() {
 106             public int compare(Archive a1, Archive a2) {
 107                 return a1.toString().compareTo(a2.toString());
 108             }
 109         });
 110 
 111         for (Map.Entry<Location, Set<Location>> e : recorder.dependencies().entrySet()) {
 112             Location o = e.getKey();
 113             Archive origin = Archive.find(o.getClassName());
 114             for (Location t : e.getValue()) {
 115                 Archive target = Archive.find(t.getClassName());
 116                 if (origin != target) {
 117                     if (!deps.contains(target)) {
 118                         deps.add(target);
 119                     }
 120                 }
 121             }
 122         }
 123         return deps;
 124     }
 125 
 126     public String toString() {
 127         return file != null ? file.getPath() : "not found";
 128     }
 129 
 130     private static class DependencyRecorder {
 131         static interface Filter {
 132             boolean accept(Location origin, Location target);
 133         }
 134 
 135         public void addDependency(Dependency d) {
 136             Set<Location> odeps = map.get(d.getOrigin());
 137             if (odeps == null) {
 138                 map.put(d.getOrigin(), odeps = new HashSet<>());
 139             }
 140             odeps.add(d.getTarget());
 141         }
 142 
 143         public Map<Location, Set<Location>> dependencies() {
 144             return map;
 145         }
 146 
 147         private final Map<Location, Set<Location>> map = new HashMap<>();
 148     }
 149 
 150     private static Comparator<Location> locationComparator =
 151         new Comparator<Location>() {
 152             public int compare(Location o1, Location o2) {
 153                 return o1.toString().compareTo(o2.toString());
 154             }
 155         };
 156 }