Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/sjavac/BuildState.java
41175 views
/*1* Copyright (c) 2012, 2015, 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.Collections;29import java.util.HashMap;30import java.util.Map;31import java.util.Set;3233import com.sun.tools.javac.util.Assert;34import com.sun.tools.sjavac.pubapi.PubApi;3536/**37* The build state class captures the source code and generated artifacts38* from a build. There are usually two build states, the previous one (prev),39* loaded from the javac_state file, and the current one (now).40*41* <p><b>This is NOT part of any supported API.42* If you write code that depends on this, you do so at your own risk.43* This code and its internal interfaces are subject to change or44* deletion without notice.</b>45*/46public class BuildState {47private Map<String,Module> modules = new HashMap<>();48private Map<String,Package> packages = new HashMap<>();49private Map<String,Source> sources = new HashMap<>();50private Map<String,File> artifacts = new HashMap<>();51// Map from package to a set of packages that depend on said package.52private Map<String,Set<String>> dependents = new HashMap<>();5354public Map<String,Module> modules() { return modules; }55public Map<String,Package> packages() { return packages; }56public Map<String,Source> sources() { return sources; }57public Map<String,File> artifacts() { return artifacts; }58public Map<String,Set<String>> dependents() { return dependents; }5960/**61* Lookup a module from a name. Create the module if it does62* not exist yet.63*/64public Module lookupModule(String mod) {65Module m = modules.get(mod);66if (m == null) {67m = new Module(mod, "???");68modules.put(mod, m);69}70return m;71}7273/**74* Find a module from a given package name. For example:75* The package name "base:java.lang" will fetch the module named "base".76* The package name ":java.net" will fetch the default module.77*/78Module findModuleFromPackageName(String pkg) {79int cp = pkg.indexOf(':');80Assert.check(cp != -1, "Could not find package name");81String mod = pkg.substring(0, cp);82return lookupModule(mod);83}8485/**86* Store references to all packages, sources and artifacts for all modules87* into the build state. I.e. flatten the module tree structure88* into global maps stored in the BuildState for easy access.89*90* @param m The set of modules.91*/92public void flattenPackagesSourcesAndArtifacts(Map<String,Module> m) {93modules = m;94// Extract all the found packages.95for (Module i : modules.values()) {96for (Map.Entry<String,Package> j : i.packages().entrySet()) {97Package p = packages.get(j.getKey());98// Check that no two different packages are stored under same name.99Assert.check(p == null || p == j.getValue());100if (p == null) {101p = j.getValue();102packages.put(j.getKey(),j.getValue());103}104for (Map.Entry<String,Source> k : p.sources().entrySet()) {105Source s = sources.get(k.getKey());106// Check that no two different sources are stored under same name.107Assert.check(s == null || s == k.getValue());108if (s == null) {109s = k.getValue();110sources.put(k.getKey(), k.getValue());111}112}113for (Map.Entry<String,File> g : p.artifacts().entrySet()) {114File f = artifacts.get(g.getKey());115// Check that no two artifacts are stored under the same file.116Assert.check(f == null || f == g.getValue());117if (f == null) {118f = g.getValue();119artifacts.put(g.getKey(), g.getValue());120}121}122}123}124}125126/**127* Store references to all artifacts found in the module tree into the maps128* stored in the build state.129*130* @param m The set of modules.131*/132public void flattenArtifacts(Map<String,Module> m) {133modules = m;134// Extract all the found packages.135for (Module i : modules.values()) {136for (Map.Entry<String,Package> j : i.packages().entrySet()) {137Package p = packages.get(j.getKey());138// Check that no two different packages are stored under same name.139Assert.check(p == null || p == j.getValue());140p = j.getValue();141packages.put(j.getKey(),j.getValue());142for (Map.Entry<String,File> g : p.artifacts().entrySet()) {143File f = artifacts.get(g.getKey());144// Check that no two artifacts are stored under the same file.145Assert.check(f == null || f == g.getValue());146artifacts.put(g.getKey(), g.getValue());147}148}149}150}151152/**153* Calculate the package dependents (ie the reverse of the dependencies).154*/155public void calculateDependents() {156dependents = new HashMap<>();157158for (String s : packages.keySet()) {159Package p = packages.get(s);160161// Collect all dependencies of the classes in this package162Set<String> deps = p.typeDependencies() // maps fqName -> set of dependencies163.values()164.stream()165.reduce(Collections.emptySet(), Util::union);166167// Now reverse the direction168169for (String dep : deps) {170// Add the dependent information to the global dependent map.171String depPkgStr = ":" + dep.substring(0, dep.lastIndexOf('.'));172dependents.merge(depPkgStr, Collections.singleton(s), Util::union);173174// Also add the dependent information to the package specific map.175// Normally, you do not compile java.lang et al. Therefore176// there are several packages that p depends upon that you177// do not have in your state database. This is perfectly fine.178Package dp = packages.get(depPkgStr);179if (dp != null) {180// But this package did exist in the state database.181dp.addDependent(p.name());182}183}184}185}186187/**188* Verify that the setModules method above did the right thing when189* running through the {@literal module->package->source} structure.190*/191public void checkInternalState(String msg, boolean linkedOnly, Map<String,Source> srcs) {192boolean baad = false;193Map<String,Source> original = new HashMap<>();194Map<String,Source> calculated = new HashMap<>();195196for (String s : sources.keySet()) {197Source ss = sources.get(s);198if (ss.isLinkedOnly() == linkedOnly) {199calculated.put(s,ss);200}201}202for (String s : srcs.keySet()) {203Source ss = srcs.get(s);204if (ss.isLinkedOnly() == linkedOnly) {205original.put(s,ss);206}207}208if (original.size() != calculated.size()) {209Log.error("INTERNAL ERROR "+msg+" original and calculated are not the same size!");210baad = true;211}212if (!original.keySet().equals(calculated.keySet())) {213Log.error("INTERNAL ERROR "+msg+" original and calculated do not have the same domain!");214baad = true;215}216if (!baad) {217for (String s : original.keySet()) {218Source s1 = original.get(s);219Source s2 = calculated.get(s);220if (s1 == null || s2 == null || !s1.equals(s2)) {221Log.error("INTERNAL ERROR "+msg+" original and calculated have differing elements for "+s);222}223baad = true;224}225}226if (baad) {227for (String s : original.keySet()) {228Source ss = original.get(s);229Source sss = calculated.get(s);230if (sss == null) {231Log.error("The file "+s+" does not exist in calculated tree of sources.");232}233}234for (String s : calculated.keySet()) {235Source ss = calculated.get(s);236Source sss = original.get(s);237if (sss == null) {238Log.error("The file "+s+" does not exist in original set of found sources.");239}240}241}242}243244/**245* Load a module from the javac state file.246*/247public Module loadModule(String l) {248Module m = Module.load(l);249modules.put(m.name(), m);250return m;251}252253/**254* Load a package from the javac state file.255*/256public Package loadPackage(Module lastModule, String l) {257Package p = Package.load(lastModule, l);258lastModule.addPackage(p);259packages.put(p.name(), p);260return p;261}262263/**264* Load a source from the javac state file.265*/266public Source loadSource(Package lastPackage, String l, boolean is_generated) {267Source s = Source.load(lastPackage, l, is_generated);268lastPackage.addSource(s);269sources.put(s.name(), s);270return s;271}272273/**274* During an incremental compile we need to copy the old javac state275* information about packages that were not recompiled.276*/277public void copyPackagesExcept(BuildState prev, Set<String> recompiled, Set<String> removed) {278for (String pkg : prev.packages().keySet()) {279// Do not copy recompiled or removed packages.280if (recompiled.contains(pkg) || removed.contains(pkg))281continue;282283Module mnew = findModuleFromPackageName(pkg);284Package pprev = prev.packages().get(pkg);285286// Even though we haven't recompiled this package, we may have287// information about its public API: It may be a classpath dependency288if (packages.containsKey(pkg)) {289pprev.setPubapi(PubApi.mergeTypes(pprev.getPubApi(),290packages.get(pkg).getPubApi()));291}292293mnew.addPackage(pprev);294// Do not forget to update the flattened data. (See JDK-8071904)295packages.put(pkg, pprev);296}297}298}299300301