Javadoc support initial commit

This commit is contained in:
BoykoAlex
2016-11-15 19:32:31 -05:00
parent dcfb0b7e24
commit 1e2af95da5
5 changed files with 121 additions and 26 deletions

View File

@@ -23,5 +23,10 @@
<artifactId>jandex</artifactId>
<version>2.0.3.Final</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -6,11 +6,12 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexReader;
import org.jboss.jandex.IndexView;
@@ -24,12 +25,28 @@ import com.google.common.base.Suppliers;
public class JandexIndex {
private static class Entry<K, V> {
K key;
V value;
Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
@FunctionalInterface
public static interface IndexFileFinder {
File findIndexFile(File jarFile);
}
private Supplier<IndexView> index;
@FunctionalInterface
public static interface SourceContainerProvider {
File getSourceContainer(File container);
}
private Supplier<List<Entry<File, IndexView>>> index;
private SourceContainerProvider sourceContainerProvider;
public JandexIndex(Stream<Path> classpathEntries) {
this(classpathEntries, jarFile -> null, Optional.empty());
@@ -43,31 +60,38 @@ public class JandexIndex {
this(classpathEntries, jarFile -> null, baseIndex);
}
public void setSourceContainerProvider(SourceContainerProvider sourceContainerProvider) {
this.sourceContainerProvider = sourceContainerProvider;
}
public SourceContainerProvider getSourceContainerProvider() {
return sourceContainerProvider;
}
public JandexIndex(Stream<Path> classpathEntries, IndexFileFinder indexFileFinder, Optional<JandexIndex> baseIndex) {
index = Suppliers.memoize(() -> {
List<Entry<File, IndexView>> indices = buildIndex(classpathEntries, indexFileFinder).collect(Collectors.toList());
if (baseIndex.isPresent()) {
return CompositeIndex.create(baseIndex.get().index.get(), buildIndex(classpathEntries, indexFileFinder));
} else {
return buildIndex(classpathEntries, indexFileFinder);
indices.addAll(baseIndex.get().index.get());
}
return indices;
});
}
private static CompositeIndex buildIndex(Stream<Path> classpathEntries, IndexFileFinder indexFileFinder) {
return CompositeIndex.create(classpathEntries
private Stream<Entry<File, IndexView>> buildIndex(Stream<Path> classpathEntries, IndexFileFinder indexFileFinder) {
return classpathEntries
.map(entry -> entry.toFile())
.map(file -> {
Optional<IndexView> index = Optional.empty();
if (file.isFile() && file.getName().endsWith(".jar")) {
return indexJar(file, indexFileFinder);
index = indexJar(file, indexFileFinder);
} else if (file.isDirectory()) {
return indexFolder(file);
} else {
return Optional.<IndexView>empty();
index = indexFolder(file);
}
return new Entry<>(file, index);
})
.filter(o -> o.isPresent())
.map(o -> o.get())
.collect(Collectors.toList()));
.filter(e -> e.value.isPresent())
.map(e -> new Entry<>(e.key, e.value.get()));
}
private static Optional<IndexView> indexFolder(File folder) {
@@ -134,8 +158,16 @@ public class JandexIndex {
}
public IType findType(String fqName) {
IndexView compositeIndex = index.get();
return Wrappers.wrap(compositeIndex, compositeIndex.getClassByName(DotName.createSimple(fqName)));
return getClassByName(DotName.createSimple(fqName));
}
IType getClassByName(DotName className) {
Optional<Entry<File, ClassInfo>> pair = index.get().stream().map(e -> new Entry<>(e.key, e.value.getClassByName(className))).filter(e -> e.value != null).findFirst();
if (pair.isPresent()) {
return Wrappers.wrap(this, pair.get().value, pair.get().key);
} else {
return null;
}
}
}

View File

@@ -2,6 +2,7 @@ package org.springframework.ide.vscode.commons.jandex;
import static org.springframework.ide.vscode.commons.util.Assert.isNotNull;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -29,7 +30,7 @@ import org.springframework.ide.vscode.commons.util.HtmlSnippet;
public class Wrappers {
public static IType wrap(IndexView index, ClassInfo info) {
public static IType wrap(JandexIndex index, ClassInfo info, File container) {
if (info == null) {
return null;
}
@@ -43,7 +44,7 @@ public class Wrappers {
@Override
public IType getDeclaringType() {
DotName enclosingClass = info.enclosingClass();
return enclosingClass == null ? null : wrap(index, index.getClassByName(enclosingClass));
return enclosingClass == null ? null : index.getClassByName(enclosingClass);
}
@Override
@@ -89,26 +90,26 @@ public class Wrappers {
@Override
public IField getField(String name) {
return wrap(index, info.field(name));
return wrap(index, info.field(name), container);
}
@Override
public Stream<IField> getFields() {
return info.fields().stream().map(f -> {
return wrap(index, f);
return wrap(index, f, container);
});
}
@Override
public IMethod getMethod(String name, Stream<IJavaType> parameters) {
List<Type> typeParameters = parameters.map(Wrappers::from).collect(Collectors.toList());
return wrap(index, info.method(name, typeParameters.toArray(new Type[typeParameters.size()])));
return wrap(index, info.method(name, typeParameters.toArray(new Type[typeParameters.size()])), container);
}
@Override
public Stream<IMethod> getMethods() {
return info.methods().stream().map(m -> {
return wrap(index, m);
return wrap(index, m, container);
});
}
@@ -120,7 +121,7 @@ public class Wrappers {
};
}
public static IField wrap(IndexView index, FieldInfo field) {
public static IField wrap(JandexIndex index, FieldInfo field, File container) {
if (field == null) {
return null;
}
@@ -133,7 +134,7 @@ public class Wrappers {
@Override
public IType getDeclaringType() {
return wrap(index, field.declaringClass());
return wrap(index, field.declaringClass(), container);
}
@Override
@@ -170,7 +171,7 @@ public class Wrappers {
};
}
public static IMethod wrap(IndexView index, MethodInfo method) {
public static IMethod wrap(JandexIndex index, MethodInfo method, File container) {
isNotNull(index);
isNotNull(method);
return new IMethod() {
@@ -182,7 +183,7 @@ public class Wrappers {
@Override
public IType getDeclaringType() {
return wrap(index, method.declaringClass());
return wrap(index, method.declaringClass(), container);
}
@Override

View File

@@ -0,0 +1,13 @@
package org.springframework.ide.vscode.commons.java.parser;
import java.net.URL;
import com.github.javaparser.ast.CompilationUnit;
public interface CompilationUnitIndex {
static final CompilationUnitIndex DEFAULT = new DefaultCompilationUnitIndex();
CompilationUnit getCompilationUnit(URL url);
}

View File

@@ -0,0 +1,44 @@
package org.springframework.ide.vscode.commons.java.parser;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import org.springframework.ide.vscode.commons.util.Log;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
class DefaultCompilationUnitIndex implements CompilationUnitIndex {
private LoadingCache<URL, CompilationUnit> cache = CacheBuilder.newBuilder().build(new CacheLoader<URL, CompilationUnit>() {
@Override
public CompilationUnit load(URL url) throws Exception {
InputStream in = url.openStream();
try {
return JavaParser.parse(in);
} catch (ParseException e) {
in.close();
Log.log("Failed to parse java source file: " + url, e);
}
return null;
}
});
@Override
public CompilationUnit getCompilationUnit(URL url) {
try {
return cache.get(url);
} catch (ExecutionException e) {
Log.log(e);
}
return null;
}
}