src/share/classes/com/sun/tools/sjavac/comp/Dependencies.java

Mon, 25 Mar 2013 16:55:14 -0700

author
mfang
date
Mon, 25 Mar 2013 16:55:14 -0700
changeset 1658
fdf30b225e1c
parent 1588
2620c953e9fe
child 1648
a03c4a86ea2b
permissions
-rw-r--r--

8010521: jdk8 l10n resource file translation update 2
Reviewed-by: naoto, yhuang

     1 /*
     2  * Copyright (c) 1999, 2013, 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  */
    26 package com.sun.tools.sjavac.comp;
    28 import javax.lang.model.element.Element;
    29 import java.util.Arrays;
    30 import java.util.Comparator;
    31 import java.util.HashMap;
    32 import java.util.HashSet;
    33 import java.util.Map;
    34 import java.util.Set;
    36 import com.sun.tools.javac.code.Symbol.ClassSymbol;
    37 import com.sun.tools.javac.util.Context;
    38 import com.sun.tools.javac.util.Log;
    39 import com.sun.tools.javac.util.Name;
    41 /** Utility class containing dependency information between packages
    42  *  and the pubapi for a package.
    43  *
    44  * <p><b>This is NOT part of any supported API.
    45  * If you write code that depends on this, you do so at your own
    46  * risk.  This code and its internal interfaces are subject to change
    47  * or deletion without notice.</b></p>
    48  */
    49 public class Dependencies {
    50     protected static final Context.Key<Dependencies> dependenciesKey =
    51         new Context.Key<Dependencies>();
    53     // The log to be used for error reporting.
    54     protected Log log;
    55     // Map from package name to packages that the package depends upon.
    56     protected Map<Name,Set<Name>> deps;
    57     // This is the set of all packages that are supplied
    58     // through the java files at the command line.
    59     protected Set<Name> explicitPackages;
    61     // Map from a package name to its public api.
    62     // Will the Name encode the module in the future?
    63     // If not, this will have to change to map from Module+Name to public api.
    64     protected Map<Name,StringBuffer> publicApiPerClass;
    66     public static Dependencies instance(Context context) {
    67         Dependencies instance = context.get(dependenciesKey);
    68         if (instance == null)
    69             instance = new Dependencies(context);
    70         return instance;
    71     }
    73     private Dependencies(Context context) {
    74         context.put(dependenciesKey, this);
    75         log = Log.instance(context);
    76     }
    78     public void reset()
    79     {
    80         deps = new HashMap<Name, Set<Name>>();
    81         explicitPackages = new HashSet<Name>();
    82         publicApiPerClass = new HashMap<Name,StringBuffer>();
    83     }
    85     /**
    86      * Fetch the set of dependencies that are relevant to the compile
    87      * that has just been performed. I.e. we are only interested in
    88      * dependencies for classes that were explicitly compiled.
    89      * @return
    90      */
    91     public Map<String,Set<String>> getDependencies() {
    92         Map<String,Set<String>> new_deps = new HashMap<String,Set<String>>();
    93         if (explicitPackages == null) return new_deps;
    94         for (Name pkg : explicitPackages) {
    95             Set<Name> set = deps.get(pkg);
    96             if (set != null) {
    97                 Set<String> new_set = new_deps.get(pkg.toString());
    98                 if (new_set == null) {
    99                     new_set = new HashSet<String>();
   100                     // Modules beware....
   101                     new_deps.put(":"+pkg.toString(), new_set);
   102                 }
   103                 for (Name d : set) {
   104                     new_set.add(":"+d.toString());
   105                 }
   106             }
   107         }
   108         return new_deps;
   109     }
   111     class CompareNames implements Comparator<Name> {
   112          public int compare(Name a, Name b) {
   113              return a.toString().compareTo(b.toString());
   114          }
   116     }
   118     /**
   119      * Convert the map from class names to their pubapi to a map
   120      * from package names to their pubapi (which is the sorted concatenation
   121      * of all the class pubapis)
   122      */
   123     public Map<String,String> getPubapis() {
   124         Map<String,String> publicApiPerPackage = new HashMap<String,String>();
   125         if (publicApiPerClass == null) return publicApiPerPackage;
   126         Name[] keys = publicApiPerClass.keySet().toArray(new Name[0]);
   127         Arrays.sort(keys, new CompareNames());
   128         StringBuffer newPublicApi = new StringBuffer();
   129         int i=0;
   130         String prevPkg = "";
   131         for (Name k : keys) {
   132             String cn = k.toString();
   133             String pn = "";
   134             int dp = cn.lastIndexOf('.');
   135             if (dp != -1) {
   136                 pn = cn.substring(0,dp);
   137             }
   138             if (!pn.equals(prevPkg)) {
   139                 if (!prevPkg.equals("")) {
   140                     // Add default module name ":"
   141                     publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
   142                 }
   143                 newPublicApi = new StringBuffer();
   144                 prevPkg = pn;
   145             }
   146             newPublicApi.append(publicApiPerClass.get(k));
   147             i++;
   148         }
   149         if (!prevPkg.equals(""))
   150             publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
   151         return publicApiPerPackage;
   152     }
   154     /**
   155      * Visit the api of a class and construct a pubapi string and
   156      * store it into the pubapi_perclass map.
   157      */
   158     public void visitPubapi(Element e) {
   159         Name n = ((ClassSymbol)e).fullname;
   160         Name p = ((ClassSymbol)e).packge().fullname;
   161         StringBuffer sb = publicApiPerClass.get(n);
   162         assert(sb == null);
   163         sb = new StringBuffer();
   164         PubapiVisitor v = new PubapiVisitor(sb);
   165         v.visit(e);
   166         if (sb.length()>0) {
   167             publicApiPerClass.put(n, sb);
   168         }
   169         explicitPackages.add(p);
   170      }
   172     /**
   173      * Collect a dependency. curr_pkg is marked as depending on dep_pkg.
   174      */
   175     public void collect(Name currPkg, Name depPkg) {
   176         if (!currPkg.equals(depPkg)) {
   177             Set<Name> theset = deps.get(currPkg);
   178             if (theset==null) {
   179                 theset = new HashSet<Name>();
   180                 deps.put(currPkg, theset);
   181             }
   182             theset.add(depPkg);
   183         }
   184     }
   185 }

mercurial