Skip to content

Commit

Permalink
[BUG] [JSR-199] ECJ cannot resolve JPMS modules if using a user-provided
Browse files Browse the repository at this point in the history
file manager

resolve TODOs:
+ exception handling: ignore, or throw AbortCompilation, perhaps log
+ encoding: pass through from Main.compilerOptions

Fixes eclipse-jdt#958
  • Loading branch information
stephan-herrmann committed Dec 27, 2024
1 parent de490a0 commit 6de6eeb
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,29 @@ public class BatchFilerImpl implements Filer {
protected final JavaFileManager _fileManager;
protected final HashSet<URI> _createdFiles;
protected String _moduleName;
protected String _encoding;

public BatchFilerImpl(BaseAnnotationProcessorManager dispatchManager, BatchProcessingEnvImpl env, Main main)
{
this._dispatchManager = dispatchManager;
this._fileManager = env._fileManager;
this._env = env;
this._createdFiles = new HashSet<>();
this._encoding = main.getDefaultEncoding();
if (this._fileManager.hasLocation(StandardLocation.SOURCE_PATH)) {
try {
for (JavaFileObject javaFileObject : this._fileManager.list(StandardLocation.SOURCE_PATH, "", //$NON-NLS-1$
Collections.singleton(Kind.SOURCE), false)) {
if (javaFileObject.getName().equals(IModule.MODULE_INFO_JAVA)) {
IModule module = ClasspathJsr199.extractModuleFromFileObject(javaFileObject, main::getNewParser, null);
IModule module = ClasspathJsr199.extractModuleFromFileObject(javaFileObject, main::getNewParser, null, this._encoding);
if (module != null)
this._moduleName = String.valueOf(module.name());
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
main.logger.logException(e);
}
}
}
Expand All @@ -96,7 +98,7 @@ public JavaFileObject createClassFile(CharSequence name,
}

this._createdFiles.add(uri);
return new HookedJavaFileObject(jfo, jfo.getName(), name.toString(), this, this._moduleName);
return new HookedJavaFileObject(jfo, jfo.getName(), name.toString(), this, this._moduleName, this._encoding);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -183,7 +185,7 @@ public JavaFileObject createSourceFile(CharSequence name,

this._createdFiles.add(uri);
// hook the file object's writers to create compilation unit and add to addedUnits()
return new HookedJavaFileObject(jfo, jfo.getName(), name.toString(), this, mod);
return new HookedJavaFileObject(jfo, jfo.getName(), name.toString(), this, mod, this._encoding);
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit;

/**
* A delegating JavaFileObject that hooks the close() methods of the Writer
Expand Down Expand Up @@ -202,12 +203,15 @@ public String toString() {

private final String _moduleName;

public HookedJavaFileObject(JavaFileObject fileObject, String fileName, String typeName, BatchFilerImpl filer, String module) {
private final String _encoding;

public HookedJavaFileObject(JavaFileObject fileObject, String fileName, String typeName, BatchFilerImpl filer, String module, String encoding) {
super(fileObject);
this._filer = filer;
this._fileName = fileName;
this._typeName = typeName;
this._moduleName = module;
this._encoding = encoding;
}

@SuppressWarnings("resource") // ForwardingOutputStream forwards close() too
Expand All @@ -225,21 +229,20 @@ public Writer openWriter() throws IOException {
protected void closed() {
if (!this._closed) {
this._closed = true;
//TODO: support encoding
switch(this.getKind()) {
case SOURCE :
try {
CompilationUnit unit = new CompilationUnit(
getCharContent(false).toString().toCharArray(),
this._fileName,
null /* encoding */,
this._encoding,
null /* destination path */,
this._filer._env.shouldIgnoreOptionalProblems(this._fileName.toCharArray()),
this._moduleName);
this._filer.addNewUnit(unit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new AbortCompilationUnit(null, e, this._encoding);
}
break;
case CLASS :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ClasspathJsr199 extends ClasspathLocation {

public static final String ENCODING_UTF_8 = "UTF-8"; //$NON-NLS-1$
private final static String NO_PATH = ""; //$NON-NLS-1$

private final Set<JavaFileObject.Kind> fileTypes;
private final JavaFileManager fileManager;
private final JavaFileManager.Location location;
private Classpath jrt;
private Supplier<Parser> parserSupplier;
private String encoding;

/**
* FileSystem.internalFindClass() detects request for initial files by filename,
Expand Down Expand Up @@ -128,7 +128,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
} else {
if (this.initialJavaFileObjects != null && this.initialJavaFileObjects.contains(jfo))
return null; // refuse to re-add an initial file (possibly via a wrong module?)
CompilationUnit cu = readCompilationUnit(jfo);
CompilationUnit cu = readCompilationUnit(jfo, this.encoding);
cu.module = answerModule;
return new NameEnvironmentAnswer(cu, fetchAccessRestriction(qualifiedBinaryFileName), answerModule);
}
Expand Down Expand Up @@ -196,14 +196,14 @@ public void initialize() throws IOException {
if (this.location == StandardLocation.SOURCE_PATH) {
for (JavaFileObject javaFileObject : this.fileManager.list(this.location, NO_PATH, Collections.singleton(JavaFileObject.Kind.SOURCE), false)) {
if (javaFileObject.getName().equals(IModule.MODULE_INFO_JAVA)) {
this.module = ClasspathJsr199.extractModuleFromFileObject(javaFileObject, this.parserSupplier, this);
this.module = ClasspathJsr199.extractModuleFromFileObject(javaFileObject, this.parserSupplier, this, this.encoding);
return;
}
}
} else {
for (JavaFileObject javaFileObject : this.fileManager.list(this.location, NO_PATH, Collections.singleton(JavaFileObject.Kind.CLASS), false)) {
if (javaFileObject.getName().equals(IModule.MODULE_INFO_CLASS)) {
this.module = ClasspathJsr199.extractModuleFromFileObject(javaFileObject, null, this);
this.module = ClasspathJsr199.extractModuleFromFileObject(javaFileObject, null, this, this.encoding);
return;
}
}
Expand Down Expand Up @@ -267,7 +267,7 @@ public char[][] listPackages() {
}
return result;
} catch (IOException e) {
// ??
// treat as if empty
}
return CharOperation.NO_CHAR_CHAR;
}
Expand Down Expand Up @@ -367,15 +367,15 @@ public static ClassFileReader readJavaClass(JavaFileObject jfo, String name) thr
}
}

public static CompilationUnit readCompilationUnit(JavaFileObject jfo) throws IOException {
return new CompilationUnit(jfo.getCharContent(false).toString().toCharArray(), jfo.getName(), ENCODING_UTF_8);
public static CompilationUnit readCompilationUnit(JavaFileObject jfo, String encoding) throws IOException {
return new CompilationUnit(jfo.getCharContent(false).toString().toCharArray(), jfo.getName(), encoding);
}

public static IModule extractModuleFromFileObject(JavaFileObject javaFileObject, Supplier<Parser> parserSupplier, Classpath pathEntry) {
public static IModule extractModuleFromFileObject(JavaFileObject javaFileObject, Supplier<Parser> parserSupplier, Classpath pathEntry, String encoding) {
try {
switch (javaFileObject.getKind()) {
case SOURCE:
return extractModuleFromSource(javaFileObject, parserSupplier.get(), pathEntry);
return extractModuleFromSource(javaFileObject, parserSupplier.get(), pathEntry, encoding);
case CLASS:
return extractModuleFromClass(javaFileObject, pathEntry);
default:
Expand All @@ -394,8 +394,8 @@ public static IModule extractModuleFromFileObject(JavaFileObject javaFileObject,
}
return null;
}
static IModule extractModuleFromSource(JavaFileObject javaFileObject, Parser parser, Classpath pathEntry) throws IOException {
CompilationUnit cu = readCompilationUnit(javaFileObject);
static IModule extractModuleFromSource(JavaFileObject javaFileObject, Parser parser, Classpath pathEntry, String encoding) throws IOException {
CompilationUnit cu = readCompilationUnit(javaFileObject, encoding);
CompilationResult compilationResult = new CompilationResult(cu, 0, 1, 10);
CompilationUnitDeclaration unit = parser.parse(cu, compilationResult);
if (unit.isModuleInfo() && unit.moduleDeclaration != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,16 @@ private void handleSingleModuleCompilation() {
protected CompilationUnit createCompilationUnit(int idx, String filename) {
return new CompilationUnit(null, filename, null);
}
/*
* External API
*/
public String getDefaultEncoding() {
if (this.compilerOptions != null)
return this.compilerOptions.defaultEncoding;
if (this.options != null)
return new CompilerOptions(this.options).defaultEncoding;
return null;
}
/*
* External API
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,19 @@ public CompilationUnit[] getCompilationUnits() {
throw new IllegalArgumentException(this.bind("unit.missing", name)); //$NON-NLS-1$
}

String encoding = getDefaultEncoding();
try {
CompilationUnit cu = new CompilationUnit(javaFileObject.getCharContent(false).toString().toCharArray(),
name,
null,
encoding,
this.destinationPaths[i],
shouldIgnoreOptionalProblems(this.ignoreOptionalProblemsFromFolders, name.toCharArray()),
this.modNames[i]);
units.add(cu);
this.javaFileObjectMap.put(cu, javaFileObject);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new AbortCompilationUnit(null, e, encoding);
}
}
i++;
Expand Down Expand Up @@ -989,7 +990,7 @@ protected CompilationUnit createCompilationUnit(int idx, String filename) {
JavaFileObject javaFileObject = this.compilationUnits.get(idx);
if (filename.endsWith(javaFileObject.getName())) {
try {
return ClasspathJsr199.readCompilationUnit(javaFileObject);
return ClasspathJsr199.readCompilationUnit(javaFileObject, getDefaultEncoding());
} catch (IOException e) {
// nop
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ private <P> void initModules(Location location, Iterable<? extends P> paths, Fun
}
}
} catch (IllegalArgumentException iae) { // e.g., from ModuleFinder.scanForModule(Classpath, File, Parser, boolean, String)
// FIXME ignore for now
// ignore
}
}
}
Expand Down

0 comments on commit 6de6eeb

Please sign in to comment.