Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/sjavac/BuildState.java
38899 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.sjavac;2627import java.io.File;28import java.util.HashMap;29import java.util.HashSet;30import java.util.Map;31import java.util.Set;3233/**34* The build state class captures the source code and generated artifacts35* from a build. There are usually two build states, the previous one (prev),36* loaded from the javac_state file, and the current one (now).37*38* <p><b>This is NOT part of any supported API.39* If you write code that depends on this, you do so at your own40* risk. This code and its internal interfaces are subject to change41* or deletion without notice.</b></p>42*/43public class BuildState {44private Map<String,Module> modules = new HashMap<String,Module>();45private Map<String,Package> packages = new HashMap<String,Package>();46private Map<String,Source> sources = new HashMap<String,Source>();47private Map<String,File> artifacts = new HashMap<String,File>();48// Map from package to a set of packages that depend on said package.49private Map<String,Set<String>> dependents = new HashMap<String,Set<String>>();5051public Map<String,Module> modules() { return modules; }52public Map<String,Package> packages() { return packages; }53public Map<String,Source> sources() { return sources; }54public Map<String,File> artifacts() { return artifacts; }55public Map<String,Set<String>> dependents() { return dependents; }5657/**58* Lookup a module from a name. Create the module if it does59* not exist yet.60*/61public Module lookupModule(String mod) {62Module m = modules.get(mod);63if (m == null) {64m = new Module(mod, "???");65modules.put(mod, m);66}67return m;68}6970/**71* Find a module from a given package name. For example:72* The package name "base:java.lang" will fetch the module named "base".73* The package name ":java.net" will fetch the default module.74*/75Module findModuleFromPackageName(String pkg) {76int cp = pkg.indexOf(':');77assert(cp != -1);78String mod = pkg.substring(0, cp);79return lookupModule(mod);80}8182/**83* Store references to all packages, sources and artifacts for all modules84* into the build state. I.e. flatten the module tree structure85* into global maps stored in the BuildState for easy access.86*87* @param m The set of modules.88*/89public void flattenPackagesSourcesAndArtifacts(Map<String,Module> m) {90modules = m;91// Extract all the found packages.92for (Module i : modules.values()) {93for (Map.Entry<String,Package> j : i.packages().entrySet()) {94Package p = packages.get(j.getKey());95// Check that no two different packages are stored under same name.96assert(p == null || p == j.getValue());97if (p == null) {98p = j.getValue();99packages.put(j.getKey(),j.getValue());100}101for (Map.Entry<String,Source> k : p.sources().entrySet()) {102Source s = sources.get(k.getKey());103// Check that no two different sources are stored under same name.104assert(s == null || s == k.getValue());105if (s == null) {106s = k.getValue();107sources.put(k.getKey(), k.getValue());108}109}110for (Map.Entry<String,File> g : p.artifacts().entrySet()) {111File f = artifacts.get(g.getKey());112// Check that no two artifacts are stored under the same file.113assert(f == null || f == g.getValue());114if (f == null) {115f = g.getValue();116artifacts.put(g.getKey(), g.getValue());117}118}119}120}121}122123/**124* Store references to all artifacts found in the module tree into the maps125* stored in the build state.126*127* @param m The set of modules.128*/129public void flattenArtifacts(Map<String,Module> m) {130modules = m;131// Extract all the found packages.132for (Module i : modules.values()) {133for (Map.Entry<String,Package> j : i.packages().entrySet()) {134Package p = packages.get(j.getKey());135// Check that no two different packages are stored under same name.136assert(p == null || p == j.getValue());137p = j.getValue();138packages.put(j.getKey(),j.getValue());139for (Map.Entry<String,File> g : p.artifacts().entrySet()) {140File f = artifacts.get(g.getKey());141// Check that no two artifacts are stored under the same file.142assert(f == null || f == g.getValue());143artifacts.put(g.getKey(), g.getValue());144}145}146}147}148149/**150* Calculate the package dependents (ie the reverse of the dependencies).151*/152public void calculateDependents() {153dependents = new HashMap<String,Set<String>>();154for (String s : packages.keySet()) {155Package p = packages.get(s);156for (String d : p.dependencies()) {157Set<String> ss = dependents.get(d);158if (ss == null) {159ss = new HashSet<String>();160dependents.put(d, ss);161}162// Add the dependent information to the global dependent map.163ss.add(s);164Package dp = packages.get(d);165// Also add the dependent information to the package specific map.166// Normally, you do not compile java.lang et al. Therefore167// there are several packages that p depends upon that you168// do not have in your state database. This is perfectly fine.169if (dp != null) {170// But this package did exist in the state database.171dp.addDependent(p.name());172}173}174}175}176177/**178* Verify that the setModules method above did the right thing when179* running through the module->package->source structure.180*/181public void checkInternalState(String msg, boolean linkedOnly, Map<String,Source> srcs) {182boolean baad = false;183Map<String,Source> original = new HashMap<String,Source>();184Map<String,Source> calculated = new HashMap<String,Source>();185186for (String s : sources.keySet()) {187Source ss = sources.get(s);188if (ss.isLinkedOnly() == linkedOnly) {189calculated.put(s,ss);190}191}192for (String s : srcs.keySet()) {193Source ss = srcs.get(s);194if (ss.isLinkedOnly() == linkedOnly) {195original.put(s,ss);196}197}198if (original.size() != calculated.size()) {199Log.error("INTERNAL ERROR "+msg+" original and calculated are not the same size!");200baad = true;201}202if (!original.keySet().equals(calculated.keySet())) {203Log.error("INTERNAL ERROR "+msg+" original and calculated do not have the same domain!");204baad = true;205}206if (!baad) {207for (String s : original.keySet()) {208Source s1 = original.get(s);209Source s2 = calculated.get(s);210if (s1 == null || s2 == null || !s1.equals(s2)) {211Log.error("INTERNAL ERROR "+msg+" original and calculated have differing elements for "+s);212}213baad = true;214}215}216if (baad) {217for (String s : original.keySet()) {218Source ss = original.get(s);219Source sss = calculated.get(s);220if (sss == null) {221Log.error("The file "+s+" does not exist in calculated tree of sources.");222}223}224for (String s : calculated.keySet()) {225Source ss = calculated.get(s);226Source sss = original.get(s);227if (sss == null) {228Log.error("The file "+s+" does not exist in original set of found sources.");229}230}231}232}233234/**235* Load a module from the javac state file.236*/237public Module loadModule(String l) {238Module m = Module.load(l);239modules.put(m.name(), m);240return m;241}242243/**244* Load a package from the javac state file.245*/246public Package loadPackage(Module lastModule, String l) {247Package p = Package.load(lastModule, l);248lastModule.addPackage(p);249packages.put(p.name(), p);250return p;251}252253/**254* Load a source from the javac state file.255*/256public Source loadSource(Package lastPackage, String l, boolean is_generated) {257Source s = Source.load(lastPackage, l, is_generated);258lastPackage.addSource(s);259sources.put(s.name(), s);260return s;261}262263/**264* During an incremental compile we need to copy the old javac state265* information about packages that were not recompiled.266*/267public void copyPackagesExcept(BuildState prev, Set<String> recompiled, Set<String> removed) {268for (String pkg : prev.packages().keySet()) {269// Do not copy recompiled or removed packages.270if (recompiled.contains(pkg) || removed.contains(pkg)) continue;271Module mnew = findModuleFromPackageName(pkg);272Package pprev = prev.packages().get(pkg);273mnew.addPackage(pprev);274// Do not forget to update the flattened data.275packages.put(pkg, pprev);276}277}278}279280281