Skip to content

Commit

Permalink
Provide a way for extensions to discover used classpath entries
Browse files Browse the repository at this point in the history
(Patch for eclipse-jdt#2529)
  • Loading branch information
guw committed Jun 6, 2024
1 parent c6b3f6d commit f26afc1
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.batch;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
Expand All @@ -27,7 +43,6 @@
import org.eclipse.jdt.internal.compiler.classfmt.ExternalAnnotationProvider;
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.parser.Parser;
Expand All @@ -36,22 +51,6 @@
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.compiler.util.Util;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

@SuppressWarnings({"rawtypes", "unchecked"})
public class ClasspathDirectory extends ClasspathLocation {

Expand Down Expand Up @@ -125,7 +124,7 @@ boolean doesFileExist(String fileName, String qualifiedPackageName) {
public List fetchLinkedJars(FileSystem.ClasspathSectionProblemReporter problemReporter) {
return null;
}
private NameEnvironmentAnswer findClassInternal(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
private NameEnvironmentAnswerWithClasspathOrigin findClassInternal(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
if (!isPackage(qualifiedPackageName, null)) return null; // most common case TODO(SHMOD): use module name from this.module?
String fileName = new String(typeName);
boolean binaryExists = ((this.mode & BINARY) != 0) && doesFileExist(fileName + SUFFIX_STRING_class, qualifiedPackageName);
Expand All @@ -135,14 +134,14 @@ private NameEnvironmentAnswer findClassInternal(char[] typeName, String qualifie
CompilationUnit unit = new CompilationUnit(null, fullSourcePath, this.encoding, this.destinationPath);
unit.module = this.module == null ? null : this.module.name();
if (!binaryExists)
return new NameEnvironmentAnswer(unit,
fetchAccessRestriction(qualifiedBinaryFileName));
return new NameEnvironmentAnswerWithClasspathOrigin(unit,
fetchAccessRestriction(qualifiedBinaryFileName), this);
String fullBinaryPath = this.path + qualifiedBinaryFileName;
long binaryModified = new File(fullBinaryPath).lastModified();
long sourceModified = new File(fullSourcePath).lastModified();
if (sourceModified > binaryModified)
return new NameEnvironmentAnswer(unit,
fetchAccessRestriction(qualifiedBinaryFileName));
return new NameEnvironmentAnswerWithClasspathOrigin(unit,
fetchAccessRestriction(qualifiedBinaryFileName), this);
}
if (binaryExists) {
try {
Expand All @@ -156,18 +155,19 @@ private NameEnvironmentAnswer findClassInternal(char[] typeName, String qualifie
}
if (reader != null) {
char[] modName = reader.moduleName != null ? reader.moduleName : this.module != null ? this.module.name() : null;
return new NameEnvironmentAnswer(
return new NameEnvironmentAnswerWithClasspathOrigin(
reader,
fetchAccessRestriction(qualifiedBinaryFileName),
modName);
modName,
this);
}
} catch (IOException | ClassFormatException e) {
// treat as if file is missing
}
}
return null;
}
public NameEnvironmentAnswer findSecondaryInClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName) {
public NameEnvironmentAnswerWithClasspathOrigin findSecondaryInClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName) {
//"package-info" is a reserved class name and can never be a secondary type (it is much faster to stop the search here).
if(CharOperation.equals(TypeConstants.PACKAGE_INFO_NAME, typeName)) {
return null;
Expand All @@ -189,11 +189,11 @@ public boolean hasAnnotationFileFor(String qualifiedTypeName) {
return false;
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
return findClass(typeName, qualifiedPackageName, moduleName, qualifiedBinaryFileName, false);
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
if (File.separatorChar == '/')
return findClassInternal(typeName, qualifiedPackageName, qualifiedBinaryFileName, asBinaryOnly);

Expand Down Expand Up @@ -241,7 +241,7 @@ private Hashtable<String, String> getSecondaryTypes(String qualifiedPackageName)
}
return packageEntry;
}
private NameEnvironmentAnswer findSourceSecondaryType(String typeName, String qualifiedPackageName, String qualifiedBinaryFileName) {
private NameEnvironmentAnswerWithClasspathOrigin findSourceSecondaryType(String typeName, String qualifiedPackageName, String qualifiedBinaryFileName) {

if (this.packageSecondaryTypes == null) this.packageSecondaryTypes = new Hashtable<>();
Hashtable<String, String> packageEntry = this.packageSecondaryTypes.get(qualifiedPackageName);
Expand All @@ -250,9 +250,9 @@ private NameEnvironmentAnswer findSourceSecondaryType(String typeName, String qu
this.packageSecondaryTypes.put(qualifiedPackageName, packageEntry);
}
String fileName = packageEntry.get(typeName);
return fileName != null ? new NameEnvironmentAnswer(new CompilationUnit(null,
return fileName != null ? new NameEnvironmentAnswerWithClasspathOrigin(new CompilationUnit(null,
fileName, this.encoding, this.destinationPath),
fetchAccessRestriction(qualifiedBinaryFileName)) : null;
fetchAccessRestriction(qualifiedBinaryFileName), this) : null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
import org.eclipse.jdt.internal.compiler.classfmt.ExternalAnnotationDecorator;
import org.eclipse.jdt.internal.compiler.classfmt.ExternalAnnotationProvider;
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.ExternalAnnotationStatus;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
import org.eclipse.jdt.internal.compiler.util.Util;
Expand Down Expand Up @@ -104,11 +103,11 @@ public List<Classpath> fetchLinkedJars(FileSystem.ClasspathSectionProblemReporte
}
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
return findClass(typeName, qualifiedPackageName, moduleName, qualifiedBinaryFileName, false);
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
if (!isPackage(qualifiedPackageName, moduleName))
return null; // most common case

Expand Down Expand Up @@ -143,7 +142,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
// location is configured for external annotations, but no .eea found, decorate in order to answer NO_EEA_FILE:
reader = new ExternalAnnotationDecorator(reader, null);
}
return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName);
return new NameEnvironmentAnswerWithClasspathOrigin(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName, this);
}
} catch (ClassFormatException | IOException e) {
// treat as if class file is missing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.util.CtSym;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
Expand Down Expand Up @@ -64,11 +63,11 @@ public List<Classpath> fetchLinkedJars(FileSystem.ClasspathSectionProblemReporte
return null;
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
return findClass(typeName, qualifiedPackageName, moduleName, qualifiedBinaryFileName, false);
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
if (!isPackage(qualifiedPackageName, moduleName))
return null; // most common case

Expand All @@ -95,7 +94,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
reader = new ClassFileReader(p.toUri(), content, qualifiedBinaryFileName.toCharArray());
reader = maybeDecorateForExternalAnnotations(qualifiedBinaryFileName, reader);
char[] modName = moduleName != null ? moduleName.toCharArray() : null;
return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName);
return new NameEnvironmentAnswerWithClasspathOrigin(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName, this);
}
} catch (ClassFormatException | IOException e) {
// continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.util.CtSym;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
import org.eclipse.jdt.internal.compiler.util.Util;
Expand All @@ -54,11 +53,11 @@ public List<Classpath> fetchLinkedJars(FileSystem.ClasspathSectionProblemReporte
return null;
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
return findClass(typeName, qualifiedPackageName, moduleName, qualifiedBinaryFileName, false);
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
if (!isPackage(qualifiedPackageName, moduleName))
return null; // most common case

Expand Down Expand Up @@ -100,7 +99,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
reader = new ClassFileReader(p.toUri(), content, qualifiedBinaryFileName.toCharArray());
reader = maybeDecorateForExternalAnnotations(qualifiedBinaryFileName, reader);
char[] modName = moduleName != null ? moduleName.toCharArray() : foundModName;
return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName);
return new NameEnvironmentAnswerWithClasspathOrigin(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName, this);
}
} catch (ClassFormatException | IOException e) {
// continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
import org.eclipse.jdt.internal.compiler.classfmt.ExternalAnnotationDecorator;
import org.eclipse.jdt.internal.compiler.classfmt.ExternalAnnotationProvider;
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.ExternalAnnotationStatus;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
import org.eclipse.jdt.internal.compiler.util.Util;
Expand All @@ -51,7 +50,7 @@ public List<Classpath> fetchLinkedJars(FileSystem.ClasspathSectionProblemReporte
return null;
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
if (!isPackage(qualifiedPackageName, moduleName))
return null; // most common case

Expand Down Expand Up @@ -87,7 +86,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
// location is configured for external annotations, but no .eea found, decorate in order to answer NO_EEA_FILE:
reader = new ExternalAnnotationDecorator(reader, null);
}
return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName);
return new NameEnvironmentAnswerWithClasspathOrigin(reader, fetchAccessRestriction(qualifiedBinaryFileName), modName, this);
}
} catch (ClassFormatException | IOException e) {
// treat as if class file is missing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.IModule.IPackageExport;
import org.eclipse.jdt.internal.compiler.env.IMultiModuleEntry;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.ExternalAnnotationStatus;
import org.eclipse.jdt.internal.compiler.util.CtSym;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
Expand Down Expand Up @@ -76,11 +75,11 @@ public boolean hasCompilationUnit(String qualifiedPackageName, String moduleName
return JRTUtil.hasCompilationUnit(this.file, qualifiedPackageName, moduleName);
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
return findClass(typeName, qualifiedPackageName, moduleName, qualifiedBinaryFileName, false);
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly) {
if (!isPackage(qualifiedPackageName, moduleName))
return null; // most common case

Expand All @@ -92,7 +91,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
char[] answerModuleName = reader.getModule();
if (answerModuleName == null && moduleName != null)
answerModuleName = moduleName.toCharArray();
return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName), answerModuleName);
return new NameEnvironmentAnswerWithClasspathOrigin(reader, fetchAccessRestriction(qualifiedBinaryFileName), answerModuleName, this);
}
} catch (ClassFormatException | IOException e) {
// treat as if class file is missing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class ClasspathJsr199 extends ClasspathLocation {
Expand Down Expand Up @@ -77,7 +76,7 @@ public List fetchLinkedJars(FileSystem.ClasspathSectionProblemReporter problemRe
}

@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName,
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName, String moduleName,
String aQualifiedBinaryFileName, boolean asBinaryOnly) {
if (this.jrt != null) {
return this.jrt.findClass(typeName, qualifiedPackageName, moduleName, aQualifiedBinaryFileName, asBinaryOnly);
Expand All @@ -102,7 +101,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
try (InputStream inputStream = jfo.openInputStream()) {
ClassFileReader reader = ClassFileReader.read(inputStream, qualifiedBinaryFileName);
if (reader != null) {
return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName));
return new NameEnvironmentAnswerWithClasspathOrigin(reader, fetchAccessRestriction(qualifiedBinaryFileName), this);
}
}
} catch (ClassFormatException e) {
Expand Down Expand Up @@ -272,7 +271,7 @@ public IModule getModule(char[] name) {
}

@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName,
public NameEnvironmentAnswerWithClasspathOrigin findClass(char[] typeName, String qualifiedPackageName,
String moduleName, String qualifiedBinaryFileName) {
//
return findClass(typeName, qualifiedPackageName, moduleName, qualifiedBinaryFileName, false);
Expand Down
Loading

0 comments on commit f26afc1

Please sign in to comment.