summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/javacs/FixImports.java48
-rw-r--r--src/main/java/org/javacs/InferSourcePath.java2
-rw-r--r--src/test/java/org/javacs/FixImportsTest.java9
-rw-r--r--src/test/java/org/javacs/JavaCompilerServiceTest.java2
-rw-r--r--src/test/resources/HasImport.java5
5 files changed, 61 insertions, 5 deletions
diff --git a/src/main/java/org/javacs/FixImports.java b/src/main/java/org/javacs/FixImports.java
index b023d44..50d531d 100644
--- a/src/main/java/org/javacs/FixImports.java
+++ b/src/main/java/org/javacs/FixImports.java
@@ -3,9 +3,12 @@ package org.javacs;
import com.google.common.base.Joiner;
import com.google.common.reflect.*;
import com.sun.source.tree.*;
+import java.io.*;
import java.nio.file.*;
import java.util.*;
+import java.util.function.*;
import java.util.logging.Logger;
+import java.util.regex.*;
import java.util.stream.*;
class FixImports {
@@ -23,9 +26,48 @@ class FixImports {
}
/** Find all already-imported symbols in all .java files in sourcePath */
- ExistingImports existingImports(List<Path> sourcePath) {
- // TODO
- return new ExistingImports(Collections.emptySet(), Collections.emptySet());
+ ExistingImports existingImports(Collection<Path> sourcePath) {
+ Set<String> classes = new HashSet<>(), packages = new HashSet<>();
+ Pattern importClass = Pattern.compile("^import +(([\\w\\.]+)\\.\\w+);"),
+ importStar = Pattern.compile("^import +([\\w\\.]+)\\.\\*;"),
+ importSimple = Pattern.compile("^import +(\\w+);");
+ Consumer<Path> findImports =
+ path -> {
+ try {
+ Iterator<String> lines = Files.lines(path).iterator();
+ int countNonImportLines = 0;
+ while (lines.hasNext() && countNonImportLines < 10) {
+ String line = lines.next();
+ if (!line.startsWith("import ")) {
+ countNonImportLines++;
+ continue;
+ }
+ // import foo.bar.Doh;
+ Matcher matchesClass = importClass.matcher(line);
+ if (matchesClass.matches()) {
+ String className = matchesClass.group(1), packageName = matchesClass.group(2);
+ packages.add(packageName);
+ classes.add(className);
+ }
+ // import foo.bar.*
+ Matcher matchesStar = importStar.matcher(line);
+ if (matchesStar.matches()) {
+ String packageName = matchesStar.group(1);
+ packages.add(packageName);
+ }
+ // import Doh
+ Matcher matchesSimple = importSimple.matcher(line);
+ if (matchesSimple.matches()) {
+ String className = matchesSimple.group(1);
+ classes.add(className);
+ }
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ };
+ sourcePath.stream().flatMap(InferSourcePath::allJavaFiles).forEach(findImports);
+ return new ExistingImports(classes, packages);
}
private Optional<String> resolveSymbol(String unresolved, ExistingImports imports) {
diff --git a/src/main/java/org/javacs/InferSourcePath.java b/src/main/java/org/javacs/InferSourcePath.java
index fa0cb84..3f0bce9 100644
--- a/src/main/java/org/javacs/InferSourcePath.java
+++ b/src/main/java/org/javacs/InferSourcePath.java
@@ -11,7 +11,7 @@ import javax.tools.*;
class InferSourcePath {
- private static Stream<Path> allJavaFiles(Path dir) {
+ static Stream<Path> allJavaFiles(Path dir) {
PathMatcher match = FileSystems.getDefault().getPathMatcher("glob:*.java");
try {
diff --git a/src/test/java/org/javacs/FixImportsTest.java b/src/test/java/org/javacs/FixImportsTest.java
index 4b77087..786c6fa 100644
--- a/src/test/java/org/javacs/FixImportsTest.java
+++ b/src/test/java/org/javacs/FixImportsTest.java
@@ -3,6 +3,7 @@ package org.javacs;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
+import java.nio.file.*;
import java.util.*;
import org.junit.Test;
@@ -16,4 +17,12 @@ public class FixImportsTest {
Map<String, String> resolved = fix.resolveSymbols(Collections.singleton("List"), emptyImports);
assertThat(resolved, hasEntry("List", "java.util.List"));
}
+
+ @Test
+ public void findExistingImports() {
+ FixImports.ExistingImports find =
+ fix.existingImports(Collections.singleton(JavaCompilerServiceTest.resourcesDir()));
+ assertThat(find.classes, hasItem("java.util.List"));
+ assertThat(find.packages, hasItem("java.util"));
+ }
}
diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java
index 39989b0..8320d0c 100644
--- a/src/test/java/org/javacs/JavaCompilerServiceTest.java
+++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java
@@ -26,7 +26,7 @@ public class JavaCompilerServiceTest {
new JavaCompilerService(
Collections.singleton(resourcesDir()), Collections.emptySet(), Collections.emptySet());
- private static Path resourcesDir() {
+ static Path resourcesDir() {
try {
return Paths.get(JavaCompilerServiceTest.class.getResource("/HelloWorld.java").toURI()).getParent();
} catch (URISyntaxException e) {
diff --git a/src/test/resources/HasImport.java b/src/test/resources/HasImport.java
new file mode 100644
index 0000000..2b4f66e
--- /dev/null
+++ b/src/test/resources/HasImport.java
@@ -0,0 +1,5 @@
+import java.util.List;
+
+class HasImport {
+
+} \ No newline at end of file