Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/sjavac/Source.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.Set;29import java.util.Collections;30import java.util.List;31import java.util.ArrayList;32import java.util.Map;3334/** A Source object maintains information about a source file.35* For example which package it belongs to and kind of source it is.36* The class also knows how to find source files (scanRoot) given include/exclude37* patterns and a root.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 own41* risk. This code and its internal interfaces are subject to change42* or deletion without notice.</b></p>43*/44public class Source implements Comparable<Source> {45// The package the source belongs to.46private Package pkg;47// Name of this source file, relative its source root.48// For example: java/lang/Object.java49// Or if the source file is inside a module:50// jdk.base/java/lang/Object.java51private String name;52// What kind of file is this.53private String suffix;54// When this source file was last_modified55private long lastModified;56// The source File.57private File file;58// The source root under which file resides.59private File root;60// If the source is generated.61private boolean isGenerated;62// If the source is only linked to, not compiled.63private boolean linkedOnly;6465@Override66public boolean equals(Object o) {67return (o instanceof Source) && name.equals(((Source)o).name);68}6970@Override71public int compareTo(Source o) {72return name.compareTo(o.name);73}7475@Override76public int hashCode() {77return name.hashCode();78}7980public Source(Module m, String n, File f, File r) {81name = n;82int dp = n.lastIndexOf(".");83if (dp != -1) {84suffix = n.substring(dp);85} else {86suffix = "";87}88file = f;89root = r;90lastModified = f.lastModified();91linkedOnly = false;92}9394public Source(Package p, String n, long lm) {95pkg = p;96name = n;97int dp = n.lastIndexOf(".");98if (dp != -1) {99suffix = n.substring(dp);100} else {101suffix = "";102}103file = null;104root = null;105lastModified = lm;106linkedOnly = false;107int ls = n.lastIndexOf('/');108}109110public String name() { return name; }111public String suffix() { return suffix; }112public Package pkg() { return pkg; }113public File file() { return file; }114public File root() { return root; }115public long lastModified() {116return lastModified;117}118119public void setPackage(Package p) {120pkg = p;121}122123public void markAsGenerated() {124isGenerated = true;125}126127public boolean isGenerated() {128return isGenerated;129}130131public void markAsLinkedOnly() {132linkedOnly = true;133}134135public boolean isLinkedOnly() {136return linkedOnly;137}138139private void save(StringBuilder b) {140String CL = linkedOnly?"L":"C";141String GS = isGenerated?"G":"S";142b.append(GS+" "+CL+" "+name+" "+file.lastModified()+"\n");143}144// Parse a line that looks like this:145// S C /code/alfa/A.java 1357631228000146static public Source load(Package lastPackage, String l, boolean isGenerated) {147int sp = l.indexOf(' ',4);148if (sp == -1) return null;149String name = l.substring(4,sp);150long last_modified = Long.parseLong(l.substring(sp+1));151152boolean isLinkedOnly = false;153if (l.charAt(2) == 'L') {154isLinkedOnly = true;155} else if (l.charAt(2) == 'C') {156isLinkedOnly = false;157} else return null;158159Source s = new Source(lastPackage, name, last_modified);160s.file = new File(name);161if (isGenerated) s.markAsGenerated();162if (isLinkedOnly) s.markAsLinkedOnly();163return s;164}165166public static void saveSources(Map<String,Source> sources, StringBuilder b) {167List<String> sorted_sources = new ArrayList<String>();168for (String key : sources.keySet()) {169sorted_sources.add(key);170}171Collections.sort(sorted_sources);172for (String key : sorted_sources) {173Source s = sources.get(key);174s.save(b);175}176}177178/**179* Recurse into the directory root and find all files matchine the excl/incl/exclfiles/inclfiles rules.180* Detects the existence of module-info.java files and presumes that the directory it resides in181* is the name of the current module.182*/183static public void scanRoot(File root,184Set<String> suffixes,185List<String> excludes, List<String> includes,186List<String> excludeFiles, List<String> includeFiles,187Map<String,Source> foundFiles,188Map<String,Module> foundModules,189Module currentModule,190boolean permitSourcesWithoutPackage,191boolean inGensrc,192boolean inLinksrc)193throws ProblemException {194195if (root == null) return;196int root_prefix = root.getPath().length()+1;197// This is the root source directory, it must not contain any Java sources files198// because we do not allow Java source files without a package.199// (Unless of course --permit-sources-without-package has been specified.)200// It might contain other source files however, (for -tr and -copy) these will201// always be included, since no package pattern can match the root directory.202currentModule = addFilesInDir(root, root_prefix, root, suffixes, permitSourcesWithoutPackage,203excludeFiles, includeFiles, false,204foundFiles, foundModules, currentModule,205inGensrc, inLinksrc);206207File[] dirfiles = root.listFiles();208for (File d : dirfiles) {209if (d.isDirectory()) {210// Descend into the directory structure.211scanDirectory(d, root_prefix, root, suffixes,212excludes, includes, excludeFiles, includeFiles,213false, foundFiles, foundModules, currentModule, inGensrc, inLinksrc);214}215}216}217218/**219* Test if a path matches any of the patterns given.220* The pattern foo.bar matches only foo.bar221* The pattern foo.* matches foo.bar and foo.bar.zoo etc222*/223static private boolean hasMatch(String path, List<String> patterns) {224for (String p : patterns) {225// Exact match226if (p.equals(path)) {227return true;228}229// Single dot the end matches this package and all its subpackages.230if (p.endsWith(".*")) {231// Remove the wildcard232String patprefix = p.substring(0,p.length()-2);233// Does the path start with the pattern prefix?234if (path.startsWith(patprefix)) {235// If the path has the same length as the pattern prefix, then it is a match.236// If the path is longer, then make sure that237// the next part of the path starts with a dot (.) to prevent238// wildcard matching in the middle of a package name.239if (path.length()==patprefix.length() || path.charAt(patprefix.length())=='.') {240return true;241}242}243}244}245return false;246}247248/**249* Matches patterns with the asterisk first. */250// The pattern foo/bar.java only matches foo/bar.java251// The pattern */bar.java matches foo/bar.java and zoo/bar.java etc252static private boolean hasFileMatch(String path, List<String> patterns) {253path = Util.normalizeDriveLetter(path);254for (String p : patterns) {255// Exact match256if (p.equals(path)) {257return true;258}259// Single dot the end matches this package and all its subpackages.260if (p.startsWith("*")) {261// Remove the wildcard262String patsuffix = p.substring(1);263// Does the path start with the pattern prefix?264if (path.endsWith(patsuffix)) {265return true;266}267}268}269return false;270}271272/**273* Add the files in the directory, assuming that the file has not been excluded.274* Returns a fresh Module object, if this was a dir with a module-info.java file.275*/276static private Module addFilesInDir(File dir, int rootPrefix, File root,277Set<String> suffixes, boolean allow_javas,278List<String> excludeFiles, List<String> includeFiles, boolean all,279Map<String,Source> foundFiles,280Map<String,Module> foundModules,281Module currentModule,282boolean inGensrc,283boolean inLinksrc)284throws ProblemException285{286for (File f : dir.listFiles()) {287if (f.isFile()) {288boolean should_add =289(excludeFiles == null || excludeFiles.isEmpty() || !hasFileMatch(f.getPath(), excludeFiles))290&& (includeFiles == null || includeFiles.isEmpty() || hasFileMatch(f.getPath(), includeFiles));291292if (should_add) {293if (!allow_javas && f.getName().endsWith(".java")) {294throw new ProblemException("No .java files are allowed in the source root "+dir.getPath()+295", please remove "+f.getName());296}297// Extract the file name relative the root.298String fn = f.getPath().substring(rootPrefix);299// Extract the package name.300int sp = fn.lastIndexOf(File.separatorChar);301String pkg = "";302if (sp != -1) {303pkg = fn.substring(0,sp).replace(File.separatorChar,'.');304}305// Is this a module-info.java file?306if (fn.endsWith("module-info.java")) {307// Aha! We have recursed into a module!308if (!currentModule.name().equals("")) {309throw new ProblemException("You have an extra module-info.java inside a module! Please remove "+fn);310}311String module_name = fn.substring(0,fn.length()-16);312currentModule = new Module(module_name, f.getPath());313foundModules.put(module_name, currentModule);314}315// Extract the suffix.316int dp = fn.lastIndexOf(".");317String suffix = "";318if (dp > 0) {319suffix = fn.substring(dp);320}321// Should the file be added?322if (all || suffixes.contains(suffix)) {323Source of = foundFiles.get(f.getPath());324if (of != null) {325throw new ProblemException("You have already added the file "+fn+" from "+of.file().getPath());326}327of = currentModule.lookupSource(f.getPath());328if (of != null) {329// Oups, the source is already added, could be ok, could be not, lets check.330if (inLinksrc) {331// So we are collecting sources for linking only.332if (of.isLinkedOnly()) {333// Ouch, this one is also for linking only. Bad.334throw new ProblemException("You have already added the link only file "+fn+" from "+of.file().getPath());335}336// Ok, the existing source is to be compiled. Thus this link only is redundant337// since all compiled are also linked to. Continue to the next source.338// But we need to add the source, so that it will be visible to linking,339// if not the multi core compile will fail because a JavaCompiler cannot340// find the necessary dependencies for its part of the source.341foundFiles.put(f.getPath(), of);342continue;343} else {344// We are looking for sources to compile, if we find an existing to be compiled345// source with the same name, it is an internal error, since we must346// find the sources to be compiled before we find the sources to be linked to.347throw new ProblemException("Internal error: Double add of file "+fn+" from "+of.file().getPath());348}349}350Source s = new Source(currentModule, f.getPath(), f, root);351if (inGensrc) s.markAsGenerated();352if (inLinksrc) {353s.markAsLinkedOnly();354}355pkg = currentModule.name()+":"+pkg;356foundFiles.put(f.getPath(), s);357currentModule.addSource(pkg, s);358}359}360}361}362return currentModule;363}364365private static boolean gurka = false;366367static private void scanDirectory(File dir, int rootPrefix, File root,368Set<String> suffixes,369List<String> excludes, List<String> includes,370List<String> excludeFiles, List<String> includeFiles, boolean all,371Map<String,Source> foundFiles,372Map<String,Module> foundModules,373Module currentModule, boolean inGensrc, boolean inLinksrc)374throws ProblemException {375376String pkg_name = "";377// Remove the root prefix from the dir path, and replace file separator with dots378// to get the package name.379if (dir.getPath().length() > rootPrefix) {380pkg_name = dir.getPath().substring(rootPrefix).replace(File.separatorChar,'.');381}382// Should this package directory be included and not excluded?383if (all || ((includes==null || includes.isEmpty() || hasMatch(pkg_name, includes)) &&384(excludes==null || excludes.isEmpty() || !hasMatch(pkg_name, excludes)))) {385// Add the source files.386currentModule = addFilesInDir(dir, rootPrefix, root, suffixes, true, excludeFiles, includeFiles, all,387foundFiles, foundModules, currentModule, inGensrc, inLinksrc);388}389390for (File d : dir.listFiles()) {391if (d.isDirectory()) {392// Descend into the directory structure.393scanDirectory(d, rootPrefix, root, suffixes,394excludes, includes, excludeFiles, includeFiles, all,395foundFiles, foundModules, currentModule, inGensrc, inLinksrc);396}397}398}399}400401402