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

changeset 1570
f91144b7da75
parent 1504
22e417cdddee
child 1588
2620c953e9fe
equal deleted inserted replaced
1569:475eb15dfdad 1570:f91144b7da75
1 /*
2 * Copyright (c) 1999, 2011, 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
26 package com.sun.tools.sjavac.comp;
27
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;
35
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;
40
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>();
52
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;
60
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;
65
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 }
72
73 private Dependencies(Context context) {
74 context.put(dependenciesKey, this);
75 log = Log.instance(context);
76 }
77
78 public void reset()
79 {
80 deps = new HashMap<Name, Set<Name>>();
81 explicitPackages = new HashSet<Name>();
82 publicApiPerClass = new HashMap<Name,StringBuffer>();
83 }
84
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 }
110
111 class CompareNames implements Comparator<Name> {
112 public int compare(Name a, Name b) {
113 return a.toString().compareTo(b.toString());
114 }
115
116 public boolean equals(Object obj) {
117 return super.equals(obj);
118 }
119 }
120
121 /**
122 * Convert the map from class names to their pubapi to a map
123 * from package names to their pubapi (which is the sorted concatenation
124 * of all the class pubapis)
125 */
126 public Map<String,String> getPubapis() {
127 Map<String,String> publicApiPerPackage = new HashMap<String,String>();
128 if (publicApiPerClass == null) return publicApiPerPackage;
129 Name[] keys = publicApiPerClass.keySet().toArray(new Name[0]);
130 Arrays.sort(keys, new CompareNames());
131 StringBuffer newPublicApi = new StringBuffer();
132 int i=0;
133 String prevPkg = "";
134 for (Name k : keys) {
135 String cn = k.toString();
136 String pn = "";
137 int dp = cn.lastIndexOf('.');
138 if (dp != -1) {
139 pn = cn.substring(0,dp);
140 }
141 if (!pn.equals(prevPkg)) {
142 if (!prevPkg.equals("")) {
143 // Add default module name ":"
144 publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
145 }
146 newPublicApi = new StringBuffer();
147 prevPkg = pn;
148 }
149 newPublicApi.append(publicApiPerClass.get(k));
150 i++;
151 }
152 if (!prevPkg.equals(""))
153 publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
154 return publicApiPerPackage;
155 }
156
157 /**
158 * Visit the api of a class and construct a pubapi string and
159 * store it into the pubapi_perclass map.
160 */
161 public void visitPubapi(Element e) {
162 Name n = ((ClassSymbol)e).fullname;
163 Name p = ((ClassSymbol)e).packge().fullname;
164 StringBuffer sb = publicApiPerClass.get(n);
165 assert(sb == null);
166 sb = new StringBuffer();
167 PubapiVisitor v = new PubapiVisitor(sb);
168 v.visit(e);
169 if (sb.length()>0) {
170 publicApiPerClass.put(n, sb);
171 }
172 explicitPackages.add(p);
173 }
174
175 /**
176 * Collect a dependency. curr_pkg is marked as depending on dep_pkg.
177 */
178 public void collect(Name currPkg, Name depPkg) {
179 if (!currPkg.equals(depPkg)) {
180 Set<Name> theset = deps.get(currPkg);
181 if (theset==null) {
182 theset = new HashSet<Name>();
183 deps.put(currPkg, theset);
184 }
185 theset.add(depPkg);
186 }
187 }
188 }

mercurial