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

Mon, 18 Feb 2013 14:33:25 +0000

author
vromero
date
Mon, 18 Feb 2013 14:33:25 +0000
changeset 1588
2620c953e9fe
parent 1504
22e417cdddee
child 1648
a03c4a86ea2b
permissions
-rw-r--r--

6563143: javac should issue a warning for overriding equals without hashCode
Reviewed-by: jjg, mcimadamore

ohrstrom@1504 1 /*
vromero@1588 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504 4 *
ohrstrom@1504 5 * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504 6 * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504 7 * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504 8 * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504 9 * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504 10 *
ohrstrom@1504 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504 14 * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504 15 * accompanied this code).
ohrstrom@1504 16 *
ohrstrom@1504 17 * You should have received a copy of the GNU General Public License version
ohrstrom@1504 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504 20 *
ohrstrom@1504 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504 22 * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504 23 * questions.
ohrstrom@1504 24 */
ohrstrom@1504 25
ohrstrom@1504 26 package com.sun.tools.sjavac.comp;
ohrstrom@1504 27
ohrstrom@1504 28 import javax.lang.model.element.Element;
ohrstrom@1504 29 import java.util.Arrays;
ohrstrom@1504 30 import java.util.Comparator;
ohrstrom@1504 31 import java.util.HashMap;
ohrstrom@1504 32 import java.util.HashSet;
ohrstrom@1504 33 import java.util.Map;
ohrstrom@1504 34 import java.util.Set;
ohrstrom@1504 35
ohrstrom@1504 36 import com.sun.tools.javac.code.Symbol.ClassSymbol;
ohrstrom@1504 37 import com.sun.tools.javac.util.Context;
ohrstrom@1504 38 import com.sun.tools.javac.util.Log;
ohrstrom@1504 39 import com.sun.tools.javac.util.Name;
ohrstrom@1504 40
ohrstrom@1504 41 /** Utility class containing dependency information between packages
ohrstrom@1504 42 * and the pubapi for a package.
ohrstrom@1504 43 *
ohrstrom@1504 44 * <p><b>This is NOT part of any supported API.
ohrstrom@1504 45 * If you write code that depends on this, you do so at your own
ohrstrom@1504 46 * risk. This code and its internal interfaces are subject to change
ohrstrom@1504 47 * or deletion without notice.</b></p>
ohrstrom@1504 48 */
ohrstrom@1504 49 public class Dependencies {
ohrstrom@1504 50 protected static final Context.Key<Dependencies> dependenciesKey =
ohrstrom@1504 51 new Context.Key<Dependencies>();
ohrstrom@1504 52
ohrstrom@1504 53 // The log to be used for error reporting.
ohrstrom@1504 54 protected Log log;
ohrstrom@1504 55 // Map from package name to packages that the package depends upon.
ohrstrom@1504 56 protected Map<Name,Set<Name>> deps;
ohrstrom@1504 57 // This is the set of all packages that are supplied
ohrstrom@1504 58 // through the java files at the command line.
ohrstrom@1504 59 protected Set<Name> explicitPackages;
ohrstrom@1504 60
ohrstrom@1504 61 // Map from a package name to its public api.
ohrstrom@1504 62 // Will the Name encode the module in the future?
ohrstrom@1504 63 // If not, this will have to change to map from Module+Name to public api.
ohrstrom@1504 64 protected Map<Name,StringBuffer> publicApiPerClass;
ohrstrom@1504 65
ohrstrom@1504 66 public static Dependencies instance(Context context) {
ohrstrom@1504 67 Dependencies instance = context.get(dependenciesKey);
ohrstrom@1504 68 if (instance == null)
ohrstrom@1504 69 instance = new Dependencies(context);
ohrstrom@1504 70 return instance;
ohrstrom@1504 71 }
ohrstrom@1504 72
ohrstrom@1504 73 private Dependencies(Context context) {
ohrstrom@1504 74 context.put(dependenciesKey, this);
ohrstrom@1504 75 log = Log.instance(context);
ohrstrom@1504 76 }
ohrstrom@1504 77
ohrstrom@1504 78 public void reset()
ohrstrom@1504 79 {
ohrstrom@1504 80 deps = new HashMap<Name, Set<Name>>();
ohrstrom@1504 81 explicitPackages = new HashSet<Name>();
ohrstrom@1504 82 publicApiPerClass = new HashMap<Name,StringBuffer>();
ohrstrom@1504 83 }
ohrstrom@1504 84
ohrstrom@1504 85 /**
ohrstrom@1504 86 * Fetch the set of dependencies that are relevant to the compile
ohrstrom@1504 87 * that has just been performed. I.e. we are only interested in
ohrstrom@1504 88 * dependencies for classes that were explicitly compiled.
ohrstrom@1504 89 * @return
ohrstrom@1504 90 */
ohrstrom@1504 91 public Map<String,Set<String>> getDependencies() {
ohrstrom@1504 92 Map<String,Set<String>> new_deps = new HashMap<String,Set<String>>();
ohrstrom@1504 93 if (explicitPackages == null) return new_deps;
ohrstrom@1504 94 for (Name pkg : explicitPackages) {
ohrstrom@1504 95 Set<Name> set = deps.get(pkg);
ohrstrom@1504 96 if (set != null) {
ohrstrom@1504 97 Set<String> new_set = new_deps.get(pkg.toString());
ohrstrom@1504 98 if (new_set == null) {
ohrstrom@1504 99 new_set = new HashSet<String>();
ohrstrom@1504 100 // Modules beware....
ohrstrom@1504 101 new_deps.put(":"+pkg.toString(), new_set);
ohrstrom@1504 102 }
ohrstrom@1504 103 for (Name d : set) {
ohrstrom@1504 104 new_set.add(":"+d.toString());
ohrstrom@1504 105 }
ohrstrom@1504 106 }
ohrstrom@1504 107 }
ohrstrom@1504 108 return new_deps;
ohrstrom@1504 109 }
ohrstrom@1504 110
ohrstrom@1504 111 class CompareNames implements Comparator<Name> {
ohrstrom@1504 112 public int compare(Name a, Name b) {
ohrstrom@1504 113 return a.toString().compareTo(b.toString());
ohrstrom@1504 114 }
ohrstrom@1504 115
ohrstrom@1504 116 }
ohrstrom@1504 117
ohrstrom@1504 118 /**
ohrstrom@1504 119 * Convert the map from class names to their pubapi to a map
ohrstrom@1504 120 * from package names to their pubapi (which is the sorted concatenation
ohrstrom@1504 121 * of all the class pubapis)
ohrstrom@1504 122 */
ohrstrom@1504 123 public Map<String,String> getPubapis() {
ohrstrom@1504 124 Map<String,String> publicApiPerPackage = new HashMap<String,String>();
ohrstrom@1504 125 if (publicApiPerClass == null) return publicApiPerPackage;
ohrstrom@1504 126 Name[] keys = publicApiPerClass.keySet().toArray(new Name[0]);
ohrstrom@1504 127 Arrays.sort(keys, new CompareNames());
ohrstrom@1504 128 StringBuffer newPublicApi = new StringBuffer();
ohrstrom@1504 129 int i=0;
ohrstrom@1504 130 String prevPkg = "";
ohrstrom@1504 131 for (Name k : keys) {
ohrstrom@1504 132 String cn = k.toString();
ohrstrom@1504 133 String pn = "";
ohrstrom@1504 134 int dp = cn.lastIndexOf('.');
ohrstrom@1504 135 if (dp != -1) {
ohrstrom@1504 136 pn = cn.substring(0,dp);
ohrstrom@1504 137 }
ohrstrom@1504 138 if (!pn.equals(prevPkg)) {
ohrstrom@1504 139 if (!prevPkg.equals("")) {
ohrstrom@1504 140 // Add default module name ":"
ohrstrom@1504 141 publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
ohrstrom@1504 142 }
ohrstrom@1504 143 newPublicApi = new StringBuffer();
ohrstrom@1504 144 prevPkg = pn;
ohrstrom@1504 145 }
ohrstrom@1504 146 newPublicApi.append(publicApiPerClass.get(k));
ohrstrom@1504 147 i++;
ohrstrom@1504 148 }
ohrstrom@1504 149 if (!prevPkg.equals(""))
ohrstrom@1504 150 publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
ohrstrom@1504 151 return publicApiPerPackage;
ohrstrom@1504 152 }
ohrstrom@1504 153
ohrstrom@1504 154 /**
ohrstrom@1504 155 * Visit the api of a class and construct a pubapi string and
ohrstrom@1504 156 * store it into the pubapi_perclass map.
ohrstrom@1504 157 */
ohrstrom@1504 158 public void visitPubapi(Element e) {
ohrstrom@1504 159 Name n = ((ClassSymbol)e).fullname;
ohrstrom@1504 160 Name p = ((ClassSymbol)e).packge().fullname;
ohrstrom@1504 161 StringBuffer sb = publicApiPerClass.get(n);
ohrstrom@1504 162 assert(sb == null);
ohrstrom@1504 163 sb = new StringBuffer();
ohrstrom@1504 164 PubapiVisitor v = new PubapiVisitor(sb);
ohrstrom@1504 165 v.visit(e);
ohrstrom@1504 166 if (sb.length()>0) {
ohrstrom@1504 167 publicApiPerClass.put(n, sb);
ohrstrom@1504 168 }
ohrstrom@1504 169 explicitPackages.add(p);
ohrstrom@1504 170 }
ohrstrom@1504 171
ohrstrom@1504 172 /**
ohrstrom@1504 173 * Collect a dependency. curr_pkg is marked as depending on dep_pkg.
ohrstrom@1504 174 */
ohrstrom@1504 175 public void collect(Name currPkg, Name depPkg) {
ohrstrom@1504 176 if (!currPkg.equals(depPkg)) {
ohrstrom@1504 177 Set<Name> theset = deps.get(currPkg);
ohrstrom@1504 178 if (theset==null) {
ohrstrom@1504 179 theset = new HashSet<Name>();
ohrstrom@1504 180 deps.put(currPkg, theset);
ohrstrom@1504 181 }
ohrstrom@1504 182 theset.add(depPkg);
ohrstrom@1504 183 }
ohrstrom@1504 184 }
ohrstrom@1504 185 }

mercurial