Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Archive.java
41161 views
/*1* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdeps;2627import com.sun.tools.classfile.Dependency.Location;2829import java.io.Closeable;30import java.io.IOException;31import java.io.UncheckedIOException;32import java.net.URI;33import java.nio.file.Files;34import java.nio.file.Path;35import java.nio.file.Paths;36import java.util.HashSet;37import java.util.Map;38import java.util.Objects;39import java.util.Optional;40import java.util.Set;41import java.util.concurrent.ConcurrentHashMap;42import java.util.stream.Stream;4344import static com.sun.tools.jdeps.Module.trace;4546/**47* Represents the source of the class files.48*/49public class Archive implements Closeable {50public static Archive getInstance(Path p, Runtime.Version version) {51try {52return new Archive(p, ClassFileReader.newInstance(p, version));53} catch (IOException e) {54throw new UncheckedIOException(e);55}56}5758private final URI location;59private final Path path;60private final String filename;61private final ClassFileReader reader;6263protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>();6465protected Archive(String name) {66this(name, null, null);67}68protected Archive(String name, URI location, ClassFileReader reader) {69this.location = location;70this.path = location != null ? Paths.get(location) : null;71this.filename = name;72this.reader = reader;73}74protected Archive(Path p, ClassFileReader reader) {75this.location = null;76this.path = p;77this.filename = path.getFileName().toString();78this.reader = reader;79}8081public ClassFileReader reader() {82return reader;83}8485public String getName() {86return filename;87}8889public Module getModule() {90return Module.UNNAMED_MODULE;91}9293public boolean contains(String entry) {94return reader.entries().contains(entry);95}9697public void addClass(Location origin) {98deps.computeIfAbsent(origin, _k -> new HashSet<>());99}100101public void addClass(Location origin, Location target) {102deps.computeIfAbsent(origin, _k -> new HashSet<>()).add(target);103}104105public Set<Location> getClasses() {106return deps.keySet();107}108109public Stream<Location> getDependencies() {110return deps.values().stream()111.flatMap(Set::stream);112}113114public boolean hasDependences() {115return getDependencies().count() > 0;116}117118public void visitDependences(Visitor v) {119for (Map.Entry<Location,Set<Location>> e: deps.entrySet()) {120for (Location target : e.getValue()) {121v.visit(e.getKey(), target);122}123}124}125126/**127* Tests if any class has been parsed.128*/129public boolean isEmpty() {130return getClasses().isEmpty();131}132133public String getPathName() {134return path != null ? path.toString() : filename;135}136137public Optional<Path> path() {138return Optional.ofNullable(path);139}140141@Override142public int hashCode() {143return Objects.hash(this.filename, this.path);144}145146@Override147public boolean equals(Object o) {148if (o instanceof Archive) {149Archive other = (Archive)o;150if (path == other.path || isSameLocation(this, other))151return true;152}153return false;154}155156@Override157public String toString() {158return filename;159}160161162public static boolean isSameLocation(Archive archive, Archive other) {163if (archive.path == null || other.path == null)164return false;165166if (archive.location != null && other.location != null &&167archive.location.equals(other.location)) {168return true;169}170171if (archive.isJrt() || other.isJrt()) {172return false;173}174175try {176return Files.isSameFile(archive.path, other.path);177} catch (IOException e) {178throw new UncheckedIOException(e);179}180}181182private boolean isJrt() {183return location != null && location.getScheme().equals("jrt");184}185186@Override187public void close() throws IOException {188trace("closing %s %n", getPathName());189if (reader != null)190reader.close();191}192193interface Visitor {194void visit(Location origin, Location target);195}196}197198199