ohrstrom@1504: /* ohrstrom@1504: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohrstrom@1504: * ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code. ohrstrom@1504: * ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that ohrstrom@1504: * accompanied this code). ohrstrom@1504: * ohrstrom@1504: * You should have received a copy of the GNU General Public License version ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation, ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohrstrom@1504: * ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any ohrstrom@1504: * questions. ohrstrom@1504: */ ohrstrom@1504: ohrstrom@1504: package com.sun.tools.sjavac; ohrstrom@1504: ohrstrom@1504: import java.io.File; ohrstrom@1504: import java.util.Set; ohrstrom@1504: import java.util.Collections; ohrstrom@1504: import java.util.List; ohrstrom@1504: import java.util.ArrayList; ohrstrom@1504: import java.util.Map; ohrstrom@1504: ohrstrom@1504: /** A Source object maintains information about a source file. ohrstrom@1504: * For example which package it belongs to and kind of source it is. ohrstrom@1504: * The class also knows how to find source files (scanRoot) given include/exclude ohrstrom@1504: * patterns and a root. ohrstrom@1504: * ohrstrom@1504: *

This is NOT part of any supported API. ohrstrom@1504: * If you write code that depends on this, you do so at your own ohrstrom@1504: * risk. This code and its internal interfaces are subject to change ohrstrom@1504: * or deletion without notice.

ohrstrom@1504: */ ohrstrom@1504: public class Source implements Comparable { ohrstrom@1504: // The package the source belongs to. ohrstrom@1504: private Package pkg; ohrstrom@1504: // Name of this source file, relative its source root. ohrstrom@1504: // For example: java/lang/Object.java ohrstrom@1504: // Or if the source file is inside a module: ohrstrom@1504: // jdk.base/java/lang/Object.java ohrstrom@1504: private String name; ohrstrom@1504: // What kind of file is this. ohrstrom@1504: private String suffix; ohrstrom@1504: // When this source file was last_modified ohrstrom@1504: private long lastModified; ohrstrom@1504: // The source File. ohrstrom@1504: private File file; ohrstrom@1504: // The source root under which file resides. ohrstrom@1504: private File root; ohrstrom@1504: // If the source is generated. ohrstrom@1504: private boolean isGenerated; ohrstrom@1504: // If the source is only linked to, not compiled. ohrstrom@1504: private boolean linkedOnly; ohrstrom@1504: ohrstrom@1504: @Override ohrstrom@1504: public boolean equals(Object o) { ohrstrom@1504: return (o instanceof Source) && name.equals(((Source)o).name); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: @Override ohrstrom@1504: public int compareTo(Source o) { ohrstrom@1504: return name.compareTo(o.name); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: @Override ohrstrom@1504: public int hashCode() { ohrstrom@1504: return name.hashCode(); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public Source(Module m, String n, File f, File r) { ohrstrom@1504: name = n; ohrstrom@1504: int dp = n.lastIndexOf("."); ohrstrom@1504: if (dp != -1) { ohrstrom@1504: suffix = n.substring(dp); ohrstrom@1504: } else { ohrstrom@1504: suffix = ""; ohrstrom@1504: } ohrstrom@1504: file = f; ohrstrom@1504: root = r; ohrstrom@1504: lastModified = f.lastModified(); ohrstrom@1504: linkedOnly = false; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public Source(Package p, String n, long lm) { ohrstrom@1504: pkg = p; ohrstrom@1504: name = n; ohrstrom@1504: int dp = n.lastIndexOf("."); ohrstrom@1504: if (dp != -1) { ohrstrom@1504: suffix = n.substring(dp); ohrstrom@1504: } else { ohrstrom@1504: suffix = ""; ohrstrom@1504: } ohrstrom@1504: file = null; ohrstrom@1504: root = null; ohrstrom@1504: lastModified = lm; ohrstrom@1504: linkedOnly = false; ohrstrom@1504: int ls = n.lastIndexOf('/'); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public String name() { return name; } ohrstrom@1504: public String suffix() { return suffix; } ohrstrom@1504: public Package pkg() { return pkg; } ohrstrom@1504: public File file() { return file; } ohrstrom@1504: public File root() { return root; } ohrstrom@1504: public long lastModified() { ohrstrom@1504: return lastModified; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public void setPackage(Package p) { ohrstrom@1504: pkg = p; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public void markAsGenerated() { ohrstrom@1504: isGenerated = true; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public boolean isGenerated() { ohrstrom@1504: return isGenerated; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public void markAsLinkedOnly() { ohrstrom@1504: linkedOnly = true; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public boolean isLinkedOnly() { ohrstrom@1504: return linkedOnly; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: private void save(StringBuilder b) { ohrstrom@1504: String CL = linkedOnly?"L":"C"; ohrstrom@1504: String GS = isGenerated?"G":"S"; ohrstrom@1504: b.append(GS+" "+CL+" "+name+" "+file.lastModified()+"\n"); ohrstrom@1504: } ohrstrom@1504: // Parse a line that looks like this: ohrstrom@1504: // S C /code/alfa/A.java 1357631228000 ohrstrom@1504: static public Source load(Package lastPackage, String l, boolean isGenerated) { ohrstrom@1504: int sp = l.indexOf(' ',4); ohrstrom@1504: if (sp == -1) return null; ohrstrom@1504: String name = l.substring(4,sp); ohrstrom@1504: long last_modified = Long.parseLong(l.substring(sp+1)); ohrstrom@1504: ohrstrom@1504: boolean isLinkedOnly = false; ohrstrom@1504: if (l.charAt(2) == 'L') { ohrstrom@1504: isLinkedOnly = true; ohrstrom@1504: } else if (l.charAt(2) == 'C') { ohrstrom@1504: isLinkedOnly = false; ohrstrom@1504: } else return null; ohrstrom@1504: ohrstrom@1504: Source s = new Source(lastPackage, name, last_modified); ohrstrom@1504: s.file = new File(name); ohrstrom@1504: if (isGenerated) s.markAsGenerated(); ohrstrom@1504: if (isLinkedOnly) s.markAsLinkedOnly(); ohrstrom@1504: return s; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public static void saveSources(Map sources, StringBuilder b) { ohrstrom@1504: List sorted_sources = new ArrayList(); ohrstrom@1504: for (String key : sources.keySet()) { ohrstrom@1504: sorted_sources.add(key); ohrstrom@1504: } ohrstrom@1504: Collections.sort(sorted_sources); ohrstrom@1504: for (String key : sorted_sources) { ohrstrom@1504: Source s = sources.get(key); ohrstrom@1504: s.save(b); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Recurse into the directory root and find all files matchine the excl/incl/exclfiles/inclfiles rules. ohrstrom@1504: * Detects the existence of module-info.java files and presumes that the directory it resides in ohrstrom@1504: * is the name of the current module. ohrstrom@1504: */ ohrstrom@1504: static public void scanRoot(File root, ohrstrom@1504: Set suffixes, ohrstrom@1504: List excludes, List includes, ohrstrom@1504: List excludeFiles, List includeFiles, ohrstrom@1504: Map foundFiles, ohrstrom@1504: Map foundModules, ohrstrom@1504: Module currentModule, ohrstrom@1504: boolean permitSourcesWithoutPackage, ohrstrom@1504: boolean inGensrc, ohrstrom@1504: boolean inLinksrc) ohrstrom@1504: throws ProblemException { ohrstrom@1504: ohrstrom@1504: if (root == null) return; ohrstrom@1504: int root_prefix = root.getPath().length()+1; ohrstrom@1504: // This is the root source directory, it must not contain any Java sources files ohrstrom@1504: // because we do not allow Java source files without a package. ohrstrom@1504: // (Unless of course --permit-sources-without-package has been specified.) ohrstrom@1504: // It might contain other source files however, (for -tr and -copy) these will ohrstrom@1504: // always be included, since no package pattern can match the root directory. ohrstrom@1504: currentModule = addFilesInDir(root, root_prefix, root, suffixes, permitSourcesWithoutPackage, ohrstrom@1504: excludeFiles, includeFiles, false, ohrstrom@1504: foundFiles, foundModules, currentModule, ohrstrom@1504: inGensrc, inLinksrc); ohrstrom@1504: ohrstrom@1504: File[] dirfiles = root.listFiles(); ohrstrom@1504: for (File d : dirfiles) { ohrstrom@1504: if (d.isDirectory()) { ohrstrom@1504: // Descend into the directory structure. ohrstrom@1504: scanDirectory(d, root_prefix, root, suffixes, ohrstrom@1504: excludes, includes, excludeFiles, includeFiles, ohrstrom@1504: false, foundFiles, foundModules, currentModule, inGensrc, inLinksrc); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Test if a path matches any of the patterns given. ohrstrom@1504: * The pattern foo.bar matches only foo.bar ohrstrom@1504: * The pattern foo.* matches foo.bar and foo.bar.zoo etc ohrstrom@1504: */ ohrstrom@1504: static private boolean hasMatch(String path, List patterns) { ohrstrom@1504: for (String p : patterns) { ohrstrom@1504: // Exact match ohrstrom@1504: if (p.equals(path)) { ohrstrom@1504: return true; ohrstrom@1504: } ohrstrom@1504: // Single dot the end matches this package and all its subpackages. ohrstrom@1504: if (p.endsWith(".*")) { ohrstrom@1504: // Remove the wildcard ohrstrom@1504: String patprefix = p.substring(0,p.length()-2); ohrstrom@1504: // Does the path start with the pattern prefix? ohrstrom@1504: if (path.startsWith(patprefix)) { ohrstrom@1504: // If the path has the same length as the pattern prefix, then it is a match. ohrstrom@1504: // If the path is longer, then make sure that ohrstrom@1504: // the next part of the path starts with a dot (.) to prevent ohrstrom@1504: // wildcard matching in the middle of a package name. ohrstrom@1504: if (path.length()==patprefix.length() || path.charAt(patprefix.length())=='.') { ohrstrom@1504: return true; ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: return false; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Matches patterns with the asterisk first. */ ohrstrom@1504: // The pattern foo/bar.java only matches foo/bar.java ohrstrom@1504: // The pattern */bar.java matches foo/bar.java and zoo/bar.java etc ohrstrom@1504: static private boolean hasFileMatch(String path, List patterns) { ohrstrom@1504: path = Util.normalizeDriveLetter(path); ohrstrom@1504: for (String p : patterns) { ohrstrom@1504: // Exact match ohrstrom@1504: if (p.equals(path)) { ohrstrom@1504: return true; ohrstrom@1504: } ohrstrom@1504: // Single dot the end matches this package and all its subpackages. ohrstrom@1504: if (p.startsWith("*")) { ohrstrom@1504: // Remove the wildcard ohrstrom@1504: String patsuffix = p.substring(1); ohrstrom@1504: // Does the path start with the pattern prefix? ohrstrom@1504: if (path.endsWith(patsuffix)) { ohrstrom@1504: return true; ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: return false; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Add the files in the directory, assuming that the file has not been excluded. ohrstrom@1504: * Returns a fresh Module object, if this was a dir with a module-info.java file. ohrstrom@1504: */ ohrstrom@1504: static private Module addFilesInDir(File dir, int rootPrefix, File root, ohrstrom@1504: Set suffixes, boolean allow_javas, ohrstrom@1504: List excludeFiles, List includeFiles, boolean all, ohrstrom@1504: Map foundFiles, ohrstrom@1504: Map foundModules, ohrstrom@1504: Module currentModule, ohrstrom@1504: boolean inGensrc, ohrstrom@1504: boolean inLinksrc) ohrstrom@1504: throws ProblemException ohrstrom@1504: { ohrstrom@1504: for (File f : dir.listFiles()) { ohrstrom@1504: if (f.isFile()) { ohrstrom@1504: boolean should_add = ohrstrom@1504: (excludeFiles == null || excludeFiles.isEmpty() || !hasFileMatch(f.getPath(), excludeFiles)) ohrstrom@1504: && (includeFiles == null || includeFiles.isEmpty() || hasFileMatch(f.getPath(), includeFiles)); ohrstrom@1504: ohrstrom@1504: if (should_add) { ohrstrom@1504: if (!allow_javas && f.getName().endsWith(".java")) { ohrstrom@1504: throw new ProblemException("No .java files are allowed in the source root "+dir.getPath()+ ohrstrom@1504: ", please remove "+f.getName()); ohrstrom@1504: } ohrstrom@1504: // Extract the file name relative the root. ohrstrom@1504: String fn = f.getPath().substring(rootPrefix); ohrstrom@1504: // Extract the package name. ohrstrom@1504: int sp = fn.lastIndexOf(File.separatorChar); ohrstrom@1504: String pkg = ""; ohrstrom@1504: if (sp != -1) { ohrstrom@1504: pkg = fn.substring(0,sp).replace(File.separatorChar,'.'); ohrstrom@1504: } ohrstrom@1504: // Is this a module-info.java file? ohrstrom@1504: if (fn.endsWith("module-info.java")) { ohrstrom@1504: // Aha! We have recursed into a module! ohrstrom@1504: if (!currentModule.name().equals("")) { ohrstrom@1504: throw new ProblemException("You have an extra module-info.java inside a module! Please remove "+fn); ohrstrom@1504: } ohrstrom@1504: String module_name = fn.substring(0,fn.length()-16); ohrstrom@1504: currentModule = new Module(module_name, f.getPath()); ohrstrom@1504: foundModules.put(module_name, currentModule); ohrstrom@1504: } ohrstrom@1504: // Extract the suffix. ohrstrom@1504: int dp = fn.lastIndexOf("."); ohrstrom@1504: String suffix = ""; ohrstrom@1504: if (dp > 0) { ohrstrom@1504: suffix = fn.substring(dp); ohrstrom@1504: } ohrstrom@1504: // Should the file be added? ohrstrom@1504: if (all || suffixes.contains(suffix)) { ohrstrom@1504: Source of = foundFiles.get(f.getPath()); ohrstrom@1504: if (of != null) { ohrstrom@1504: throw new ProblemException("You have already added the file "+fn+" from "+of.file().getPath()); ohrstrom@1504: } ohrstrom@1504: of = currentModule.lookupSource(f.getPath()); ohrstrom@1504: if (of != null) { ohrstrom@1504: // Oups, the source is already added, could be ok, could be not, lets check. ohrstrom@1504: if (inLinksrc) { ohrstrom@1504: // So we are collecting sources for linking only. ohrstrom@1504: if (of.isLinkedOnly()) { ohrstrom@1504: // Ouch, this one is also for linking only. Bad. ohrstrom@1504: throw new ProblemException("You have already added the link only file "+fn+" from "+of.file().getPath()); ohrstrom@1504: } ohrstrom@1504: // Ok, the existing source is to be compiled. Thus this link only is redundant ohrstrom@1504: // since all compiled are also linked to. Continue to the next source. ohrstrom@1504: // But we need to add the source, so that it will be visible to linking, ohrstrom@1504: // if not the multi core compile will fail because a JavaCompiler cannot ohrstrom@1504: // find the necessary dependencies for its part of the source. ohrstrom@1504: foundFiles.put(f.getPath(), of); ohrstrom@1504: continue; ohrstrom@1504: } else { ohrstrom@1504: // We are looking for sources to compile, if we find an existing to be compiled ohrstrom@1504: // source with the same name, it is an internal error, since we must ohrstrom@1504: // find the sources to be compiled before we find the sources to be linked to. ohrstrom@1504: throw new ProblemException("Internal error: Double add of file "+fn+" from "+of.file().getPath()); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: Source s = new Source(currentModule, f.getPath(), f, root); ohrstrom@1504: if (inGensrc) s.markAsGenerated(); ohrstrom@1504: if (inLinksrc) { ohrstrom@1504: s.markAsLinkedOnly(); ohrstrom@1504: } ohrstrom@1504: pkg = currentModule.name()+":"+pkg; ohrstrom@1504: foundFiles.put(f.getPath(), s); ohrstrom@1504: currentModule.addSource(pkg, s); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: return currentModule; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: private static boolean gurka = false; ohrstrom@1504: ohrstrom@1504: static private void scanDirectory(File dir, int rootPrefix, File root, ohrstrom@1504: Set suffixes, ohrstrom@1504: List excludes, List includes, ohrstrom@1504: List excludeFiles, List includeFiles, boolean all, ohrstrom@1504: Map foundFiles, ohrstrom@1504: Map foundModules, ohrstrom@1504: Module currentModule, boolean inGensrc, boolean inLinksrc) ohrstrom@1504: throws ProblemException { ohrstrom@1504: ohrstrom@1504: String pkg_name = ""; ohrstrom@1504: // Remove the root prefix from the dir path, and replace file separator with dots ohrstrom@1504: // to get the package name. ohrstrom@1504: if (dir.getPath().length() > rootPrefix) { ohrstrom@1504: pkg_name = dir.getPath().substring(rootPrefix).replace(File.separatorChar,'.'); ohrstrom@1504: } ohrstrom@1504: // Should this package directory be included and not excluded? ohrstrom@1504: if (all || ((includes==null || includes.isEmpty() || hasMatch(pkg_name, includes)) && ohrstrom@1504: (excludes==null || excludes.isEmpty() || !hasMatch(pkg_name, excludes)))) { ohrstrom@1504: // Add the source files. ohrstrom@1504: currentModule = addFilesInDir(dir, rootPrefix, root, suffixes, true, excludeFiles, includeFiles, all, ohrstrom@1504: foundFiles, foundModules, currentModule, inGensrc, inLinksrc); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: for (File d : dir.listFiles()) { ohrstrom@1504: if (d.isDirectory()) { ohrstrom@1504: // Descend into the directory structure. ohrstrom@1504: scanDirectory(d, rootPrefix, root, suffixes, ohrstrom@1504: excludes, includes, excludeFiles, includeFiles, all, ohrstrom@1504: foundFiles, foundModules, currentModule, inGensrc, inLinksrc); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: }