package org.eclipse.jdt.internal.compiler.batch;
import java.io.File;
import java.io.IOException;
import java.nio.file.InvalidPathException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.zip.ZipFile;
import javax.lang.model.SourceVersion;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ExternalAnnotationDecorator;
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
import org.eclipse.jdt.internal.compiler.env.IModulePathEntry;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.IModuleAwareNameEnvironment;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.lookup.ModuleBinding;
import org.eclipse.jdt.internal.compiler.parser.Parser;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.compiler.env.IUpdatableModule;
import org.eclipse.jdt.internal.compiler.env.IUpdatableModule.UpdateKind;
import org.eclipse.jdt.internal.compiler.env.IUpdatableModule.UpdatesByKind;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
import org.eclipse.jdt.internal.compiler.util.Util;
public class FileSystem implements IModuleAwareNameEnvironment, SuffixConstants {
public static ArrayList<FileSystem.Classpath> EMPTY_CLASSPATH = new ArrayList<>();
public interface Classpath extends IModulePathEntry {
char[][][] findTypeNames(String qualifiedPackageName, String moduleName);
NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName);
NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly);
boolean isPackage(String qualifiedPackageName, String moduleName);
default boolean hasModule() { return getModule() != null; }
default boolean hasCUDeclaringPackage(String qualifiedPackageName, Function<CompilationUnit, String> pkgNameExtractor) {
return hasCompilationUnit(qualifiedPackageName, null);
}
List<Classpath> fetchLinkedJars(ClasspathSectionProblemReporter problemReporter);
void reset();
char[] normalizedPath();
String getPath();
void initialize() throws IOException;
boolean hasAnnotationFileFor(String qualifiedTypeName);
public void acceptModule(IModule module);
public String getDestinationPath();
Collection<String> getModuleNames(Collection<String> limitModules);
Collection<String> getModuleNames(Collection<String> limitModules, Function<String,IModule> getModule);
}
public interface ClasspathSectionProblemReporter {
void invalidClasspathSection(String jarFilePath);
void multipleClasspathSections(String jarFilePath);
}
public static class ClasspathNormalizer {
public static ArrayList<Classpath> normalize(ArrayList<Classpath> classpaths) {
ArrayList<Classpath> normalizedClasspath = new ArrayList<>();
HashSet<Classpath> cache = new HashSet<>();
for (Iterator<Classpath> iterator = classpaths.iterator(); iterator.hasNext(); ) {
FileSystem.Classpath classpath = iterator.next();
if (!cache.contains(classpath)) {
normalizedClasspath.add(classpath);
cache.add(classpath);
}
}
return normalizedClasspath;
}
}
protected Classpath[] classpaths;
protected IModule module;
Set<String> knownFileNames;
protected boolean annotationsFromClasspath;
private static HashMap<File, Classpath> JRT_CLASSPATH_CACHE = null;
protected Map<String,Classpath> moduleLocations = new HashMap<>();
Map<String,UpdatesByKind> moduleUpdates = new HashMap<>();
static boolean isJRE12Plus = false;
private boolean hasLimitModules = false;
static {
try {
isJRE12Plus = SourceVersion.valueOf("RELEASE_12") != null;
} catch(IllegalArgumentException iae) {
}
}
public FileSystem(String[] classpathNames, String[] initialFileNames, String encoding) {
this(classpathNames, initialFileNames, encoding, null);
}
protected FileSystem(String[] classpathNames, String[] initialFileNames, String encoding, Collection<String> limitModules) {
final int classpathSize = classpathNames.length;
this.classpaths = new Classpath[classpathSize];
int counter = 0;
this.hasLimitModules = limitModules != null && !limitModules.isEmpty();
for (int i = 0; i < classpathSize; i++) {
Classpath classpath = getClasspath(classpathNames[i], encoding, null, null, null);
try {
classpath.initialize();
for (String moduleName : classpath.getModuleNames(limitModules))
this.moduleLocations.put(moduleName, classpath);
this.classpaths[counter++] = classpath;
} catch (IOException e) {
}
}
if (counter != classpathSize) {
System.arraycopy(this.classpaths, 0, (this.classpaths = new Classpath[counter]), 0, counter);
}
initializeKnownFileNames(initialFileNames);
}
protected FileSystem(Classpath[] paths, String[] initialFileNames, boolean annotationsFromClasspath, Set<String> limitedModules) {
final int length = paths.length;
int counter = 0;
this.classpaths = new FileSystem.Classpath[length];
this.hasLimitModules = limitedModules != null && !limitedModules.isEmpty();
for (int i = 0; i < length; i++) {
final Classpath classpath = paths[i];
try {
classpath.initialize();
for (String moduleName : classpath.getModuleNames(limitedModules))
this.moduleLocations.put(moduleName, classpath);
this.classpaths[counter++] = classpath;
} catch(IOException | InvalidPathException exception) {
}
}
if (counter != length) {
System.arraycopy(this.classpaths, 0, (this.classpaths = new FileSystem.Classpath[counter]), 0, counter);
}
initializeModuleLocations(limitedModules);
initializeKnownFileNames(initialFileNames);
this.annotationsFromClasspath = annotationsFromClasspath;
}
private void initializeModuleLocations(Set<String> limitedModules) {
if (limitedModules == null) {
for (Classpath c : this.classpaths) {
for (String moduleName : c.getModuleNames(null))
this.moduleLocations.put(moduleName, c);
}
} else {
Map<String, Classpath> moduleMap = new HashMap<>();
for (Classpath c : this.classpaths) {
for (String moduleName : c.getModuleNames(null)) {
moduleMap.put(moduleName, c);
}
}
for (Classpath c : this.classpaths) {
for (String moduleName : c.getModuleNames(limitedModules, m -> getModuleFromEnvironment(m.toCharArray()))) {
Classpath classpath = moduleMap.get(moduleName);
this.moduleLocations.put(moduleName, classpath);
}
}
}
}
protected FileSystem(Classpath[] paths, String[] initialFileNames, boolean annotationsFromClasspath) {
this(paths, initialFileNames, annotationsFromClasspath, null);
}
public static Classpath getClasspath(String classpathName, String encoding, AccessRuleSet accessRuleSet) {
return getClasspath(classpathName, encoding, false, accessRuleSet, null, null, null);
}
public static Classpath getClasspath(String classpathName, String encoding, AccessRuleSet accessRuleSet, Map<String, String> options, String release) {
return getClasspath(classpathName, encoding, false, accessRuleSet, null, options, release);
}
public static Classpath getJrtClasspath(String jdkHome, String encoding, AccessRuleSet accessRuleSet, Map<String, String> options) {
return new ClasspathJrt(new File(convertPathSeparators(jdkHome)), true, accessRuleSet, null);
}
public static Classpath getOlderSystemRelease(String jdkHome, String release, AccessRuleSet accessRuleSet) {
return isJRE12Plus ?
new ClasspathJep247Jdk12(new File(convertPathSeparators(jdkHome)), release, accessRuleSet) :
new ClasspathJep247(new File(convertPathSeparators(jdkHome)), release, accessRuleSet);
}
public static Classpath getClasspath(String classpathName, String encoding,
boolean isSourceOnly, AccessRuleSet accessRuleSet,
String destinationPath, Map<String, String> options, String release) {
Classpath result = null;
File file = new File(convertPathSeparators(classpathName));
if (file.isDirectory()) {
if (file.exists()) {
result = new ClasspathDirectory(file, encoding,
isSourceOnly ? ClasspathLocation.SOURCE :
ClasspathLocation.SOURCE | ClasspathLocation.BINARY,
accessRuleSet,
destinationPath == null || destinationPath == Main.NONE ?
destinationPath :
convertPathSeparators(destinationPath), options);
}
} else {
int format = Util.archiveFormat(classpathName);
if (format == Util.ZIP_FILE) {
if (isSourceOnly) {
result = new ClasspathSourceJar(file, true, accessRuleSet,
encoding,
destinationPath == null || destinationPath == Main.NONE ?
destinationPath :
convertPathSeparators(destinationPath));
} else if (destinationPath == null) {
if (classpathName.endsWith(JRTUtil.JRT_FS_JAR)) {
if (JRT_CLASSPATH_CACHE == null) {
JRT_CLASSPATH_CACHE = new HashMap<>();
} else {
result = JRT_CLASSPATH_CACHE.get(file);
}
if (result == null) {
result = new ClasspathJrt(file, true, accessRuleSet, null);
try {
result.initialize();
} catch (IOException e) {
}
JRT_CLASSPATH_CACHE.put(file, result);
}
} else {
result =
(release == null) ?
new ClasspathJar(file, true, accessRuleSet, null) :
new ClasspathMultiReleaseJar(file, true, accessRuleSet, destinationPath, release);
}
}
} else if (format == Util.JMOD_FILE) {
return new ClasspathJmod(file, true, accessRuleSet, null);
}
}
return result;
}
private void initializeKnownFileNames(String[] initialFileNames) {
if (initialFileNames == null) {
this.knownFileNames = new HashSet<>(0);
return;
}
this.knownFileNames = new HashSet<>(initialFileNames.length * 2);
for (int i = initialFileNames.length; --i >= 0;) {
File compilationUnitFile = new File(initialFileNames[i]);
char[] fileName = null;
try {
fileName = compilationUnitFile.getCanonicalPath().toCharArray();
} catch (IOException e) {
continue;
}
char[] matchingPathName = null;
final int lastIndexOf = CharOperation.lastIndexOf('.', fileName);
if (lastIndexOf != -1) {
fileName = CharOperation.subarray(fileName, 0, lastIndexOf);
}
CharOperation.replace(fileName, '\\', '/');
boolean globalPathMatches = false;
for (int j = 0, max = this.classpaths.length; j < max; j++) {
char[] matchCandidate = this.classpaths[j].normalizedPath();
boolean currentPathMatch = false;
if (this.classpaths[j] instanceof ClasspathDirectory
&& CharOperation.prefixEquals(matchCandidate, fileName)) {
currentPathMatch = true;
if (matchingPathName == null) {
matchingPathName = matchCandidate;
} else {
if (currentPathMatch) {
if (matchCandidate.length > matchingPathName.length) {
matchingPathName = matchCandidate;
}
} else {
if (!globalPathMatches && matchCandidate.length < matchingPathName.length) {
matchingPathName = matchCandidate;
}
}
}
if (currentPathMatch) {
globalPathMatches = true;
}
}
}
if (matchingPathName == null) {
this.knownFileNames.add(new String(fileName));
} else {
this.knownFileNames.add(new String(CharOperation.subarray(fileName, matchingPathName.length, fileName.length)));
}
matchingPathName = null;
}
}
public void scanForModules(Parser parser) {
for (int i = 0, max = this.classpaths.length; i < max; i++) {
File file = new File(this.classpaths[i].getPath());
IModule iModule = ModuleFinder.scanForModule(this.classpaths[i], file, parser, false, null);
if (iModule != null)
this.moduleLocations.put(String.valueOf(iModule.name()), this.classpaths[i]);
}
}
@Override
public void cleanup() {
for (int i = 0, max = this.classpaths.length; i < max; i++)
this.classpaths[i].reset();
}
private static String convertPathSeparators(String path) {
return File.separatorChar == '/'
? path.replace('\\', '/')
: path.replace('/', '\\');
}
private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[] typeName, boolean asBinaryOnly, char[] moduleName) {
NameEnvironmentAnswer answer = internalFindClass(qualifiedTypeName, typeName, asBinaryOnly, moduleName);
if (this.annotationsFromClasspath && answer != null && answer.getBinaryType() instanceof ClassFileReader) {
for (int i = 0, length = this.classpaths.length; i < length; i++) {
Classpath classpathEntry = this.classpaths[i];
if (classpathEntry.hasAnnotationFileFor(qualifiedTypeName)) {
ZipFile zip = classpathEntry instanceof ClasspathJar ? ((ClasspathJar) classpathEntry).zipFile : null;
boolean shouldClose = false;
try {
if (zip == null) {
zip = ExternalAnnotationDecorator.getAnnotationZipFile(classpathEntry.getPath(), null);
shouldClose = true;
}
answer.setBinaryType(ExternalAnnotationDecorator.create(answer.getBinaryType(), classpathEntry.getPath(),
qualifiedTypeName, zip));
return answer;
} catch (IOException e) {
} finally {
if (shouldClose && zip != null)
try {
zip.close();
} catch (IOException e) { }
}
}
}
answer.setBinaryType(new ExternalAnnotationDecorator(answer.getBinaryType(), null));
}
return answer;
}
private NameEnvironmentAnswer internalFindClass(String qualifiedTypeName, char[] typeName, boolean asBinaryOnly, char[] moduleName) {
if (this.knownFileNames.contains(qualifiedTypeName)) return null;
String qualifiedBinaryFileName = qualifiedTypeName + SUFFIX_STRING_class;
String qualifiedPackageName =
qualifiedTypeName.length() == typeName.length
? Util.EMPTY_STRING
: qualifiedBinaryFileName.substring(0, qualifiedTypeName.length() - typeName.length - 1);
LookupStrategy strategy = LookupStrategy.get(moduleName);
if (strategy == LookupStrategy.Named) {
if (this.moduleLocations != null) {
String moduleNameString = String.valueOf(moduleName);
Classpath classpath = this.moduleLocations.get(moduleNameString);
if (classpath != null) {
return classpath.findClass(typeName, qualifiedPackageName, moduleNameString, qualifiedBinaryFileName);
}
}
return null;
}
String qp2 = File.separatorChar == '/' ? qualifiedPackageName : qualifiedPackageName.replace('/', File.separatorChar);
NameEnvironmentAnswer suggestedAnswer = null;
if (qualifiedPackageName == qp2) {
for (int i = 0, length = this.classpaths.length; i < length; i++) {
if (!strategy.matches(this.classpaths[i], Classpath::hasModule))
continue;
NameEnvironmentAnswer answer = this.classpaths[i].findClass(typeName, qualifiedPackageName, null, qualifiedBinaryFileName, asBinaryOnly);
if (answer != null) {
if (answer.moduleName() != null && !this.moduleLocations.containsKey(String.valueOf(answer.moduleName())))
continue;
if (!answer.ignoreIfBetter()) {
if (answer.isBetter(suggestedAnswer))
return answer;
} else if (answer.isBetter(suggestedAnswer))
suggestedAnswer = answer;
}
}
} else {
String qb2 = qualifiedBinaryFileName.replace('/', File.separatorChar);
for (int i = 0, length = this.classpaths.length; i < length; i++) {
Classpath p = this.classpaths[i];
if (!strategy.matches(p, Classpath::hasModule))
continue;
NameEnvironmentAnswer answer = !(p instanceof ClasspathDirectory)
? p.findClass(typeName, qualifiedPackageName, null, qualifiedBinaryFileName, asBinaryOnly)
: p.findClass(typeName, qp2, null, qb2, asBinaryOnly);
if (answer != null) {
if (answer.moduleName() != null && !this.moduleLocations.containsKey(String.valueOf(answer.moduleName())))
continue;
if (!answer.ignoreIfBetter()) {
if (answer.isBetter(suggestedAnswer))
return answer;
} else if (answer.isBetter(suggestedAnswer))
suggestedAnswer = answer;
}
}
}
return suggestedAnswer;
}
@Override
public NameEnvironmentAnswer findType(char[][] compoundName, char[] moduleName) {
if (compoundName != null)
return findClass(
new String(CharOperation.concatWith(compoundName, '/')),
compoundName[compoundName.length - 1],
false,
moduleName);
return null;
}
public char[][][] findTypeNames(char[][] packageName) {
char[][][] result = null;
if (packageName != null) {
String qualifiedPackageName = new String(CharOperation.concatWith(packageName, '/'));
String qualifiedPackageName2 = File.separatorChar == '/' ? qualifiedPackageName : qualifiedPackageName.replace('/', File.separatorChar);
if (qualifiedPackageName == qualifiedPackageName2) {
for (int i = 0, length = this.classpaths.length; i < length; i++) {
char[][][] answers = this.classpaths[i].findTypeNames(qualifiedPackageName, null);
if (answers != null) {
if (result == null) {
result = answers;
} else {
int resultLength = result.length;
int answersLength = answers.length;
System.arraycopy(result, 0, (result = new char[answersLength + resultLength][][]), 0, resultLength);
System.arraycopy(answers, 0, result, resultLength, answersLength);
}
}
}
} else {
for (int i = 0, length = this.classpaths.length; i < length; i++) {
Classpath p = this.classpaths[i];
char[][][] answers = !(p instanceof ClasspathDirectory) ? p.findTypeNames(qualifiedPackageName, null)
: p.findTypeNames(qualifiedPackageName2, null);
if (answers != null) {
if (result == null) {
result = answers;
} else {
int resultLength = result.length;
int answersLength = answers.length;
System.arraycopy(result, 0, (result = new char[answersLength + resultLength][][]), 0,
resultLength);
System.arraycopy(answers, 0, result, resultLength, answersLength);
}
}
}
}
}
return result;
}
@Override
public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName, char[] moduleName) {
if (typeName != null)
return findClass(
new String(CharOperation.concatWith(packageName, typeName, '/')),
typeName,
false,
moduleName);
return null;
}
@Override
public char[][] getModulesDeclaringPackage(char[][] packageName, char[] moduleName) {
String qualifiedPackageName = new String(CharOperation.concatWith(packageName, '/'));
String moduleNameString = String.valueOf(moduleName);
LookupStrategy strategy = LookupStrategy.get(moduleName);
if (strategy == LookupStrategy.Named) {
if (this.moduleLocations != null) {
Classpath classpath = this.moduleLocations.get(moduleNameString);
if (classpath != null) {
if (classpath.isPackage(qualifiedPackageName, moduleNameString))
return new char[][] {moduleName};
}
}
return null;
}
char[][] allNames = null;
boolean hasUnobserable = false;
for (Classpath cp : this.classpaths) {
if (strategy.matches(cp, Classpath::hasModule)) {
if (strategy == LookupStrategy.Unnamed) {
if (cp.isPackage(qualifiedPackageName, moduleNameString))
return new char[][] { ModuleBinding.UNNAMED };
} else {
char[][] declaringModules = cp.getModulesDeclaringPackage(qualifiedPackageName, null);
if (declaringModules != null) {
if (cp instanceof ClasspathJrt && this.hasLimitModules) {
declaringModules = filterModules(declaringModules);
hasUnobserable |= declaringModules == null;
}
if (allNames == null)
allNames = declaringModules;
else
allNames = CharOperation.arrayConcat(allNames, declaringModules);
}
}
}
}
if (allNames == null && hasUnobserable)
return new char[][] { ModuleBinding.UNOBSERVABLE };
return allNames;
}
private char[][] filterModules(char[][] declaringModules) {
char[][] filtered = Arrays.stream(declaringModules).filter(m -> this.moduleLocations.containsKey(new String(m))).toArray(char[][]::new);
if (filtered.length == 0)
return null;
return filtered;
}
private Parser getParser() {
Map<String,String> opts = new HashMap<String, String>();
opts.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_9);
return new Parser(
new ProblemReporter(DefaultErrorHandlingPolicies.exitOnFirstError(), new CompilerOptions(opts), new DefaultProblemFactory(Locale.getDefault())),
false);
}
@Override
public boolean hasCompilationUnit(char[][] qualifiedPackageName, char[] moduleName, boolean checkCUs) {
String qPackageName = String.valueOf(CharOperation.concatWith(qualifiedPackageName, '/'));
String moduleNameString = String.valueOf(moduleName);
LookupStrategy strategy = LookupStrategy.get(moduleName);
Parser parser = checkCUs ? getParser() : null;
Function<CompilationUnit, String> pkgNameExtractor = (sourceUnit) -> {
String pkgName = null;
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 1);
char[][] name = parser.parsePackageDeclaration(sourceUnit.getContents(), compilationResult);
if (name != null) {
pkgName = CharOperation.toString(name);
}
return pkgName;
};
switch (strategy) {
case Named:
if (this.moduleLocations != null) {
Classpath location = this.moduleLocations.get(moduleNameString);
if (location != null)
return checkCUs ? location.hasCUDeclaringPackage(qPackageName, pkgNameExtractor)
: location.hasCompilationUnit(qPackageName, moduleNameString);
}
return false;
default:
for (int i = 0; i < this.classpaths.length; i++) {
Classpath location = this.classpaths[i];
if (strategy.matches(location, Classpath::hasModule))
if (location.hasCompilationUnit(qPackageName, moduleNameString))
return true;
}
return false;
}
}
@Override
public IModule getModule(char[] name) {
if (this.module != null && CharOperation.equals(name, this.module.name())) {
return this.module;
}
if (this.moduleLocations.containsKey(new String(name))) {
for (Classpath classpath : this.classpaths) {
IModule mod = classpath.getModule(name);
if (mod != null) {
return mod;
}
}
}
return null;
}
public IModule getModuleFromEnvironment(char[] name) {
if (this.module != null && CharOperation.equals(name, this.module.name())) {
return this.module;
}
for (Classpath classpath : this.classpaths) {
IModule mod = classpath.getModule(name);
if (mod != null) {
return mod;
}
}
return null;
}
@Override
public char[][] getAllAutomaticModules() {
Set<char[]> set = new HashSet<>();
for (int i = 0, l = this.classpaths.length; i < l; i++) {
if (this.classpaths[i].isAutomaticModule()) {
set.add(this.classpaths[i].getModule().name());
}
}
return set.toArray(new char[set.size()][]);
}
@Override
public char[][] listPackages(char[] moduleName) {
switch (LookupStrategy.get(moduleName)) {
case Named:
Classpath classpath = this.moduleLocations.get(new String(moduleName));
if (classpath != null)
return classpath.listPackages();
return CharOperation.NO_CHAR_CHAR;
default:
throw new UnsupportedOperationException("can list packages only of a named module");
}
}
void addModuleUpdate(String moduleName, Consumer<IUpdatableModule> update, UpdateKind kind) {
UpdatesByKind updates = this.moduleUpdates.get(moduleName);
if (updates == null) {
this.moduleUpdates.put(moduleName, updates = new UpdatesByKind());
}
updates.getList(kind, true).add(update);
}
@Override
public void applyModuleUpdates(IUpdatableModule compilerModule, IUpdatableModule.UpdateKind kind) {
char[] name = compilerModule.name();
if (name != ModuleBinding.UNNAMED) {
UpdatesByKind updates = this.moduleUpdates.get(String.valueOf(name));
if (updates != null) {
for (Consumer<IUpdatableModule> update : updates.getList(kind, false))
update.accept(compilerModule);
}
}
}
}