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

Thu, 12 Oct 2017 19:50:01 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:50:01 +0800
changeset 2702
9ca8d8713094
parent 2538
1e39ae45d8ac
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

merge

     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;
    27 import com.sun.tools.classfile.Dependency.Location;
    29 import java.io.IOException;
    30 import java.nio.file.Path;
    31 import java.util.HashSet;
    32 import java.util.Map;
    33 import java.util.Set;
    34 import java.util.concurrent.ConcurrentHashMap;
    36 /**
    37  * Represents the source of the class files.
    38  */
    39 public class Archive {
    40     public static Archive getInstance(Path p) throws IOException {
    41         return new Archive(p, ClassFileReader.newInstance(p));
    42     }
    44     private final Path path;
    45     private final String filename;
    46     private final ClassFileReader reader;
    47     protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>();
    49     protected Archive(String name) {
    50         this.path = null;
    51         this.filename = name;
    52         this.reader = null;
    53     }
    55     protected Archive(Path p, ClassFileReader reader) {
    56         this.path = p;
    57         this.filename = path.getFileName().toString();
    58         this.reader = reader;
    59     }
    61     public ClassFileReader reader() {
    62         return reader;
    63     }
    65     public String getName() {
    66         return filename;
    67     }
    69     public void addClass(Location origin) {
    70         Set<Location> set = deps.get(origin);
    71         if (set == null) {
    72             set = new HashSet<>();
    73             deps.put(origin, set);
    74         }
    75     }
    77     public void addClass(Location origin, Location target) {
    78         Set<Location> set = deps.get(origin);
    79         if (set == null) {
    80             set = new HashSet<>();
    81             deps.put(origin, set);
    82         }
    83         set.add(target);
    84     }
    86     public Set<Location> getClasses() {
    87         return deps.keySet();
    88     }
    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     }
    98     public boolean isEmpty() {
    99         return getClasses().isEmpty();
   100     }
   102     public String getPathName() {
   103         return path != null ? path.toString() : filename;
   104     }
   106     public String toString() {
   107         return filename;
   108     }
   110     interface Visitor {
   111         void visit(Location origin, Location target);
   112     }
   113 }

mercurial