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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/sjavac/Source.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,400 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.sjavac;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.util.Set;
    1.33 +import java.util.Collections;
    1.34 +import java.util.List;
    1.35 +import java.util.ArrayList;
    1.36 +import java.util.Map;
    1.37 +
    1.38 +/** A Source object maintains information about a source file.
    1.39 + * For example which package it belongs to and kind of source it is.
    1.40 + * The class also knows how to find source files (scanRoot) given include/exclude
    1.41 + * patterns and a root.
    1.42 + *
    1.43 + * <p><b>This is NOT part of any supported API.
    1.44 + * If you write code that depends on this, you do so at your own
    1.45 + * risk.  This code and its internal interfaces are subject to change
    1.46 + * or deletion without notice.</b></p>
    1.47 + */
    1.48 +public class Source implements Comparable<Source> {
    1.49 +    // The package the source belongs to.
    1.50 +   private Package pkg;
    1.51 +    // Name of this source file, relative its source root.
    1.52 +    // For example: java/lang/Object.java
    1.53 +    // Or if the source file is inside a module:
    1.54 +    // jdk.base/java/lang/Object.java
    1.55 +    private String name;
    1.56 +    // What kind of file is this.
    1.57 +    private String suffix;
    1.58 +    // When this source file was last_modified
    1.59 +    private long lastModified;
    1.60 +    // The source File.
    1.61 +    private File file;
    1.62 +    // The source root under which file resides.
    1.63 +    private File root;
    1.64 +    // If the source is generated.
    1.65 +    private boolean isGenerated;
    1.66 +    // If the source is only linked to, not compiled.
    1.67 +    private boolean linkedOnly;
    1.68 +
    1.69 +    @Override
    1.70 +    public boolean equals(Object o) {
    1.71 +        return (o instanceof Source) && name.equals(((Source)o).name);
    1.72 +    }
    1.73 +
    1.74 +    @Override
    1.75 +    public int compareTo(Source o) {
    1.76 +        return name.compareTo(o.name);
    1.77 +    }
    1.78 +
    1.79 +    @Override
    1.80 +    public int hashCode() {
    1.81 +        return name.hashCode();
    1.82 +    }
    1.83 +
    1.84 +    public Source(Module m, String n, File f, File r) {
    1.85 +        name = n;
    1.86 +        int dp = n.lastIndexOf(".");
    1.87 +        if (dp != -1) {
    1.88 +            suffix = n.substring(dp);
    1.89 +        } else {
    1.90 +            suffix = "";
    1.91 +        }
    1.92 +        file = f;
    1.93 +        root = r;
    1.94 +        lastModified = f.lastModified();
    1.95 +        linkedOnly = false;
    1.96 +    }
    1.97 +
    1.98 +    public Source(Package p, String n, long lm) {
    1.99 +        pkg = p;
   1.100 +        name = n;
   1.101 +        int dp = n.lastIndexOf(".");
   1.102 +        if (dp != -1) {
   1.103 +            suffix = n.substring(dp);
   1.104 +        } else {
   1.105 +            suffix = "";
   1.106 +        }
   1.107 +        file = null;
   1.108 +        root = null;
   1.109 +        lastModified = lm;
   1.110 +        linkedOnly = false;
   1.111 +        int ls = n.lastIndexOf('/');
   1.112 +    }
   1.113 +
   1.114 +    public String name() { return name; }
   1.115 +    public String suffix() { return suffix; }
   1.116 +    public Package pkg() { return pkg; }
   1.117 +    public File   file() { return file; }
   1.118 +    public File   root() { return root; }
   1.119 +    public long lastModified() {
   1.120 +        return lastModified;
   1.121 +    }
   1.122 +
   1.123 +    public void setPackage(Package p) {
   1.124 +        pkg = p;
   1.125 +    }
   1.126 +
   1.127 +    public void markAsGenerated() {
   1.128 +        isGenerated = true;
   1.129 +    }
   1.130 +
   1.131 +    public boolean isGenerated() {
   1.132 +        return isGenerated;
   1.133 +    }
   1.134 +
   1.135 +    public void markAsLinkedOnly() {
   1.136 +        linkedOnly = true;
   1.137 +    }
   1.138 +
   1.139 +    public boolean isLinkedOnly() {
   1.140 +        return linkedOnly;
   1.141 +    }
   1.142 +
   1.143 +    private void save(StringBuilder b) {
   1.144 +        String CL = linkedOnly?"L":"C";
   1.145 +        String GS = isGenerated?"G":"S";
   1.146 +        b.append(GS+" "+CL+" "+name+" "+file.lastModified()+"\n");
   1.147 +    }
   1.148 +    // Parse a line that looks like this:
   1.149 +    // S C /code/alfa/A.java 1357631228000
   1.150 +    static public Source load(Package lastPackage, String l, boolean isGenerated) {
   1.151 +        int sp = l.indexOf(' ',4);
   1.152 +        if (sp == -1) return null;
   1.153 +        String name = l.substring(4,sp);
   1.154 +        long last_modified = Long.parseLong(l.substring(sp+1));
   1.155 +
   1.156 +        boolean isLinkedOnly = false;
   1.157 +        if (l.charAt(2) == 'L') {
   1.158 +            isLinkedOnly = true;
   1.159 +        } else if (l.charAt(2) == 'C') {
   1.160 +            isLinkedOnly = false;
   1.161 +        } else return null;
   1.162 +
   1.163 +        Source s = new Source(lastPackage, name, last_modified);
   1.164 +        s.file = new File(name);
   1.165 +        if (isGenerated) s.markAsGenerated();
   1.166 +        if (isLinkedOnly) s.markAsLinkedOnly();
   1.167 +        return s;
   1.168 +    }
   1.169 +
   1.170 +    public static void saveSources(Map<String,Source> sources, StringBuilder b) {
   1.171 +        List<String> sorted_sources = new ArrayList<String>();
   1.172 +        for (String key : sources.keySet()) {
   1.173 +            sorted_sources.add(key);
   1.174 +        }
   1.175 +        Collections.sort(sorted_sources);
   1.176 +        for (String key : sorted_sources) {
   1.177 +            Source s = sources.get(key);
   1.178 +            s.save(b);
   1.179 +        }
   1.180 +    }
   1.181 +
   1.182 +    /**
   1.183 +     * Recurse into the directory root and find all files matchine the excl/incl/exclfiles/inclfiles rules.
   1.184 +     * Detects the existence of module-info.java files and presumes that the directory it resides in
   1.185 +     * is the name of the current module.
   1.186 +     */
   1.187 +    static public void scanRoot(File root,
   1.188 +                                Set<String> suffixes,
   1.189 +                                List<String> excludes, List<String> includes,
   1.190 +                                List<String> excludeFiles, List<String> includeFiles,
   1.191 +                                Map<String,Source> foundFiles,
   1.192 +                                Map<String,Module> foundModules,
   1.193 +                                Module currentModule,
   1.194 +                                boolean permitSourcesWithoutPackage,
   1.195 +                                boolean inGensrc,
   1.196 +                                boolean inLinksrc)
   1.197 +        throws ProblemException {
   1.198 +
   1.199 +        if (root == null) return;
   1.200 +        int root_prefix = root.getPath().length()+1;
   1.201 +        // This is the root source directory, it must not contain any Java sources files
   1.202 +        // because we do not allow Java source files without a package.
   1.203 +        // (Unless of course --permit-sources-without-package has been specified.)
   1.204 +        // It might contain other source files however, (for -tr and -copy) these will
   1.205 +        // always be included, since no package pattern can match the root directory.
   1.206 +        currentModule = addFilesInDir(root, root_prefix, root, suffixes, permitSourcesWithoutPackage,
   1.207 +                                       excludeFiles, includeFiles, false,
   1.208 +                                       foundFiles, foundModules, currentModule,
   1.209 +                                       inGensrc, inLinksrc);
   1.210 +
   1.211 +        File[] dirfiles = root.listFiles();
   1.212 +        for (File d : dirfiles) {
   1.213 +            if (d.isDirectory()) {
   1.214 +                // Descend into the directory structure.
   1.215 +                scanDirectory(d, root_prefix, root, suffixes,
   1.216 +                              excludes, includes, excludeFiles, includeFiles,
   1.217 +                              false, foundFiles, foundModules, currentModule, inGensrc, inLinksrc);
   1.218 +            }
   1.219 +        }
   1.220 +    }
   1.221 +
   1.222 +    /**
   1.223 +     * Test if a path matches any of the patterns given.
   1.224 +     * The pattern foo.bar matches only foo.bar
   1.225 +     * The pattern foo.* matches foo.bar and foo.bar.zoo etc
   1.226 +     */
   1.227 +    static private boolean hasMatch(String path, List<String> patterns) {
   1.228 +        for (String p : patterns) {
   1.229 +            // Exact match
   1.230 +            if (p.equals(path)) {
   1.231 +                return true;
   1.232 +            }
   1.233 +            // Single dot the end matches this package and all its subpackages.
   1.234 +            if (p.endsWith(".*")) {
   1.235 +                // Remove the wildcard
   1.236 +                String patprefix = p.substring(0,p.length()-2);
   1.237 +                // Does the path start with the pattern prefix?
   1.238 +                if (path.startsWith(patprefix)) {
   1.239 +                    // If the path has the same length as the pattern prefix, then it is a match.
   1.240 +                    // If the path is longer, then make sure that
   1.241 +                    // the next part of the path starts with a dot (.) to prevent
   1.242 +                    // wildcard matching in the middle of a package name.
   1.243 +                    if (path.length()==patprefix.length() || path.charAt(patprefix.length())=='.') {
   1.244 +                        return true;
   1.245 +                    }
   1.246 +                }
   1.247 +            }
   1.248 +        }
   1.249 +        return false;
   1.250 +    }
   1.251 +
   1.252 +    /**
   1.253 +     * Matches patterns with the asterisk first. */
   1.254 +     // The pattern foo/bar.java only matches foo/bar.java
   1.255 +     // The pattern */bar.java matches foo/bar.java and zoo/bar.java etc
   1.256 +    static private boolean hasFileMatch(String path, List<String> patterns) {
   1.257 +        path = Util.normalizeDriveLetter(path);
   1.258 +        for (String p : patterns) {
   1.259 +            // Exact match
   1.260 +            if (p.equals(path)) {
   1.261 +                return true;
   1.262 +            }
   1.263 +            // Single dot the end matches this package and all its subpackages.
   1.264 +            if (p.startsWith("*")) {
   1.265 +                // Remove the wildcard
   1.266 +                String patsuffix = p.substring(1);
   1.267 +                // Does the path start with the pattern prefix?
   1.268 +                if (path.endsWith(patsuffix)) {
   1.269 +                    return true;
   1.270 +                }
   1.271 +            }
   1.272 +        }
   1.273 +        return false;
   1.274 +    }
   1.275 +
   1.276 +    /**
   1.277 +     * Add the files in the directory, assuming that the file has not been excluded.
   1.278 +     * Returns a fresh Module object, if this was a dir with a module-info.java file.
   1.279 +     */
   1.280 +    static private Module addFilesInDir(File dir, int rootPrefix, File root,
   1.281 +                                        Set<String> suffixes, boolean allow_javas,
   1.282 +                                        List<String> excludeFiles, List<String> includeFiles, boolean all,
   1.283 +                                        Map<String,Source> foundFiles,
   1.284 +                                        Map<String,Module> foundModules,
   1.285 +                                        Module currentModule,
   1.286 +                                        boolean inGensrc,
   1.287 +                                        boolean inLinksrc)
   1.288 +        throws ProblemException
   1.289 +    {
   1.290 +        for (File f : dir.listFiles()) {
   1.291 +            if (f.isFile()) {
   1.292 +                boolean should_add =
   1.293 +                    (excludeFiles == null || excludeFiles.isEmpty() || !hasFileMatch(f.getPath(), excludeFiles))
   1.294 +                    && (includeFiles == null || includeFiles.isEmpty() || hasFileMatch(f.getPath(), includeFiles));
   1.295 +
   1.296 +                if (should_add) {
   1.297 +                    if (!allow_javas && f.getName().endsWith(".java")) {
   1.298 +                        throw new ProblemException("No .java files are allowed in the source root "+dir.getPath()+
   1.299 +                                                   ", please remove "+f.getName());
   1.300 +                    }
   1.301 +                    // Extract the file name relative the root.
   1.302 +                    String fn = f.getPath().substring(rootPrefix);
   1.303 +                    // Extract the package name.
   1.304 +                    int sp = fn.lastIndexOf(File.separatorChar);
   1.305 +                    String pkg = "";
   1.306 +                    if (sp != -1) {
   1.307 +                        pkg = fn.substring(0,sp).replace(File.separatorChar,'.');
   1.308 +                    }
   1.309 +                    // Is this a module-info.java file?
   1.310 +                    if (fn.endsWith("module-info.java")) {
   1.311 +                        // Aha! We have recursed into a module!
   1.312 +                        if (!currentModule.name().equals("")) {
   1.313 +                            throw new ProblemException("You have an extra module-info.java inside a module! Please remove "+fn);
   1.314 +                        }
   1.315 +                        String module_name = fn.substring(0,fn.length()-16);
   1.316 +                        currentModule = new Module(module_name, f.getPath());
   1.317 +                        foundModules.put(module_name, currentModule);
   1.318 +                    }
   1.319 +                    // Extract the suffix.
   1.320 +                    int dp = fn.lastIndexOf(".");
   1.321 +                    String suffix = "";
   1.322 +                    if (dp > 0) {
   1.323 +                        suffix = fn.substring(dp);
   1.324 +                    }
   1.325 +                    // Should the file be added?
   1.326 +                    if (all || suffixes.contains(suffix)) {
   1.327 +                        Source of = foundFiles.get(f.getPath());
   1.328 +                        if (of != null) {
   1.329 +                            throw new ProblemException("You have already added the file "+fn+" from "+of.file().getPath());
   1.330 +                        }
   1.331 +                        of = currentModule.lookupSource(f.getPath());
   1.332 +                        if (of != null) {
   1.333 +                            // Oups, the source is already added, could be ok, could be not, lets check.
   1.334 +                            if (inLinksrc) {
   1.335 +                                // So we are collecting sources for linking only.
   1.336 +                                if (of.isLinkedOnly()) {
   1.337 +                                    // Ouch, this one is also for linking only. Bad.
   1.338 +                                    throw new ProblemException("You have already added the link only file "+fn+" from "+of.file().getPath());
   1.339 +                                }
   1.340 +                                // Ok, the existing source is to be compiled. Thus this link only is redundant
   1.341 +                                // since all compiled are also linked to. Continue to the next source.
   1.342 +                                // But we need to add the source, so that it will be visible to linking,
   1.343 +                                // if not the multi core compile will fail because a JavaCompiler cannot
   1.344 +                                // find the necessary dependencies for its part of the source.
   1.345 +                                foundFiles.put(f.getPath(), of);
   1.346 +                                continue;
   1.347 +                            } else {
   1.348 +                                // We are looking for sources to compile, if we find an existing to be compiled
   1.349 +                                // source with the same name, it is an internal error, since we must
   1.350 +                                // find the sources to be compiled before we find the sources to be linked to.
   1.351 +                                throw new ProblemException("Internal error: Double add of file "+fn+" from "+of.file().getPath());
   1.352 +                            }
   1.353 +                        }
   1.354 +                        Source s = new Source(currentModule, f.getPath(), f, root);
   1.355 +                        if (inGensrc) s.markAsGenerated();
   1.356 +                        if (inLinksrc) {
   1.357 +                            s.markAsLinkedOnly();
   1.358 +                        }
   1.359 +                        pkg = currentModule.name()+":"+pkg;
   1.360 +                        foundFiles.put(f.getPath(), s);
   1.361 +                        currentModule.addSource(pkg, s);
   1.362 +                    }
   1.363 +                }
   1.364 +            }
   1.365 +        }
   1.366 +        return currentModule;
   1.367 +    }
   1.368 +
   1.369 +    private static boolean gurka = false;
   1.370 +
   1.371 +    static private void scanDirectory(File dir, int rootPrefix, File root,
   1.372 +                                      Set<String> suffixes,
   1.373 +                                      List<String> excludes, List<String> includes,
   1.374 +                                      List<String> excludeFiles, List<String> includeFiles, boolean all,
   1.375 +                                      Map<String,Source> foundFiles,
   1.376 +                                      Map<String,Module> foundModules,
   1.377 +                                      Module currentModule, boolean inGensrc, boolean inLinksrc)
   1.378 +        throws ProblemException {
   1.379 +
   1.380 +        String pkg_name = "";
   1.381 +        // Remove the root prefix from the dir path, and replace file separator with dots
   1.382 +        // to get the package name.
   1.383 +        if (dir.getPath().length() > rootPrefix) {
   1.384 +            pkg_name = dir.getPath().substring(rootPrefix).replace(File.separatorChar,'.');
   1.385 +        }
   1.386 +        // Should this package directory be included and not excluded?
   1.387 +        if (all || ((includes==null || includes.isEmpty() || hasMatch(pkg_name, includes)) &&
   1.388 +                    (excludes==null || excludes.isEmpty() || !hasMatch(pkg_name, excludes)))) {
   1.389 +            // Add the source files.
   1.390 +            currentModule = addFilesInDir(dir, rootPrefix, root, suffixes, true, excludeFiles, includeFiles, all,
   1.391 +                                          foundFiles, foundModules, currentModule, inGensrc, inLinksrc);
   1.392 +        }
   1.393 +
   1.394 +        for (File d : dir.listFiles()) {
   1.395 +            if (d.isDirectory()) {
   1.396 +                // Descend into the directory structure.
   1.397 +                scanDirectory(d, rootPrefix, root, suffixes,
   1.398 +                              excludes, includes, excludeFiles, includeFiles, all,
   1.399 +                              foundFiles, foundModules, currentModule, inGensrc, inLinksrc);
   1.400 +            }
   1.401 +        }
   1.402 +    }
   1.403 +}

mercurial