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

Fri, 18 Jul 2014 10:43:41 -0700

author
mchung
date
Fri, 18 Jul 2014 10:43:41 -0700
changeset 2539
a51b7fd0543b
parent 2538
1e39ae45d8ac
child 2702
9ca8d8713094
permissions
-rw-r--r--

8050804: (jdeps) Recommend supported API to replace use of JDK internal API
Reviewed-by: dfuchs

mchung@1472 1 /*
mchung@2538 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
mchung@1472 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mchung@1472 4 *
mchung@1472 5 * This code is free software; you can redistribute it and/or modify it
mchung@1472 6 * under the terms of the GNU General Public License version 2 only, as
mchung@1472 7 * published by the Free Software Foundation. Oracle designates this
mchung@1472 8 * particular file as subject to the "Classpath" exception as provided
mchung@1472 9 * by Oracle in the LICENSE file that accompanied this code.
mchung@1472 10 *
mchung@1472 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mchung@1472 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mchung@1472 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mchung@1472 14 * version 2 for more details (a copy is included in the LICENSE file that
mchung@1472 15 * accompanied this code).
mchung@1472 16 *
mchung@1472 17 * You should have received a copy of the GNU General Public License version
mchung@1472 18 * 2 along with this work; if not, write to the Free Software Foundation,
mchung@1472 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mchung@1472 20 *
mchung@1472 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mchung@1472 22 * or visit www.oracle.com if you need additional information or have any
mchung@1472 23 * questions.
mchung@1472 24 */
mchung@1472 25 package com.sun.tools.jdeps;
mchung@1472 26
mchung@1472 27 import com.sun.tools.classfile.Dependency.Location;
mchung@2538 28
mchung@2538 29 import java.io.IOException;
mchung@2139 30 import java.nio.file.Path;
mchung@1472 31 import java.util.HashSet;
mchung@1472 32 import java.util.Map;
mchung@1472 33 import java.util.Set;
mchung@2538 34 import java.util.concurrent.ConcurrentHashMap;
mchung@1472 35
mchung@1472 36 /**
mchung@1472 37 * Represents the source of the class files.
mchung@1472 38 */
mchung@1472 39 public class Archive {
mchung@2538 40 public static Archive getInstance(Path p) throws IOException {
mchung@2538 41 return new Archive(p, ClassFileReader.newInstance(p));
mchung@2538 42 }
mchung@2538 43
mchung@2139 44 private final Path path;
mchung@1472 45 private final String filename;
mchung@1472 46 private final ClassFileReader reader;
mchung@2538 47 protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>();
mchung@1577 48
mchung@2538 49 protected Archive(String name) {
mchung@2139 50 this.path = null;
mchung@1472 51 this.filename = name;
mchung@1472 52 this.reader = null;
mchung@1472 53 }
mchung@1472 54
mchung@2538 55 protected Archive(Path p, ClassFileReader reader) {
mchung@2139 56 this.path = p;
mchung@2139 57 this.filename = path.getFileName().toString();
mchung@1472 58 this.reader = reader;
mchung@1472 59 }
mchung@1472 60
mchung@1472 61 public ClassFileReader reader() {
mchung@1472 62 return reader;
mchung@1472 63 }
mchung@1472 64
mchung@2538 65 public String getName() {
mchung@1472 66 return filename;
mchung@1472 67 }
mchung@1472 68
mchung@1577 69 public void addClass(Location origin) {
mchung@1577 70 Set<Location> set = deps.get(origin);
mchung@1577 71 if (set == null) {
mchung@2139 72 set = new HashSet<>();
mchung@1577 73 deps.put(origin, set);
mchung@1472 74 }
mchung@1472 75 }
mchung@2172 76
mchung@1577 77 public void addClass(Location origin, Location target) {
mchung@1577 78 Set<Location> set = deps.get(origin);
mchung@1577 79 if (set == null) {
mchung@2139 80 set = new HashSet<>();
mchung@1577 81 deps.put(origin, set);
mchung@1577 82 }
mchung@1577 83 set.add(target);
mchung@1472 84 }
mchung@1472 85
mchung@2172 86 public Set<Location> getClasses() {
mchung@2172 87 return deps.keySet();
mchung@2172 88 }
mchung@2172 89
mchung@2172 90 public void visitDependences(Visitor v) {
mchung@1577 91 for (Map.Entry<Location,Set<Location>> e: deps.entrySet()) {
mchung@1577 92 for (Location target : e.getValue()) {
mchung@1577 93 v.visit(e.getKey(), target);
mchung@1472 94 }
mchung@1472 95 }
mchung@1472 96 }
mchung@1472 97
mchung@2538 98 public boolean isEmpty() {
mchung@2538 99 return getClasses().isEmpty();
mchung@2538 100 }
mchung@2538 101
mchung@2172 102 public String getPathName() {
mchung@2139 103 return path != null ? path.toString() : filename;
mchung@1472 104 }
mchung@1472 105
mchung@2172 106 public String toString() {
mchung@2172 107 return filename;
mchung@2172 108 }
mchung@2172 109
mchung@1577 110 interface Visitor {
mchung@1577 111 void visit(Location origin, Location target);
mchung@1472 112 }
mchung@1472 113 }

mercurial