src/share/classes/com/sun/tools/sjavac/BuildState.java

Thu, 19 Sep 2013 08:26:26 -0700

author
ohrstrom
date
Thu, 19 Sep 2013 08:26:26 -0700
changeset 2039
0cfd5baa7154
parent 1504
22e417cdddee
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8024609: sjavac assertion fails during call to BuildState.collectArtifacts
Reviewed-by: jjg

ohrstrom@1504 1 /*
ohrstrom@2039 2 * Copyright (c) 2012, 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;
ohrstrom@1504 27
ohrstrom@1504 28 import java.io.File;
ohrstrom@1504 29 import java.util.HashMap;
ohrstrom@1504 30 import java.util.HashSet;
ohrstrom@1504 31 import java.util.Map;
ohrstrom@1504 32 import java.util.Set;
ohrstrom@1504 33
ohrstrom@1504 34 /**
ohrstrom@1504 35 * The build state class captures the source code and generated artifacts
ohrstrom@1504 36 * from a build. There are usually two build states, the previous one (prev),
ohrstrom@1504 37 * loaded from the javac_state file, and the current one (now).
ohrstrom@1504 38 *
ohrstrom@1504 39 * <p><b>This is NOT part of any supported API.
ohrstrom@1504 40 * If you write code that depends on this, you do so at your own
ohrstrom@1504 41 * risk. This code and its internal interfaces are subject to change
ohrstrom@1504 42 * or deletion without notice.</b></p>
ohrstrom@1504 43 */
ohrstrom@1504 44 public class BuildState {
ohrstrom@1504 45 private Map<String,Module> modules = new HashMap<String,Module>();
ohrstrom@1504 46 private Map<String,Package> packages = new HashMap<String,Package>();
ohrstrom@1504 47 private Map<String,Source> sources = new HashMap<String,Source>();
ohrstrom@1504 48 private Map<String,File> artifacts = new HashMap<String,File>();
ohrstrom@1504 49 // Map from package to a set of packages that depend on said package.
ohrstrom@1504 50 private Map<String,Set<String>> dependents = new HashMap<String,Set<String>>();
ohrstrom@1504 51
ohrstrom@1504 52 public Map<String,Module> modules() { return modules; }
ohrstrom@1504 53 public Map<String,Package> packages() { return packages; }
ohrstrom@1504 54 public Map<String,Source> sources() { return sources; }
ohrstrom@1504 55 public Map<String,File> artifacts() { return artifacts; }
ohrstrom@1504 56 public Map<String,Set<String>> dependents() { return dependents; }
ohrstrom@1504 57
ohrstrom@1504 58 /**
ohrstrom@1504 59 * Lookup a module from a name. Create the module if it does
ohrstrom@1504 60 * not exist yet.
ohrstrom@1504 61 */
ohrstrom@1504 62 public Module lookupModule(String mod) {
ohrstrom@1504 63 Module m = modules.get(mod);
ohrstrom@1504 64 if (m == null) {
ohrstrom@1504 65 m = new Module(mod, "???");
ohrstrom@1504 66 modules.put(mod, m);
ohrstrom@1504 67 }
ohrstrom@1504 68 return m;
ohrstrom@1504 69 }
ohrstrom@1504 70
ohrstrom@1504 71 /**
ohrstrom@1504 72 * Find a module from a given package name. For example:
ohrstrom@1504 73 * The package name "base:java.lang" will fetch the module named "base".
ohrstrom@1504 74 * The package name ":java.net" will fetch the default module.
ohrstrom@1504 75 */
ohrstrom@1504 76 Module findModuleFromPackageName(String pkg) {
ohrstrom@1504 77 int cp = pkg.indexOf(':');
ohrstrom@1504 78 assert(cp != -1);
ohrstrom@1504 79 String mod = pkg.substring(0, cp);
ohrstrom@1504 80 return lookupModule(mod);
ohrstrom@1504 81 }
ohrstrom@1504 82
ohrstrom@1504 83 /**
ohrstrom@2039 84 * Store references to all packages, sources and artifacts for all modules
ohrstrom@2039 85 * into the build state. I.e. flatten the module tree structure
ohrstrom@2039 86 * into global maps stored in the BuildState for easy access.
ohrstrom@1504 87 *
ohrstrom@1504 88 * @param m The set of modules.
ohrstrom@1504 89 */
ohrstrom@2039 90 public void flattenPackagesSourcesAndArtifacts(Map<String,Module> m) {
ohrstrom@1504 91 modules = m;
ohrstrom@1504 92 // Extract all the found packages.
ohrstrom@1504 93 for (Module i : modules.values()) {
ohrstrom@1504 94 for (Map.Entry<String,Package> j : i.packages().entrySet()) {
ohrstrom@1504 95 Package p = packages.get(j.getKey());
ohrstrom@1504 96 // Check that no two different packages are stored under same name.
ohrstrom@1504 97 assert(p == null || p == j.getValue());
ohrstrom@1504 98 if (p == null) {
ohrstrom@1504 99 p = j.getValue();
ohrstrom@1504 100 packages.put(j.getKey(),j.getValue());
ohrstrom@1504 101 }
ohrstrom@1504 102 for (Map.Entry<String,Source> k : p.sources().entrySet()) {
ohrstrom@1504 103 Source s = sources.get(k.getKey());
ohrstrom@1504 104 // Check that no two different sources are stored under same name.
ohrstrom@1504 105 assert(s == null || s == k.getValue());
ohrstrom@1504 106 if (s == null) {
ohrstrom@1504 107 s = k.getValue();
ohrstrom@1504 108 sources.put(k.getKey(), k.getValue());
ohrstrom@1504 109 }
ohrstrom@1504 110 }
ohrstrom@1504 111 for (Map.Entry<String,File> g : p.artifacts().entrySet()) {
ohrstrom@1504 112 File f = artifacts.get(g.getKey());
ohrstrom@1504 113 // Check that no two artifacts are stored under the same file.
ohrstrom@1504 114 assert(f == null || f == g.getValue());
ohrstrom@1504 115 if (f == null) {
ohrstrom@1504 116 f = g.getValue();
ohrstrom@1504 117 artifacts.put(g.getKey(), g.getValue());
ohrstrom@1504 118 }
ohrstrom@1504 119 }
ohrstrom@1504 120 }
ohrstrom@1504 121 }
ohrstrom@1504 122 }
ohrstrom@1504 123
ohrstrom@1504 124 /**
ohrstrom@2039 125 * Store references to all artifacts found in the module tree into the maps
ohrstrom@2039 126 * stored in the build state.
ohrstrom@1504 127 *
ohrstrom@1504 128 * @param m The set of modules.
ohrstrom@1504 129 */
ohrstrom@2039 130 public void flattenArtifacts(Map<String,Module> m) {
ohrstrom@1504 131 modules = m;
ohrstrom@1504 132 // Extract all the found packages.
ohrstrom@1504 133 for (Module i : modules.values()) {
ohrstrom@1504 134 for (Map.Entry<String,Package> j : i.packages().entrySet()) {
ohrstrom@1504 135 Package p = packages.get(j.getKey());
ohrstrom@1504 136 // Check that no two different packages are stored under same name.
ohrstrom@1504 137 assert(p == null || p == j.getValue());
ohrstrom@1504 138 p = j.getValue();
ohrstrom@1504 139 packages.put(j.getKey(),j.getValue());
ohrstrom@1504 140 for (Map.Entry<String,File> g : p.artifacts().entrySet()) {
ohrstrom@1504 141 File f = artifacts.get(g.getKey());
ohrstrom@1504 142 // Check that no two artifacts are stored under the same file.
ohrstrom@1504 143 assert(f == null || f == g.getValue());
ohrstrom@1504 144 artifacts.put(g.getKey(), g.getValue());
ohrstrom@1504 145 }
ohrstrom@1504 146 }
ohrstrom@1504 147 }
ohrstrom@1504 148 }
ohrstrom@1504 149
ohrstrom@1504 150 /**
ohrstrom@1504 151 * Calculate the package dependents (ie the reverse of the dependencies).
ohrstrom@1504 152 */
ohrstrom@1504 153 public void calculateDependents() {
ohrstrom@1504 154 dependents = new HashMap<String,Set<String>>();
ohrstrom@1504 155 for (String s : packages.keySet()) {
ohrstrom@1504 156 Package p = packages.get(s);
ohrstrom@1504 157 for (String d : p.dependencies()) {
ohrstrom@1504 158 Set<String> ss = dependents.get(d);
ohrstrom@1504 159 if (ss == null) {
ohrstrom@1504 160 ss = new HashSet<String>();
ohrstrom@1504 161 dependents.put(d, ss);
ohrstrom@1504 162 }
ohrstrom@1504 163 // Add the dependent information to the global dependent map.
ohrstrom@1504 164 ss.add(s);
ohrstrom@1504 165 Package dp = packages.get(d);
ohrstrom@1504 166 // Also add the dependent information to the package specific map.
ohrstrom@1504 167 // Normally, you do not compile java.lang et al. Therefore
ohrstrom@1504 168 // there are several packages that p depends upon that you
ohrstrom@1504 169 // do not have in your state database. This is perfectly fine.
ohrstrom@1504 170 if (dp != null) {
ohrstrom@1504 171 // But this package did exist in the state database.
ohrstrom@1504 172 dp.addDependent(p.name());
ohrstrom@1504 173 }
ohrstrom@1504 174 }
ohrstrom@1504 175 }
ohrstrom@1504 176 }
ohrstrom@1504 177
ohrstrom@1504 178 /**
ohrstrom@1504 179 * Verify that the setModules method above did the right thing when
ohrstrom@1504 180 * running through the module->package->source structure.
ohrstrom@1504 181 */
ohrstrom@1504 182 public void checkInternalState(String msg, boolean linkedOnly, Map<String,Source> srcs) {
ohrstrom@1504 183 boolean baad = false;
ohrstrom@1504 184 Map<String,Source> original = new HashMap<String,Source>();
ohrstrom@1504 185 Map<String,Source> calculated = new HashMap<String,Source>();
ohrstrom@1504 186
ohrstrom@1504 187 for (String s : sources.keySet()) {
ohrstrom@1504 188 Source ss = sources.get(s);
ohrstrom@1504 189 if (ss.isLinkedOnly() == linkedOnly) {
ohrstrom@1504 190 calculated.put(s,ss);
ohrstrom@1504 191 }
ohrstrom@1504 192 }
ohrstrom@1504 193 for (String s : srcs.keySet()) {
ohrstrom@1504 194 Source ss = srcs.get(s);
ohrstrom@1504 195 if (ss.isLinkedOnly() == linkedOnly) {
ohrstrom@1504 196 original.put(s,ss);
ohrstrom@1504 197 }
ohrstrom@1504 198 }
ohrstrom@1504 199 if (original.size() != calculated.size()) {
ohrstrom@1504 200 Log.error("INTERNAL ERROR "+msg+" original and calculated are not the same size!");
ohrstrom@1504 201 baad = true;
ohrstrom@1504 202 }
ohrstrom@1504 203 if (!original.keySet().equals(calculated.keySet())) {
ohrstrom@1504 204 Log.error("INTERNAL ERROR "+msg+" original and calculated do not have the same domain!");
ohrstrom@1504 205 baad = true;
ohrstrom@1504 206 }
ohrstrom@1504 207 if (!baad) {
ohrstrom@1504 208 for (String s : original.keySet()) {
ohrstrom@1504 209 Source s1 = original.get(s);
ohrstrom@1504 210 Source s2 = calculated.get(s);
ohrstrom@1504 211 if (s1 == null || s2 == null || !s1.equals(s2)) {
ohrstrom@1504 212 Log.error("INTERNAL ERROR "+msg+" original and calculated have differing elements for "+s);
ohrstrom@1504 213 }
ohrstrom@1504 214 baad = true;
ohrstrom@1504 215 }
ohrstrom@1504 216 }
ohrstrom@1504 217 if (baad) {
ohrstrom@1504 218 for (String s : original.keySet()) {
ohrstrom@1504 219 Source ss = original.get(s);
ohrstrom@1504 220 Source sss = calculated.get(s);
ohrstrom@1504 221 if (sss == null) {
ohrstrom@1504 222 Log.error("The file "+s+" does not exist in calculated tree of sources.");
ohrstrom@1504 223 }
ohrstrom@1504 224 }
ohrstrom@1504 225 for (String s : calculated.keySet()) {
ohrstrom@1504 226 Source ss = calculated.get(s);
ohrstrom@1504 227 Source sss = original.get(s);
ohrstrom@1504 228 if (sss == null) {
ohrstrom@1504 229 Log.error("The file "+s+" does not exist in original set of found sources.");
ohrstrom@1504 230 }
ohrstrom@1504 231 }
ohrstrom@1504 232 }
ohrstrom@1504 233 }
ohrstrom@1504 234
ohrstrom@1504 235 /**
ohrstrom@1504 236 * Load a module from the javac state file.
ohrstrom@1504 237 */
ohrstrom@1504 238 public Module loadModule(String l) {
ohrstrom@1504 239 Module m = Module.load(l);
ohrstrom@1504 240 modules.put(m.name(), m);
ohrstrom@1504 241 return m;
ohrstrom@1504 242 }
ohrstrom@1504 243
ohrstrom@1504 244 /**
ohrstrom@1504 245 * Load a package from the javac state file.
ohrstrom@1504 246 */
ohrstrom@1504 247 public Package loadPackage(Module lastModule, String l) {
ohrstrom@1504 248 Package p = Package.load(lastModule, l);
ohrstrom@1504 249 lastModule.addPackage(p);
ohrstrom@1504 250 packages.put(p.name(), p);
ohrstrom@1504 251 return p;
ohrstrom@1504 252 }
ohrstrom@1504 253
ohrstrom@1504 254 /**
ohrstrom@1504 255 * Load a source from the javac state file.
ohrstrom@1504 256 */
ohrstrom@1504 257 public Source loadSource(Package lastPackage, String l, boolean is_generated) {
ohrstrom@1504 258 Source s = Source.load(lastPackage, l, is_generated);
ohrstrom@1504 259 lastPackage.addSource(s);
ohrstrom@1504 260 sources.put(s.name(), s);
ohrstrom@1504 261 return s;
ohrstrom@1504 262 }
ohrstrom@1504 263
ohrstrom@1504 264 /**
ohrstrom@1504 265 * During an incremental compile we need to copy the old javac state
ohrstrom@1504 266 * information about packages that were not recompiled.
ohrstrom@1504 267 */
ohrstrom@1504 268 public void copyPackagesExcept(BuildState prev, Set<String> recompiled, Set<String> removed) {
ohrstrom@1504 269 for (String pkg : prev.packages().keySet()) {
ohrstrom@1504 270 // Do not copy recompiled or removed packages.
ohrstrom@1504 271 if (recompiled.contains(pkg) || removed.contains(pkg)) continue;
ohrstrom@1504 272 Module mnew = findModuleFromPackageName(pkg);
ohrstrom@1504 273 Package pprev = prev.packages().get(pkg);
ohrstrom@1504 274 mnew.addPackage(pprev);
ohrstrom@2039 275 // Do not forget to update the flattened data.
ohrstrom@2039 276 packages.put(pkg, pprev);
ohrstrom@1504 277 }
ohrstrom@1504 278 }
ohrstrom@1504 279 }

mercurial