Implement getSourceFolders in JdtLsClasspath

Also various refactoring and bug fixing
This commit is contained in:
Kris De Volder
2018-04-24 11:56:05 -07:00
parent b8c4384441
commit d72d4b7bc8
22 changed files with 333 additions and 136 deletions

View File

@@ -12,6 +12,7 @@ package org.springframework.ide.vscode.commons.jandex;
import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -24,6 +25,7 @@ import org.springframework.ide.vscode.commons.util.Log;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import reactor.core.publisher.Flux;
import reactor.util.function.Tuple2;
@@ -51,13 +53,16 @@ public abstract class JandexClasspath implements IClasspath {
}
protected JandexIndex createIndex() {
Stream<Path> classpathEntries = Stream.empty();
Collection<Path> classpathEntries = ImmutableList.of();
try {
classpathEntries = getClasspathEntries().stream();
classpathEntries = getClasspathEntryPaths();
for (Path path : classpathEntries) {
System.out.println(path);
}
} catch (Exception e) {
Log.log(e);
}
return new JandexIndex(classpathEntries.map(p -> p.toFile()).collect(Collectors.toList()), jarFile -> findIndexFile(jarFile), classpathResource -> {
return new JandexIndex(classpathEntries.stream().map(p -> p.toFile()).collect(Collectors.toList()), jarFile -> findIndexFile(jarFile), classpathResource -> {
switch (providerType) {
// case JAVA_PARSER:
// return createParserJavadocProvider(classpathResource);

View File

@@ -20,7 +20,7 @@ public class BootProjectUtil {
try {
IClasspath cp = jp.getClasspath();
if (cp!=null) {
return cp.getClasspathEntries().stream().anyMatch(cpe -> isBootEntry(cpe));
return cp.getClasspathEntryPaths().stream().anyMatch(cpe -> isBootEntry(cpe));
}
} catch (Exception e) {
Log.log(e);

View File

@@ -11,11 +11,13 @@
package org.springframework.ide.vscode.commons.java;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath.CPE;
import org.springframework.ide.vscode.commons.util.Log;
import com.google.common.base.Objects;
@@ -25,18 +27,58 @@ public class ClasspathData {
final public static ClasspathData EMPTY_CLASSPATH_DATA = new ClasspathData(null, Collections.emptySet(),
Collections.emptySet(), null);
final public String name;
final public Set<Path> classpathEntries;
final public Set<String> classpathResources;
final public Path outputFolder;
private String name;
private Set<CPE> classpathEntries;
private Set<String> classpathResources;
private String outputFolder;
public ClasspathData(String name, Set<Path> classpathEntries, Set<String> classpathResources, Path outputFolder) {
public ClasspathData() {
}
public ClasspathData(String name, Set<CPE> classpathEntries, Set<String> classpathResources, String outputFolder) {
this.name = name;
this.classpathEntries = classpathEntries;
this.classpathResources = classpathResources;
this.outputFolder = outputFolder;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<CPE> getClasspathEntries() {
return classpathEntries;
}
public void setClasspathEntries(Set<CPE> classpathEntries) {
this.classpathEntries = classpathEntries;
}
public Set<String> getClasspathResources() {
return classpathResources;
}
public void setClasspathResources(Set<String> classpathResources) {
this.classpathResources = classpathResources;
}
public String getOutputFolder() {
return outputFolder;
}
public void setOutputFolder(String outputFolder) {
this.outputFolder = outputFolder;
}
public static ClasspathData getEmptyClasspathData() {
return EMPTY_CLASSPATH_DATA;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ClasspathData) {
@@ -52,9 +94,12 @@ public class ClasspathData {
return false;
}
public static ClasspathData from(String name, Collection<Path> classpathEntries,
public static ClasspathData from(String name, Collection<CPE> classpathEntries,
Collection<String> classpathResources, Path outputFolder) {
return new ClasspathData(name, new LinkedHashSet<>(classpathEntries), new LinkedHashSet<>(classpathResources),
outputFolder);
return new ClasspathData(name,
new LinkedHashSet<>(classpathEntries),
new LinkedHashSet<>(classpathResources),
outputFolder==null ? null : outputFolder.toString()
);
}
}

View File

@@ -23,6 +23,8 @@ import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.ide.vscode.commons.util.Log;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ClasspathFileBasedCache {
public static final ClasspathFileBasedCache NULL = new ClasspathFileBasedCache(null);
@@ -45,13 +47,9 @@ public class ClasspathFileBasedCache {
FileWriter writer = null;
try {
Files.createDirectories(file.getParentFile().toPath());
JSONObject json = new JSONObject();
json.put(NAME_PROPERTY, data.name);
json.put(CLASSPATH_ENTRIES_PROPERTY, data.classpathEntries.stream().map(e -> e.toString()).collect(Collectors.toList()));
json.put(CLASSPATH_RESOURCES_PROPERTY, data.classpathResources);
json.put(OUTPUT_FOLDER_PROPERTY, data.outputFolder);
writer = new FileWriter(file);
json.write(writer);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, data);
} catch (IOException e) {
Log.log(e);
} finally {
@@ -69,30 +67,12 @@ public class ClasspathFileBasedCache {
public boolean isCached() {
return file != null && file.exists();
}
public synchronized ClasspathData load() {
public synchronized ClasspathData load() {
if (file != null && file.exists()) {
ObjectMapper mapper = new ObjectMapper();
try {
JSONObject json = new JSONObject(new JSONTokener(new FileInputStream(file)));
String name = json.getString(NAME_PROPERTY);
JSONArray classpathEntriesJson = json.optJSONArray(CLASSPATH_ENTRIES_PROPERTY);
JSONArray classpathResourcesJson = json.optJSONArray(CLASSPATH_RESOURCES_PROPERTY);
String outputFolderStr = json.optString(OUTPUT_FOLDER_PROPERTY);
return new ClasspathData(
name,
classpathEntriesJson == null ? Collections.emptySet() : classpathEntriesJson.toList().stream()
.filter(o -> o instanceof String)
.map(o -> (String) o)
.map(s -> new File(s).toPath())
.collect(Collectors.toSet()),
classpathResourcesJson == null ? Collections.emptySet() : classpathResourcesJson.toList().stream()
.filter(o -> o instanceof String)
.map(o -> (String) o)
.collect(Collectors.toSet()),
outputFolderStr == null ? null : new File(outputFolderStr).toPath()
);
return mapper.readValue(file, ClasspathData.class);
} catch (Throwable e) {
Log.log(e);
}

View File

@@ -13,11 +13,13 @@ package org.springframework.ide.vscode.commons.java;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath.CPE;
import org.springframework.ide.vscode.commons.util.Assert;
import com.google.common.base.Objects;
@@ -79,22 +81,23 @@ public class DelegatingCachedClasspath<T extends IClasspath> implements IClasspa
@Override
public String getName() {
return cachedData.get().name;
return cachedData.get().getName();
}
@Override
public Path getOutputFolder() {
return cachedData.get().outputFolder;
String of = cachedData.get().getOutputFolder();
return of == null ? null : Paths.get(of);
}
@Override
public ImmutableList<Path> getClasspathEntries() throws Exception {
return ImmutableList.copyOf(cachedData.get().classpathEntries);
public ImmutableList<CPE> getClasspathEntries() throws Exception {
return ImmutableList.copyOf(cachedData.get().getClasspathEntries());
}
@Override
public ImmutableList<String> getClasspathResources() {
return ImmutableList.copyOf(cachedData.get().classpathResources);
return ImmutableList.copyOf(cachedData.get().getClasspathResources());
}
public boolean isCached() {

View File

@@ -13,9 +13,15 @@ package org.springframework.ide.vscode.commons.java;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.function.Predicate;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath.CPE;
import com.google.common.collect.ImmutableList;
import reactor.core.publisher.Flux;
@@ -50,7 +56,7 @@ public interface IClasspath {
* @return collection of classpath entries in a form file/folder paths
* @throws Exception
*/
ImmutableList<Path> getClasspathEntries() throws Exception;
Collection<CPE> getClasspathEntries() throws Exception;
/**
* Classpath resources paths relative to the source folder path
@@ -67,4 +73,25 @@ public interface IClasspath {
void reindex();
Optional<URL> sourceContainer(File classpathResource);
@Deprecated
default Collection<Path> getClasspathEntryPaths() throws Exception {
LinkedHashSet<Path> entries = new LinkedHashSet<>();
for (CPE cpe : this.getClasspathEntries()) {
if (Classpath.ENTRY_KIND_BINARY.equals(cpe.getKind())) {
entries.add(Paths.get(cpe.getPath()));
} else if (Classpath.ENTRY_KIND_SOURCE.equals(cpe.getKind())) {
String of = cpe.getOutputFolder();
if (of!=null) {
entries.add(Paths.get(cpe.getOutputFolder()));
} else {
Path op = getOutputFolder();
if (op!=null) {
entries.add(op);
}
}
}
}
return ImmutableList.copyOf(entries);
}
}