Archive.java revision 3170:dc017a37aac5
1/*
2 * Copyright (c) 2012, 2014, 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
26package com.sun.tools.jdeps;
27
28import com.sun.tools.classfile.ClassFile;
29import com.sun.tools.classfile.Dependency.Location;
30
31import java.io.IOException;
32import java.io.UncheckedIOException;
33import java.nio.file.Path;
34import java.util.HashSet;
35import java.util.Map;
36import java.util.Set;
37import java.util.concurrent.ConcurrentHashMap;
38
39/**
40 * Represents the source of the class files.
41 */
42public class Archive {
43    public static Archive getInstance(Path p) {
44        try {
45            return new Archive(p, ClassFileReader.newInstance(p));
46        } catch (IOException e) {
47            throw new UncheckedIOException(e);
48        }
49    }
50
51    private final Path path;
52    private final String filename;
53    private final ClassFileReader reader;
54    protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>();
55
56    protected Archive(String name) {
57        this(name, null);
58    }
59    protected Archive(String name, ClassFileReader reader) {
60        this.path = null;
61        this.filename = name;
62        this.reader = reader;
63    }
64    protected Archive(Path p, ClassFileReader reader) {
65        this.path = p;
66        this.filename = path.getFileName().toString();
67        this.reader = reader;
68    }
69
70    public ClassFileReader reader() {
71        return reader;
72    }
73
74    public String getName() {
75        return filename;
76    }
77
78    public void addClass(Location origin) {
79        deps.computeIfAbsent(origin, _k -> new HashSet<>());
80    }
81
82    public void addClass(Location origin, Location target) {
83        deps.computeIfAbsent(origin, _k -> new HashSet<>()).add(target);
84    }
85
86    public Set<Location> getClasses() {
87        return deps.keySet();
88    }
89
90    public void visitDependences(Visitor v) {
91        for (Map.Entry<Location,Set<Location>> e: deps.entrySet()) {
92            for (Location target : e.getValue()) {
93                v.visit(e.getKey(), target);
94            }
95        }
96    }
97
98    public boolean isEmpty() {
99        return getClasses().isEmpty();
100    }
101
102    public String getPathName() {
103        return path != null ? path.toString() : filename;
104    }
105
106    public String toString() {
107        return filename;
108    }
109
110    public Path path() {
111        return path;
112    }
113
114    interface Visitor {
115        void visit(Location origin, Location target);
116    }
117}
118