Skip to content

Commit

Permalink
Let 'GradleStandardJavaFileManager' implement 'StandardJavaFileManager'
Browse files Browse the repository at this point in the history
...by forwarding all methods to the wrapped file manager.
This gives it more robustness when running with custom compiler and
file manager implementation. In particular, it avoids hitting the
following issue with the Eclipse compiler:
eclipse-jdt/eclipse.jdt.core#985

Signed-off-by: Jendrik Johannes <jendrik.johannes@gmail.com>
  • Loading branch information
jjohannes committed Apr 3, 2024
1 parent 2746346 commit 798d88f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@

import org.gradle.internal.classpath.ClassPath;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import java.io.File;
import java.io.IOException;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Set;

import static org.gradle.api.internal.tasks.compile.filter.AnnotationProcessorFilter.getFilteredClassLoader;

public class GradleStandardJavaFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
public class GradleStandardJavaFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> implements StandardJavaFileManager {
private final ClassPath annotationProcessorPath;
private final boolean hasEmptySourcePaths;

Expand Down Expand Up @@ -92,4 +96,69 @@ public ClassLoader getClassLoader(Location location) {

return classLoader;
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
return fileManager.getJavaFileObjectsFromFiles(files);
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
return fileManager.getJavaFileObjects(files);
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
return fileManager.getJavaFileObjectsFromStrings(names);
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
return fileManager.getJavaFileObjects(names);
}

@Override
public void setLocation(Location location, Iterable<? extends File> files) throws IOException {
fileManager.setLocation(location, files);
}

@Override
public Iterable<? extends File> getLocation(Location location) {
return fileManager.getLocation(location);
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(Iterable<? extends Path> paths) {
return fileManager.getJavaFileObjectsFromPaths(paths);
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(Path... paths) {
return fileManager.getJavaFileObjects(paths);
}

@Override
public void setLocationFromPaths(Location location, Collection<? extends Path> paths) throws IOException {
fileManager.setLocationFromPaths(location, paths);
}

@Override
public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths) throws IOException {
fileManager.setLocationForModule(location, moduleName, paths);
}

@Override
public Iterable<? extends Path> getLocationAsPaths(Location location) {
return fileManager.getLocationAsPaths(location);
}

@Override
public Path asPath(FileObject file) {
return fileManager.asPath(file);
}

@Override
public void setPathFactory(PathFactory f) {
fileManager.setPathFactory(f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class EclipseCompilerIntegrationTest extends AbstractIntegrationSpec {
plugins {
id 'java-library'
}
group = 'org'
version = '1.0-beta2'
${mavenCentralRepository()}
Expand Down Expand Up @@ -89,4 +87,35 @@ class EclipseCompilerIntegrationTest extends AbstractIntegrationSpec {
then:
result.assertTaskExecuted(':compileJava')
}

def "custom file manager does not cause issue with eclipse compiler"() {
given:
settingsFile << "include('other-project')"
file('other-project/build.gradle') << "plugins { id 'java-library' }"
file("other-project/src/main/java/foo/Bar.java") << """
package foo;
public class Bar {}
"""

// Same package name (bar) as class on the classpath, potential issue on Windows file system
// See: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/985
file("src/main/java/foo/bar/Main.java") << """
package foo;
public class Main {}
"""
buildFile << '''
dependencies {
implementation project(':other-project')
}
tasks.withType(JavaCompile).configureEach {
customCompilerClasspath.from(configurations.ecj)
}
'''

when:
run ':compileJava'

then:
result.assertTaskExecuted(':compileJava')
}
}

0 comments on commit 798d88f

Please sign in to comment.