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

Mon, 11 Mar 2013 19:03:35 -0700

author
ohrstrom
date
Mon, 11 Mar 2013 19:03:35 -0700
changeset 1625
fbb6e470079d
parent 1504
22e417cdddee
child 2039
0cfd5baa7154
permissions
-rw-r--r--

8009843: sjavac should accept -cp as synonym for -classpath
Reviewed-by: jjg

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

mercurial