summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2019-01-08 20:18:49 -0800
committerGeorge Fraser <george@fivetran.com>2019-01-08 20:18:49 -0800
commita8d28f51dfc42c88eddbe1b5156f5b1be5aece83 (patch)
treea9c95f9d86332df2a375870cdaf96c58929c0889
parent07a4b8b44b492d18cd759022395eb3a593f42f75 (diff)
downloadjava-language-server-a8d28f51dfc42c88eddbe1b5156f5b1be5aece83.zip
Use ForwardingJavaFileManager
-rw-r--r--src/main/java/org/javacs/JavaCompilerService.java2
-rw-r--r--src/main/java/org/javacs/SourceFileManager.java128
-rw-r--r--src/test/java/org/javacs/SourceFileManagerTest.java4
3 files changed, 14 insertions, 120 deletions
diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java
index 8379922..1087e5f 100644
--- a/src/main/java/org/javacs/JavaCompilerService.java
+++ b/src/main/java/org/javacs/JavaCompilerService.java
@@ -24,7 +24,7 @@ public class JavaCompilerService {
final List<Diagnostic<? extends JavaFileObject>> diags = new ArrayList<>();
// Use the same file manager for multiple tasks, so we don't repeatedly re-compile the same files
// TODO intercept files that aren't in the batch and erase method bodies so compilation is faster
- final StandardJavaFileManager fileManager;
+ final SourceFileManager fileManager;
public JavaCompilerService(Set<Path> classPath, Set<Path> docPath) {
System.err.println("Class path:");
diff --git a/src/main/java/org/javacs/SourceFileManager.java b/src/main/java/org/javacs/SourceFileManager.java
index 6af01fa..64ef0b0 100644
--- a/src/main/java/org/javacs/SourceFileManager.java
+++ b/src/main/java/org/javacs/SourceFileManager.java
@@ -2,16 +2,16 @@ package org.javacs;
import java.io.File;
import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Logger;
import javax.tools.*;
-class SourceFileManager implements StandardJavaFileManager {
- private final StandardJavaFileManager delegate = createDelegateFileManager();
+class SourceFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
+ public SourceFileManager() {
+ super(createDelegateFileManager());
+ }
private static StandardJavaFileManager createDelegateFileManager() {
var compiler = ServiceLoader.load(JavaCompiler.class).iterator().next();
@@ -23,31 +23,13 @@ class SourceFileManager implements StandardJavaFileManager {
}
@Override
- public ClassLoader getClassLoader(Location location) {
- return delegate.getClassLoader(location);
- }
-
- private URL[] urls(Set<Path> paths) {
- var urls = new URL[paths.size()];
- var i = 0;
- for (var p : paths) {
- try {
- urls[i++] = p.toUri().toURL();
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- }
- return urls;
- }
-
- @Override
public Iterable<JavaFileObject> list(
Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
if (location == StandardLocation.SOURCE_PATH) {
var stream = FileStore.list(packageName).stream().map(this::asJavaFileObject).filter(this::isJavaSource);
return stream::iterator;
} else {
- return delegate.list(location, packageName, kinds, recurse);
+ return super.list(location, packageName, kinds, recurse);
}
}
@@ -68,7 +50,7 @@ class SourceFileManager implements StandardJavaFileManager {
if (!packageName.isEmpty()) className = packageName + "." + className;
return className;
} else {
- return delegate.inferBinaryName(location, file);
+ return super.inferBinaryName(location, file);
}
}
@@ -83,13 +65,8 @@ class SourceFileManager implements StandardJavaFileManager {
}
@Override
- public boolean handleOption(String current, Iterator<String> remaining) {
- return delegate.handleOption(current, remaining);
- }
-
- @Override
public boolean hasLocation(Location location) {
- return location == StandardLocation.SOURCE_PATH || delegate.hasLocation(location);
+ return location == StandardLocation.SOURCE_PATH || super.hasLocation(location);
}
@Override
@@ -103,7 +80,7 @@ class SourceFileManager implements StandardJavaFileManager {
}
return null;
}
- return delegate.getJavaFileForInput(location, className, kind);
+ return super.getJavaFileForInput(location, className, kind);
}
@Override
@@ -111,56 +88,7 @@ class SourceFileManager implements StandardJavaFileManager {
if (location == StandardLocation.SOURCE_PATH) {
return null;
}
- return delegate.getFileForInput(location, packageName, relativeName);
- }
-
- @Override
- public JavaFileObject getJavaFileForOutput(
- Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling)
- throws IOException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void flush() throws IOException {}
-
- @Override
- public void close() throws IOException {}
-
- @Override
- public int isSupportedOption(String option) {
- return delegate.isSupportedOption(option);
- }
-
- @Override
- public Location getLocationForModule(Location location, String moduleName) throws IOException {
- return delegate.getLocationForModule(location, moduleName);
- }
-
- @Override
- public Location getLocationForModule(Location location, JavaFileObject file) throws IOException {
- return delegate.getLocationForModule(location, file);
- }
-
- @Override
- public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
- getClass().getModule().addUses(service);
- return ServiceLoader.load(service, getClassLoader(location));
- }
-
- @Override
- public String inferModuleName(Location location) throws IOException {
- return delegate.inferModuleName(location);
- }
-
- @Override
- public Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException {
- return delegate.listLocationsForModules(location);
+ return super.getFileForInput(location, packageName, relativeName);
}
@Override
@@ -169,20 +97,10 @@ class SourceFileManager implements StandardJavaFileManager {
var source = (SourceFileObject) file;
return FileStore.contains(source.path);
} else {
- return delegate.contains(location, file);
+ return super.contains(location, file);
}
}
- private boolean contains(Set<Path> location, SourceFileObject source) {
- for (var root : location) {
- if (source.path.startsWith(root)) {
- return true;
- }
- }
- return false;
- }
-
- @Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
var result = new ArrayList<JavaFileObject>();
for (var f : files) {
@@ -191,32 +109,8 @@ class SourceFileManager implements StandardJavaFileManager {
return result;
}
- // Just for compatibility with StandardJavaFileManager
- // TODO delete this once we no longer need the useSourceFileManager flag
-
- @Override
- public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
- throw new UnsupportedOperationException();
- }
-
- @Override
public void setLocation(Location location, Iterable<? extends File> files) throws IOException {
- delegate.setLocation(location, files);
- }
-
- @Override
- public Iterable<? extends File> getLocation(Location location) {
- return delegate.getLocation(location);
+ fileManager.setLocation(location, files);
}
private static final Logger LOG = Logger.getLogger("main");
diff --git a/src/test/java/org/javacs/SourceFileManagerTest.java b/src/test/java/org/javacs/SourceFileManagerTest.java
index 523b6a4..64a35ac 100644
--- a/src/test/java/org/javacs/SourceFileManagerTest.java
+++ b/src/test/java/org/javacs/SourceFileManagerTest.java
@@ -21,8 +21,8 @@ public class SourceFileManagerTest {
private static SourceFileManager createSourceFileManager() {
var fileManager = new SourceFileManager();
try {
- fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, List.of(src));
- fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, List.of(classes));
+ fileManager.setLocation(StandardLocation.SOURCE_PATH, List.of(src.toFile()));
+ fileManager.setLocation(StandardLocation.CLASS_PATH, List.of(classes.toFile()));
} catch (IOException e) {
throw new RuntimeException(e);
}