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

Fri, 18 Jan 2013 00:16:21 +0100

author
ohrstrom
date
Fri, 18 Jan 2013 00:16:21 +0100
changeset 1504
22e417cdddee
child 2039
0cfd5baa7154
permissions
-rw-r--r--

8004658: Add internal smart javac wrapper to solve JEP 139
Reviewed-by: jjg

ohrstrom@1504 1 /*
ohrstrom@1504 2 * Copyright (c) 2012, 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@1504 84 * Collect all packages, sources and artifacts for all modules
ohrstrom@1504 85 * into the build state.
ohrstrom@1504 86 *
ohrstrom@1504 87 * @param m The set of modules.
ohrstrom@1504 88 */
ohrstrom@1504 89 public void collectPackagesSourcesAndArtifacts(Map<String,Module> m) {
ohrstrom@1504 90 modules = m;
ohrstrom@1504 91 // Extract all the found packages.
ohrstrom@1504 92 for (Module i : modules.values()) {
ohrstrom@1504 93 for (Map.Entry<String,Package> j : i.packages().entrySet()) {
ohrstrom@1504 94 Package p = packages.get(j.getKey());
ohrstrom@1504 95 // Check that no two different packages are stored under same name.
ohrstrom@1504 96 assert(p == null || p == j.getValue());
ohrstrom@1504 97 if (p == null) {
ohrstrom@1504 98 p = j.getValue();
ohrstrom@1504 99 packages.put(j.getKey(),j.getValue());
ohrstrom@1504 100 }
ohrstrom@1504 101 for (Map.Entry<String,Source> k : p.sources().entrySet()) {
ohrstrom@1504 102 Source s = sources.get(k.getKey());
ohrstrom@1504 103 // Check that no two different sources are stored under same name.
ohrstrom@1504 104 assert(s == null || s == k.getValue());
ohrstrom@1504 105 if (s == null) {
ohrstrom@1504 106 s = k.getValue();
ohrstrom@1504 107 sources.put(k.getKey(), k.getValue());
ohrstrom@1504 108 }
ohrstrom@1504 109 }
ohrstrom@1504 110 for (Map.Entry<String,File> g : p.artifacts().entrySet()) {
ohrstrom@1504 111 File f = artifacts.get(g.getKey());
ohrstrom@1504 112 // Check that no two artifacts are stored under the same file.
ohrstrom@1504 113 assert(f == null || f == g.getValue());
ohrstrom@1504 114 if (f == null) {
ohrstrom@1504 115 f = g.getValue();
ohrstrom@1504 116 artifacts.put(g.getKey(), g.getValue());
ohrstrom@1504 117 }
ohrstrom@1504 118 }
ohrstrom@1504 119 }
ohrstrom@1504 120 }
ohrstrom@1504 121 }
ohrstrom@1504 122
ohrstrom@1504 123 /**
ohrstrom@1504 124 * Collect all the artifacts of all modules and packages.
ohrstrom@1504 125 *
ohrstrom@1504 126 * @param m The set of modules.
ohrstrom@1504 127 */
ohrstrom@1504 128 public void collectArtifacts(Map<String,Module> m) {
ohrstrom@1504 129 modules = m;
ohrstrom@1504 130 // Extract all the found packages.
ohrstrom@1504 131 for (Module i : modules.values()) {
ohrstrom@1504 132 for (Map.Entry<String,Package> j : i.packages().entrySet()) {
ohrstrom@1504 133 Package p = packages.get(j.getKey());
ohrstrom@1504 134 // Check that no two different packages are stored under same name.
ohrstrom@1504 135 assert(p == null || p == j.getValue());
ohrstrom@1504 136 p = j.getValue();
ohrstrom@1504 137 packages.put(j.getKey(),j.getValue());
ohrstrom@1504 138 for (Map.Entry<String,File> g : p.artifacts().entrySet()) {
ohrstrom@1504 139 File f = artifacts.get(g.getKey());
ohrstrom@1504 140 // Check that no two artifacts are stored under the same file.
ohrstrom@1504 141 assert(f == null || f == g.getValue());
ohrstrom@1504 142 artifacts.put(g.getKey(), g.getValue());
ohrstrom@1504 143 }
ohrstrom@1504 144 }
ohrstrom@1504 145 }
ohrstrom@1504 146 }
ohrstrom@1504 147
ohrstrom@1504 148 /**
ohrstrom@1504 149 * Calculate the package dependents (ie the reverse of the dependencies).
ohrstrom@1504 150 */
ohrstrom@1504 151 public void calculateDependents() {
ohrstrom@1504 152 dependents = new HashMap<String,Set<String>>();
ohrstrom@1504 153 for (String s : packages.keySet()) {
ohrstrom@1504 154 Package p = packages.get(s);
ohrstrom@1504 155 for (String d : p.dependencies()) {
ohrstrom@1504 156 Set<String> ss = dependents.get(d);
ohrstrom@1504 157 if (ss == null) {
ohrstrom@1504 158 ss = new HashSet<String>();
ohrstrom@1504 159 dependents.put(d, ss);
ohrstrom@1504 160 }
ohrstrom@1504 161 // Add the dependent information to the global dependent map.
ohrstrom@1504 162 ss.add(s);
ohrstrom@1504 163 Package dp = packages.get(d);
ohrstrom@1504 164 // Also add the dependent information to the package specific map.
ohrstrom@1504 165 // Normally, you do not compile java.lang et al. Therefore
ohrstrom@1504 166 // there are several packages that p depends upon that you
ohrstrom@1504 167 // do not have in your state database. This is perfectly fine.
ohrstrom@1504 168 if (dp != null) {
ohrstrom@1504 169 // But this package did exist in the state database.
ohrstrom@1504 170 dp.addDependent(p.name());
ohrstrom@1504 171 }
ohrstrom@1504 172 }
ohrstrom@1504 173 }
ohrstrom@1504 174 }
ohrstrom@1504 175
ohrstrom@1504 176 /**
ohrstrom@1504 177 * Verify that the setModules method above did the right thing when
ohrstrom@1504 178 * running through the module->package->source structure.
ohrstrom@1504 179 */
ohrstrom@1504 180 public void checkInternalState(String msg, boolean linkedOnly, Map<String,Source> srcs) {
ohrstrom@1504 181 boolean baad = false;
ohrstrom@1504 182 Map<String,Source> original = new HashMap<String,Source>();
ohrstrom@1504 183 Map<String,Source> calculated = new HashMap<String,Source>();
ohrstrom@1504 184
ohrstrom@1504 185 for (String s : sources.keySet()) {
ohrstrom@1504 186 Source ss = sources.get(s);
ohrstrom@1504 187 if (ss.isLinkedOnly() == linkedOnly) {
ohrstrom@1504 188 calculated.put(s,ss);
ohrstrom@1504 189 }
ohrstrom@1504 190 }
ohrstrom@1504 191 for (String s : srcs.keySet()) {
ohrstrom@1504 192 Source ss = srcs.get(s);
ohrstrom@1504 193 if (ss.isLinkedOnly() == linkedOnly) {
ohrstrom@1504 194 original.put(s,ss);
ohrstrom@1504 195 }
ohrstrom@1504 196 }
ohrstrom@1504 197 if (original.size() != calculated.size()) {
ohrstrom@1504 198 Log.error("INTERNAL ERROR "+msg+" original and calculated are not the same size!");
ohrstrom@1504 199 baad = true;
ohrstrom@1504 200 }
ohrstrom@1504 201 if (!original.keySet().equals(calculated.keySet())) {
ohrstrom@1504 202 Log.error("INTERNAL ERROR "+msg+" original and calculated do not have the same domain!");
ohrstrom@1504 203 baad = true;
ohrstrom@1504 204 }
ohrstrom@1504 205 if (!baad) {
ohrstrom@1504 206 for (String s : original.keySet()) {
ohrstrom@1504 207 Source s1 = original.get(s);
ohrstrom@1504 208 Source s2 = calculated.get(s);
ohrstrom@1504 209 if (s1 == null || s2 == null || !s1.equals(s2)) {
ohrstrom@1504 210 Log.error("INTERNAL ERROR "+msg+" original and calculated have differing elements for "+s);
ohrstrom@1504 211 }
ohrstrom@1504 212 baad = true;
ohrstrom@1504 213 }
ohrstrom@1504 214 }
ohrstrom@1504 215 if (baad) {
ohrstrom@1504 216 for (String s : original.keySet()) {
ohrstrom@1504 217 Source ss = original.get(s);
ohrstrom@1504 218 Source sss = calculated.get(s);
ohrstrom@1504 219 if (sss == null) {
ohrstrom@1504 220 Log.error("The file "+s+" does not exist in calculated tree of sources.");
ohrstrom@1504 221 }
ohrstrom@1504 222 }
ohrstrom@1504 223 for (String s : calculated.keySet()) {
ohrstrom@1504 224 Source ss = calculated.get(s);
ohrstrom@1504 225 Source sss = original.get(s);
ohrstrom@1504 226 if (sss == null) {
ohrstrom@1504 227 Log.error("The file "+s+" does not exist in original set of found sources.");
ohrstrom@1504 228 }
ohrstrom@1504 229 }
ohrstrom@1504 230 }
ohrstrom@1504 231 }
ohrstrom@1504 232
ohrstrom@1504 233 /**
ohrstrom@1504 234 * Load a module from the javac state file.
ohrstrom@1504 235 */
ohrstrom@1504 236 public Module loadModule(String l) {
ohrstrom@1504 237 Module m = Module.load(l);
ohrstrom@1504 238 modules.put(m.name(), m);
ohrstrom@1504 239 return m;
ohrstrom@1504 240 }
ohrstrom@1504 241
ohrstrom@1504 242 /**
ohrstrom@1504 243 * Load a package from the javac state file.
ohrstrom@1504 244 */
ohrstrom@1504 245 public Package loadPackage(Module lastModule, String l) {
ohrstrom@1504 246 Package p = Package.load(lastModule, l);
ohrstrom@1504 247 lastModule.addPackage(p);
ohrstrom@1504 248 packages.put(p.name(), p);
ohrstrom@1504 249 return p;
ohrstrom@1504 250 }
ohrstrom@1504 251
ohrstrom@1504 252 /**
ohrstrom@1504 253 * Load a source from the javac state file.
ohrstrom@1504 254 */
ohrstrom@1504 255 public Source loadSource(Package lastPackage, String l, boolean is_generated) {
ohrstrom@1504 256 Source s = Source.load(lastPackage, l, is_generated);
ohrstrom@1504 257 lastPackage.addSource(s);
ohrstrom@1504 258 sources.put(s.name(), s);
ohrstrom@1504 259 return s;
ohrstrom@1504 260 }
ohrstrom@1504 261
ohrstrom@1504 262 /**
ohrstrom@1504 263 * During an incremental compile we need to copy the old javac state
ohrstrom@1504 264 * information about packages that were not recompiled.
ohrstrom@1504 265 */
ohrstrom@1504 266 public void copyPackagesExcept(BuildState prev, Set<String> recompiled, Set<String> removed) {
ohrstrom@1504 267 for (String pkg : prev.packages().keySet()) {
ohrstrom@1504 268 // Do not copy recompiled or removed packages.
ohrstrom@1504 269 if (recompiled.contains(pkg) || removed.contains(pkg)) continue;
ohrstrom@1504 270 Module mnew = findModuleFromPackageName(pkg);
ohrstrom@1504 271 Package pprev = prev.packages().get(pkg);
ohrstrom@1504 272 mnew.addPackage(pprev);
ohrstrom@1504 273 }
ohrstrom@1504 274 }
ohrstrom@1504 275 }

mercurial