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 package com.sun.tools.jdeps; 26 27 import com.sun.tools.classfile.ClassFile; 28 import com.sun.tools.classfile.Dependency.Location; 29 30 import java.io.IOException; 31 import java.io.UncheckedIOException; 32 import java.nio.file.Path; 33 import java.util.HashSet; 34 import java.util.Map; 35 import java.util.Set; 36 import java.util.concurrent.ConcurrentHashMap; 37 38 /** 39 * Represents the source of the class files. 40 */ 41 public 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 }