summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2018-09-16 17:18:11 -0700
committerGeorge Fraser <george@fivetran.com>2018-09-16 17:18:11 -0700
commit177395e3bd3f59064adc0e8744af6173dd8aae03 (patch)
tree5e0e04e6a39509084ca6b262b319b49c4ba9e104 /src
parentdf76c549836fcb2707a36b091023c2d0062ea293 (diff)
downloadjava-language-server-177395e3bd3f59064adc0e8744af6173dd8aae03.zip
Modularize
Diffstat (limited to 'src')
-rw-r--r--src/main/java/module-info.java13
-rw-r--r--src/main/java/org/javacs/HideModuleInfo.java228
-rw-r--r--src/main/java/org/javacs/InferSourcePath.java2
-rw-r--r--src/main/java/org/javacs/JavaCompilerService.java20
-rw-r--r--src/main/java/org/javacs/JavaLanguageServer.java5
-rw-r--r--src/test/java/org/javacs/CodeLensTest.java8
6 files changed, 268 insertions, 8 deletions
diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java
new file mode 100644
index 0000000..32d049b
--- /dev/null
+++ b/src/main/java/module-info.java
@@ -0,0 +1,13 @@
+module javacs {
+ requires com.google.common;
+ requires jdk.compiler;
+ requires java.logging;
+ requires org.eclipse.lsp4j;
+ requires gson;
+ requires org.eclipse.lsp4j.jsonrpc;
+ requires com.fasterxml.jackson.core;
+ requires com.fasterxml.jackson.databind;
+ requires com.fasterxml.jackson.datatype.jdk8;
+ requires com.fasterxml.jackson.datatype.jsr310;
+ requires remark;
+}
diff --git a/src/main/java/org/javacs/HideModuleInfo.java b/src/main/java/org/javacs/HideModuleInfo.java
new file mode 100644
index 0000000..19770fc
--- /dev/null
+++ b/src/main/java/org/javacs/HideModuleInfo.java
@@ -0,0 +1,228 @@
+package org.javacs;
+
+import com.google.common.collect.Iterables;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.ServiceLoader;
+import java.util.Set;
+import javax.tools.FileObject;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+
+class HideModuleInfo implements StandardJavaFileManager {
+ private final StandardJavaFileManager delegate;
+
+ HideModuleInfo(StandardJavaFileManager delegate) {
+ this.delegate = delegate;
+ }
+
+ private boolean notModuleInfo(JavaFileObject file) {
+ return file == null || !file.getName().endsWith("module-info.java");
+ }
+
+ private boolean notModuleInfo(File file) {
+ return file == null || !file.getName().endsWith("module-info.java");
+ }
+
+ private boolean notModuleInfo(Path file) {
+ return file == null || !file.getFileName().endsWith("module-info.java");
+ }
+
+ private JavaFileObject skipModuleInfo(JavaFileObject file) {
+ if (file == null) return null;
+ if (file.getName().endsWith("module-info.java")) return null;
+ else return file;
+ }
+
+ private FileObject skipModuleInfo(FileObject file) {
+ if (file == null) return null;
+ if (file.getName().endsWith("module-info.java")) return null;
+ else return file;
+ }
+
+ private Iterable<? extends JavaFileObject> removeModuleInfo(Iterable<? extends JavaFileObject> in) {
+ return Iterables.filter(in, this::notModuleInfo);
+ }
+
+ private Iterable<JavaFileObject> removeModuleInfoInvariant(Iterable<JavaFileObject> in) {
+ return Iterables.filter(in, this::notModuleInfo);
+ }
+
+ private Iterable<? extends File> removeModuleInfoFile(Iterable<? extends File> in) {
+ return Iterables.filter(in, this::notModuleInfo);
+ }
+
+ private Iterable<? extends Path> removeModuleInfoPath(Iterable<? extends Path> in) {
+ return Iterables.filter(in, this::notModuleInfo);
+ }
+
+ @Override
+ public boolean isSameFile(FileObject a, FileObject b) {
+ return delegate.isSameFile(a, b);
+ }
+
+ @Override
+ public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
+ return removeModuleInfo(delegate.getJavaFileObjectsFromFiles(files));
+ }
+
+ @Override
+ public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(Iterable<? extends Path> paths) {
+ return removeModuleInfo(delegate.getJavaFileObjectsFromPaths(paths));
+ }
+
+ @Override
+ public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
+ return removeModuleInfo(delegate.getJavaFileObjects(files));
+ }
+
+ @Override
+ public Iterable<? extends JavaFileObject> getJavaFileObjects(Path... paths) {
+ return removeModuleInfo(delegate.getJavaFileObjects(paths));
+ }
+
+ @Override
+ public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
+ return removeModuleInfo(delegate.getJavaFileObjectsFromStrings(names));
+ }
+
+ @Override
+ public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
+ return removeModuleInfo(delegate.getJavaFileObjects(names));
+ }
+
+ @Override
+ public void setLocation(Location location, Iterable<? extends File> files) throws IOException {
+ delegate.setLocation(location, files);
+ }
+
+ @Override
+ public void setLocationFromPaths(Location location, Collection<? extends Path> paths) throws IOException {
+ delegate.setLocationFromPaths(location, paths);
+ }
+
+ @Override
+ public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths)
+ throws IOException {
+ delegate.setLocationForModule(location, moduleName, paths);
+ }
+
+ @Override
+ public Iterable<? extends File> getLocation(Location location) {
+ return removeModuleInfoFile(delegate.getLocation(location));
+ }
+
+ @Override
+ public Iterable<? extends Path> getLocationAsPaths(Location location) {
+ return removeModuleInfoPath(delegate.getLocationAsPaths(location));
+ }
+
+ @Override
+ public Path asPath(FileObject file) {
+ return delegate.asPath(file);
+ }
+
+ @Override
+ public void setPathFactory(PathFactory f) {
+ delegate.setPathFactory(f);
+ }
+
+ @Override
+ public ClassLoader getClassLoader(Location location) {
+ return delegate.getClassLoader(location);
+ }
+
+ @Override
+ public Iterable<JavaFileObject> list(
+ Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
+ return removeModuleInfoInvariant(delegate.list(location, packageName, kinds, recurse));
+ }
+
+ @Override
+ public String inferBinaryName(Location location, JavaFileObject file) {
+ return delegate.inferBinaryName(location, file);
+ }
+
+ @Override
+ public boolean handleOption(String current, Iterator<String> remaining) {
+ return delegate.handleOption(current, remaining);
+ }
+
+ @Override
+ public boolean hasLocation(Location location) {
+ return delegate.hasLocation(location);
+ }
+
+ @Override
+ public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind)
+ throws IOException {
+ return skipModuleInfo(delegate.getJavaFileForInput(location, className, kind));
+ }
+
+ @Override
+ public JavaFileObject getJavaFileForOutput(
+ Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
+ return skipModuleInfo(delegate.getJavaFileForOutput(location, className, kind, sibling));
+ }
+
+ @Override
+ public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
+ return skipModuleInfo(delegate.getFileForInput(location, packageName, relativeName));
+ }
+
+ @Override
+ public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling)
+ throws IOException {
+ return skipModuleInfo(delegate.getFileForOutput(location, packageName, relativeName, sibling));
+ }
+
+ @Override
+ public void flush() throws IOException {
+ delegate.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ delegate.close();
+ }
+
+ @Override
+ public Location getLocationForModule(Location location, String moduleName) throws IOException {
+ return delegate.getLocationForModule(location, moduleName);
+ }
+
+ @Override
+ public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
+ return delegate.getLocationForModule(location, fo);
+ }
+
+ @Override
+ public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
+ return delegate.getServiceLoader(location, service);
+ }
+
+ @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);
+ }
+
+ @Override
+ public boolean contains(Location location, FileObject fo) throws IOException {
+ if (fo.getName().endsWith("module-info.java")) return false;
+
+ return delegate.contains(location, fo);
+ }
+
+ @Override
+ public int isSupportedOption(String option) {
+ return delegate.isSupportedOption(option);
+ }
+}
diff --git a/src/main/java/org/javacs/InferSourcePath.java b/src/main/java/org/javacs/InferSourcePath.java
index f261d12..d54c987 100644
--- a/src/main/java/org/javacs/InferSourcePath.java
+++ b/src/main/java/org/javacs/InferSourcePath.java
@@ -58,6 +58,8 @@ class InferSourcePath {
@Override
public void accept(Path java) {
+ if (java.getFileName().toString().equals("module-info.java")) return;
+
if (!alreadyKnown(java)) {
infer(java)
.ifPresent(
diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java
index 46d767c..a4fed6f 100644
--- a/src/main/java/org/javacs/JavaCompilerService.java
+++ b/src/main/java/org/javacs/JavaCompilerService.java
@@ -51,16 +51,12 @@ public class JavaCompilerService {
private 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
private final StandardJavaFileManager fileManager =
- compiler.getStandardFileManager(diags::add, null, Charset.defaultCharset());
+ new HideModuleInfo(compiler.getStandardFileManager(diags::add, null, Charset.defaultCharset()));
// Cache a single compiled file
// Since the user can only edit one file at a time, this should be sufficient
private Cache cache;
public JavaCompilerService(Set<Path> sourcePath, Set<Path> classPath, Set<Path> docPath) {
- var klass = compiler.getClass();
- var path = klass.getName().replace('.', '/');
- var location = klass.getResource(String.format("/%s.class", path));
-
System.err.println("Source path:");
for (var p : sourcePath) {
System.err.println(" " + p);
@@ -152,7 +148,21 @@ public class JavaCompilerService {
fileManager.getJavaFileObjectsFromFiles(files));
}
+ private Collection<Path> removeModuleInfo(Collection<Path> files) {
+ var result = new ArrayList<Path>();
+ for (var f : files) {
+ if (f.getFileName().endsWith("module-info.java"))
+ LOG.info("Skip " + f);
+ else
+ result.add(f);
+ }
+ return result;
+ }
+
List<Diagnostic<? extends JavaFileObject>> lint(Collection<Path> files) {
+ files = removeModuleInfo(files);
+ if (files.isEmpty()) return Collections.emptyList();
+
var task = batchTask(files);
try {
task.parse();
diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java
index eb2d68f..79f7768 100644
--- a/src/main/java/org/javacs/JavaLanguageServer.java
+++ b/src/main/java/org/javacs/JavaLanguageServer.java
@@ -59,6 +59,11 @@ class JavaLanguageServer implements LanguageServer {
for (var f : files) {
List<org.eclipse.lsp4j.Diagnostic> ds = new ArrayList<>();
for (var j : javaDiagnostics) {
+ if (j.getSource() == null) {
+ LOG.warning("No source in warning " + j.getMessage(null));
+ continue;
+ }
+
var uri = j.getSource().toUri();
if (uri.equals(f)) {
var content = textDocuments.contents(uri).content;
diff --git a/src/test/java/org/javacs/CodeLensTest.java b/src/test/java/org/javacs/CodeLensTest.java
index 2d02fd1..6f0c802 100644
--- a/src/test/java/org/javacs/CodeLensTest.java
+++ b/src/test/java/org/javacs/CodeLensTest.java
@@ -5,6 +5,7 @@ import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.ExecutionException;
import org.eclipse.lsp4j.CodeLens;
@@ -31,7 +32,7 @@ public class CodeLensTest {
for (var lens : lenses) {
var command = new StringJoiner(", ");
for (var arg : lens.getCommand().getArguments()) {
- command.add(arg.toString());
+ command.add(Objects.toString(arg));
}
commands.add(command.toString());
}
@@ -45,7 +46,8 @@ public class CodeLensTest {
assertThat(lenses, not(empty()));
var commands = commands(lenses);
- assertThat(commands, hasItem("HasTest, testMethod"));
- assertThat(commands, hasItem("HasTest, otherTestMethod"));
+ assertThat(commands, hasItem(containsString("HasTest, null")));
+ assertThat(commands, hasItem(containsString("HasTest, testMethod")));
+ assertThat(commands, hasItem(containsString("HasTest, otherTestMethod")));
}
}