src/share/classes/com/sun/tools/jdeps/Archive.java

changeset 1472
0c244701188e
child 1577
88286a36bb34
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/jdeps/Archive.java	Fri Dec 28 22:25:21 2012 -0800
     1.3 @@ -0,0 +1,173 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.jdeps;
    1.29 +
    1.30 +import com.sun.tools.classfile.Dependency;
    1.31 +import com.sun.tools.classfile.Dependency.Location;
    1.32 +import java.io.File;
    1.33 +import java.util.Comparator;
    1.34 +import java.util.HashMap;
    1.35 +import java.util.HashSet;
    1.36 +import java.util.Map;
    1.37 +import java.util.Set;
    1.38 +import java.util.SortedMap;
    1.39 +import java.util.SortedSet;
    1.40 +import java.util.TreeMap;
    1.41 +import java.util.TreeSet;
    1.42 +
    1.43 +/**
    1.44 + * Represents the source of the class files.
    1.45 + */
    1.46 +public class Archive {
    1.47 +    private static Map<String,Archive> archiveForClass = new HashMap<String,Archive>();
    1.48 +    public static Archive find(Location loc) {
    1.49 +        return archiveForClass.get(loc.getName());
    1.50 +    }
    1.51 +
    1.52 +    private final File file;
    1.53 +    private final String filename;
    1.54 +    private final DependencyRecorder recorder;
    1.55 +    private final ClassFileReader reader;
    1.56 +    public Archive(String name) {
    1.57 +        this.file = null;
    1.58 +        this.filename = name;
    1.59 +        this.recorder = new DependencyRecorder();
    1.60 +        this.reader = null;
    1.61 +    }
    1.62 +
    1.63 +    public Archive(File f, ClassFileReader reader) {
    1.64 +        this.file = f;
    1.65 +        this.filename = f.getName();
    1.66 +        this.recorder = new DependencyRecorder();
    1.67 +        this.reader = reader;
    1.68 +    }
    1.69 +
    1.70 +    public ClassFileReader reader() {
    1.71 +        return reader;
    1.72 +    }
    1.73 +
    1.74 +    public String getFileName() {
    1.75 +        return filename;
    1.76 +    }
    1.77 +
    1.78 +    public void addClass(String classFileName) {
    1.79 +        Archive a = archiveForClass.get(classFileName);
    1.80 +        assert(a == null || a == this); // ## issue warning?
    1.81 +        if (!archiveForClass.containsKey(classFileName)) {
    1.82 +            archiveForClass.put(classFileName, this);
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    public void addDependency(Dependency d) {
    1.87 +        recorder.addDependency(d);
    1.88 +    }
    1.89 +
    1.90 +    /**
    1.91 +     * Returns a sorted map of a class to its dependencies.
    1.92 +     */
    1.93 +    public SortedMap<Location, SortedSet<Location>> getDependencies() {
    1.94 +        DependencyRecorder.Filter filter = new DependencyRecorder.Filter() {
    1.95 +            public boolean accept(Location origin, Location target) {
    1.96 +                 return (archiveForClass.get(origin.getName()) !=
    1.97 +                            archiveForClass.get(target.getName()));
    1.98 +        }};
    1.99 +
   1.100 +        SortedMap<Location, SortedSet<Location>> result =
   1.101 +            new TreeMap<Location, SortedSet<Location>>(locationComparator);
   1.102 +        for (Map.Entry<Location, Set<Location>> e : recorder.dependencies().entrySet()) {
   1.103 +            Location o = e.getKey();
   1.104 +            for (Location t : e.getValue()) {
   1.105 +                if (filter.accept(o, t)) {
   1.106 +                    SortedSet<Location> odeps = result.get(o);
   1.107 +                    if (odeps == null) {
   1.108 +                        odeps = new TreeSet<Location>(locationComparator);
   1.109 +                        result.put(o, odeps);
   1.110 +                    }
   1.111 +                    odeps.add(t);
   1.112 +                }
   1.113 +            }
   1.114 +        }
   1.115 +        return result;
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Returns the set of archives this archive requires.
   1.120 +     */
   1.121 +    public Set<Archive> getRequiredArchives() {
   1.122 +        SortedSet<Archive> deps = new TreeSet<Archive>(new Comparator<Archive>() {
   1.123 +            public int compare(Archive a1, Archive a2) {
   1.124 +                return a1.toString().compareTo(a2.toString());
   1.125 +            }
   1.126 +        });
   1.127 +
   1.128 +        for (Map.Entry<Location, Set<Location>> e : recorder.dependencies().entrySet()) {
   1.129 +            Location o = e.getKey();
   1.130 +            Archive origin = Archive.find(o);
   1.131 +            for (Location t : e.getValue()) {
   1.132 +                Archive target = Archive.find(t);
   1.133 +                assert(origin != null && target != null);
   1.134 +                if (origin != target) {
   1.135 +                    if (!deps.contains(target)) {
   1.136 +                        deps.add(target);
   1.137 +                    }
   1.138 +                }
   1.139 +            }
   1.140 +        }
   1.141 +        return deps;
   1.142 +    }
   1.143 +
   1.144 +    public String toString() {
   1.145 +        return file != null ? file.getPath() : filename;
   1.146 +    }
   1.147 +
   1.148 +    private static class DependencyRecorder {
   1.149 +        static interface Filter {
   1.150 +            boolean accept(Location origin, Location target);
   1.151 +        }
   1.152 +
   1.153 +        public void addDependency(Dependency d) {
   1.154 +            Set<Location> odeps = map.get(d.getOrigin());
   1.155 +            if (odeps == null) {
   1.156 +                odeps = new HashSet<Location>();
   1.157 +                map.put(d.getOrigin(), odeps);
   1.158 +            }
   1.159 +            odeps.add(d.getTarget());
   1.160 +        }
   1.161 +
   1.162 +        public Map<Location, Set<Location>> dependencies() {
   1.163 +            return map;
   1.164 +        }
   1.165 +
   1.166 +        private final Map<Location, Set<Location>> map =
   1.167 +            new HashMap<Location, Set<Location>>();
   1.168 +    }
   1.169 +
   1.170 +    private static Comparator<Location> locationComparator =
   1.171 +        new Comparator<Location>() {
   1.172 +            public int compare(Location o1, Location o2) {
   1.173 +                return o1.toString().compareTo(o2.toString());
   1.174 +            }
   1.175 +        };
   1.176 +}

mercurial