Archive.java revision 2942:08092deced3f
1140086Sdas/*
2140086Sdas * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3140086Sdas * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4140086Sdas *
5140086Sdas * This code is free software; you can redistribute it and/or modify it
6140086Sdas * under the terms of the GNU General Public License version 2 only, as
7140086Sdas * published by the Free Software Foundation.  Oracle designates this
8140086Sdas * particular file as subject to the "Classpath" exception as provided
9140086Sdas * by Oracle in the LICENSE file that accompanied this code.
10140086Sdas *
11140086Sdas * This code is distributed in the hope that it will be useful, but WITHOUT
12140086Sdas * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13140086Sdas * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14140086Sdas * version 2 for more details (a copy is included in the LICENSE file that
15140086Sdas * accompanied this code).
16140086Sdas *
17140086Sdas * You should have received a copy of the GNU General Public License version
18140086Sdas * 2 along with this work; if not, write to the Free Software Foundation,
19140086Sdas * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20140086Sdas *
21140086Sdas * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22140086Sdas * or visit www.oracle.com if you need additional information or have any
23140086Sdas * questions.
24140086Sdas */
25140086Sdaspackage com.sun.tools.jdeps;
26140086Sdas
27140086Sdasimport com.sun.tools.classfile.ClassFile;
28140086Sdasimport com.sun.tools.classfile.Dependency.Location;
29140086Sdas
30140086Sdasimport java.io.IOException;
31140086Sdasimport java.io.UncheckedIOException;
32140086Sdasimport java.nio.file.Path;
33140086Sdasimport java.util.HashSet;
34140086Sdasimport java.util.Map;
35140086Sdasimport java.util.Set;
36140086Sdasimport java.util.concurrent.ConcurrentHashMap;
37192760Sattilio
38217108Skib/**
39217108Skib * Represents the source of the class files.
40 */
41public class Archive {
42    public static Archive getInstance(Path p) {
43        try {
44            return new Archive(p, ClassFileReader.newInstance(p));
45        } catch (IOException e) {
46            throw new UncheckedIOException(e);
47        }
48    }
49
50    private final Path path;
51    private final String filename;
52    private final ClassFileReader reader;
53    protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>();
54
55    protected Archive(String name) {
56        this(name, null);
57    }
58    protected Archive(String name, ClassFileReader reader) {
59        this.path = null;
60        this.filename = name;
61        this.reader = reader;
62    }
63    protected Archive(Path p, ClassFileReader reader) {
64        this.path = p;
65        this.filename = path.getFileName().toString();
66        this.reader = reader;
67    }
68
69    public ClassFileReader reader() {
70        return reader;
71    }
72
73    public String getName() {
74        return filename;
75    }
76
77    public void addClass(Location origin) {
78        deps.computeIfAbsent(origin, _k -> new HashSet<>());
79    }
80
81    public void addClass(Location origin, Location target) {
82        deps.computeIfAbsent(origin, _k -> new HashSet<>()).add(target);
83    }
84
85    public Set<Location> getClasses() {
86        return deps.keySet();
87    }
88
89    public void visitDependences(Visitor v) {
90        for (Map.Entry<Location,Set<Location>> e: deps.entrySet()) {
91            for (Location target : e.getValue()) {
92                v.visit(e.getKey(), target);
93            }
94        }
95    }
96
97    public boolean isEmpty() {
98        return getClasses().isEmpty();
99    }
100
101    public String getPathName() {
102        return path != null ? path.toString() : filename;
103    }
104
105    public String toString() {
106        return filename;
107    }
108
109    public Path path() {
110        return path;
111    }
112
113    interface Visitor {
114        void visit(Location origin, Location target);
115    }
116}
117