Roaster parser and javadoc support

This commit is contained in:
BoykoAlex
2016-11-17 17:14:51 -05:00
parent f2abf2d21d
commit 2c9a250ec5
9 changed files with 390 additions and 55 deletions

View File

@@ -12,6 +12,12 @@
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<!-- roatser version -->
<roaster.version>2.19.2.Final</roaster.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
@@ -28,5 +34,16 @@
<artifactId>javaparser-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-api</artifactId>
<version>${roaster.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-jdt</artifactId>
<version>${roaster.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

View File

@@ -10,5 +10,5 @@ public interface IJavadocProvider {
IJavadoc getJavadoc(IMethod method);
IJavadoc getJavadoc(IAnnotation method);
IJavadoc getJavadoc(IAnnotation annotation);
}

View File

@@ -21,11 +21,11 @@ import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.visitor.GenericVisitorAdapter;
public class JavadocProvider implements IJavadocProvider {
public class ParserJavadocProvider implements IJavadocProvider {
private SourceUrlProvider sourceUrlProvider;
public JavadocProvider(SourceUrlProvider sourceUrlProvider) {
public ParserJavadocProvider(SourceUrlProvider sourceUrlProvider) {
this.sourceUrlProvider = sourceUrlProvider;
}

View File

@@ -0,0 +1,41 @@
package org.springframework.ide.vscode.commons.java.roaster;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.JavaUnit;
import org.springframework.ide.vscode.commons.util.Log;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
class DefaultJavaUnitIndex implements JavaUnitIndex {
private LoadingCache<URL, JavaUnit> cache = CacheBuilder.newBuilder().build(new CacheLoader<URL, JavaUnit>() {
@Override
public JavaUnit load(URL url) throws Exception {
InputStream in = url.openStream();
try {
return Roaster.parseUnit(in);
} finally {
in.close();
}
}
});
@Override
public JavaUnit getJavaUnit(URL url) {
try {
return cache.get(url);
} catch (ExecutionException e) {
Log.log(e);
}
return null;
}
}

View File

@@ -0,0 +1,13 @@
package org.springframework.ide.vscode.commons.java.roaster;
import java.net.URL;
import org.jboss.forge.roaster.model.JavaUnit;
public interface JavaUnitIndex {
static final JavaUnitIndex DEFAULT = new DefaultJavaUnitIndex();
JavaUnit getJavaUnit(URL url);
}

View File

@@ -0,0 +1,34 @@
package org.springframework.ide.vscode.commons.java.roaster;
import org.jboss.forge.roaster.model.JavaDoc;
import org.springframework.ide.vscode.commons.javadoc.IJavadoc;
public class RoasterJavadoc implements IJavadoc {
private JavaDoc<?> javadoc;
public RoasterJavadoc(JavaDoc<?> javadoc) {
this.javadoc = javadoc;
}
@Override
public String raw() {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String plainText() {
return javadoc.getFullText();
}
@Override
public String html() {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String markdown() {
throw new UnsupportedOperationException("Not yet implemented");
}
}

View File

@@ -0,0 +1,102 @@
package org.springframework.ide.vscode.commons.java.roaster;
import org.jboss.forge.roaster.model.Field;
import org.jboss.forge.roaster.model.FieldHolder;
import org.jboss.forge.roaster.model.JavaDocCapable;
import org.jboss.forge.roaster.model.JavaType;
import org.jboss.forge.roaster.model.JavaUnit;
import org.jboss.forge.roaster.model.Method;
import org.jboss.forge.roaster.model.MethodHolder;
import org.jboss.forge.roaster.model.TypeHolder;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.springframework.ide.vscode.commons.java.IAnnotation;
import org.springframework.ide.vscode.commons.java.IField;
import org.springframework.ide.vscode.commons.java.IJavadocProvider;
import org.springframework.ide.vscode.commons.java.IMethod;
import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.javadoc.IJavadoc;
import org.springframework.ide.vscode.commons.javadoc.SourceUrlProvider;
import org.springframework.ide.vscode.commons.util.Log;
public class RoasterJavadocProvider implements IJavadocProvider {
private SourceUrlProvider sourceUrlProvider;
public RoasterJavadocProvider(SourceUrlProvider sourceUrlProvider) {
this.sourceUrlProvider = sourceUrlProvider;
}
@Override
public IJavadoc getJavadoc(IType type) {
try {
JavaClassSource declaration = (JavaClassSource) getDeclaration(type);
return new RoasterJavadoc(declaration.getJavaDoc());
} catch (Exception e) {
Log.log(e);
return null;
}
}
@Override
public IJavadoc getJavadoc(IField field) {
try {
JavaType<?> typeDeclaration = getDeclaration(field.getDeclaringType());
if (typeDeclaration instanceof FieldHolder) {
Field<?> fieldDeclaration = ((FieldHolder<?>)typeDeclaration).getField(field.getElementName());
if (fieldDeclaration instanceof JavaDocCapable) {
return new RoasterJavadoc(((JavaDocCapable<?>)fieldDeclaration).getJavaDoc());
}
}
} catch (Exception e) {
Log.log(e);
}
return null;
}
@Override
public IJavadoc getJavadoc(IMethod method) {
if (method.parameters().findFirst().isPresent()) {
throw new UnsupportedOperationException("Only methods with no parameters are supported");
}
try {
JavaType<?> typeDeclaration = getDeclaration(method.getDeclaringType());
if (typeDeclaration instanceof MethodHolder) {
Method<?, ?> methodDeclaration = ((MethodHolder<?>)typeDeclaration).getMethod(method.getElementName());
if (methodDeclaration instanceof JavaDocCapable) {
return new RoasterJavadoc(((JavaDocCapable<?>)methodDeclaration).getJavaDoc());
}
}
} catch (Exception e) {
Log.log(e);
}
return null;
}
@Override
public IJavadoc getJavadoc(IAnnotation annotation) {
throw new UnsupportedOperationException("Not yet implemented");
}
private JavaUnit getJavaUnit(IType type) throws Exception {
return JavaUnitIndex.DEFAULT.getJavaUnit(sourceUrlProvider.sourceUrl(type));
}
private JavaType<?> getDeclaration(IType type) throws Exception {
if (type == null) {
return null;
}
IType declaringType = type.getDeclaringType();
if (declaringType == null) {
JavaUnit ju = getJavaUnit(type);
return ju.getTopLevelTypes().stream().filter(jt -> jt.getName().equals(type.getElementName())).findFirst().orElse(null);
} else {
JavaType<?> declaringTypeDeclaration = getDeclaration(declaringType);
if (declaringTypeDeclaration instanceof TypeHolder) {
return ((TypeHolder<?>)declaringTypeDeclaration).getNestedType(type.getElementName());
} else {
return null;
}
}
}
}

View File

@@ -25,7 +25,8 @@ import org.springframework.ide.vscode.commons.jandex.JandexIndex;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavadocProvider;
import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.java.parser.JavadocProvider;
import org.springframework.ide.vscode.commons.java.parser.ParserJavadocProvider;
import org.springframework.ide.vscode.commons.java.roaster.RoasterJavadocProvider;
import org.springframework.ide.vscode.commons.javadoc.SourceUrlProviderFromSourceContainer;
import org.springframework.ide.vscode.commons.maven.MavenCore;
import org.springframework.ide.vscode.commons.maven.MavenException;
@@ -41,6 +42,8 @@ import com.google.common.base.Suppliers;
*
*/
public class MavenProjectClasspath implements IClasspath {
public static boolean USE_JAVA_PARSER = false;
private MavenCore maven;
private MavenProject project;
@@ -60,7 +63,9 @@ public class MavenProjectClasspath implements IClasspath {
} catch (Exception e) {
Log.log(e);
}
return new JandexIndex(classpathEntries, jarFile -> findIndexFile(jarFile), classpathResource -> createJavadocProvider(classpathResource), maven.getJavaIndexForJreLibs());
return new JandexIndex(classpathEntries, jarFile -> findIndexFile(jarFile), classpathResource -> {
return USE_JAVA_PARSER ? createParserJavadocProvider(classpathResource) : createRoasterJavadocProvider(classpathResource);
}, maven.getJavaIndexForJreLibs());
});
}
@@ -84,40 +89,6 @@ public class MavenProjectClasspath implements IClasspath {
return maven.resolveDependencies(project, null).stream().filter(a -> file.equals(a.getFile())).findFirst();
}
private IJavadocProvider createJavadocProvider(File classpathResource) {
if (classpathResource.isDirectory()) {
if (classpathResource.toString().startsWith(project.getBuild().getOutputDirectory())) {
return new JavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(project.getBuild().getSourceDirectory()).toURI().toURL(), type);
});
} else if (classpathResource.toString().startsWith(project.getBuild().getTestOutputDirectory())) {
return new JavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(project.getBuild().getTestSourceDirectory()).toURI().toURL(), type);
});
} else {
throw new IllegalArgumentException("Cannot find source folder for " + classpathResource);
}
} else {
// Assume it's a JAR file
return new JavadocProvider(type -> {
try {
Artifact artifact = getArtifactFromJarFile(classpathResource).get();
URL sourceContainer = maven.getSources(artifact).getFile().toURI().toURL();
System.out.println("----> SOURCE " + sourceContainer);
return SourceUrlProviderFromSourceContainer.JAR_SOURCE_URL_PROVIDER.sourceUrl(sourceContainer,
type);
} catch (MavenException e) {
Log.log("Failed to find sources JAR for " + classpathResource, e);
} catch (MalformedURLException e) {
Log.log("Invalid URL for sources JAR for " + classpathResource, e);
}
return null;
});
}
}
@Override
public Stream<String> getClasspathResources() {
return project.getBuild().getResources().stream().flatMap(resource -> {
@@ -135,4 +106,72 @@ public class MavenProjectClasspath implements IClasspath {
});
}
private IJavadocProvider createRoasterJavadocProvider(File classpathResource) {
if (classpathResource.isDirectory()) {
if (classpathResource.toString().startsWith(project.getBuild().getOutputDirectory())) {
return new RoasterJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(project.getBuild().getSourceDirectory()).toURI().toURL(), type);
});
} else if (classpathResource.toString().startsWith(project.getBuild().getTestOutputDirectory())) {
return new RoasterJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(project.getBuild().getTestSourceDirectory()).toURI().toURL(), type);
});
} else {
throw new IllegalArgumentException("Cannot find source folder for " + classpathResource);
}
} else {
// Assume it's a JAR file
return new RoasterJavadocProvider(type -> {
try {
Artifact artifact = getArtifactFromJarFile(classpathResource).get();
URL sourceContainer = maven.getSources(artifact).getFile().toURI().toURL();
System.out.println("----> SOURCE " + sourceContainer);
return SourceUrlProviderFromSourceContainer.JAR_SOURCE_URL_PROVIDER.sourceUrl(sourceContainer,
type);
} catch (MavenException e) {
Log.log("Failed to find sources JAR for " + classpathResource, e);
} catch (MalformedURLException e) {
Log.log("Invalid URL for sources JAR for " + classpathResource, e);
}
return null;
});
}
}
private IJavadocProvider createParserJavadocProvider(File classpathResource) {
if (classpathResource.isDirectory()) {
if (classpathResource.toString().startsWith(project.getBuild().getOutputDirectory())) {
return new ParserJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(project.getBuild().getSourceDirectory()).toURI().toURL(), type);
});
} else if (classpathResource.toString().startsWith(project.getBuild().getTestOutputDirectory())) {
return new ParserJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(project.getBuild().getTestSourceDirectory()).toURI().toURL(), type);
});
} else {
throw new IllegalArgumentException("Cannot find source folder for " + classpathResource);
}
} else {
// Assume it's a JAR file
return new ParserJavadocProvider(type -> {
try {
Artifact artifact = getArtifactFromJarFile(classpathResource).get();
URL sourceContainer = maven.getSources(artifact).getFile().toURI().toURL();
System.out.println("----> SOURCE " + sourceContainer);
return SourceUrlProviderFromSourceContainer.JAR_SOURCE_URL_PROVIDER.sourceUrl(sourceContainer,
type);
} catch (MavenException e) {
Log.log("Failed to find sources JAR for " + classpathResource, e);
} catch (MalformedURLException e) {
Log.log("Invalid URL for sources JAR for " + classpathResource, e);
}
return null;
});
}
}
}

View File

@@ -17,6 +17,7 @@ import org.springframework.ide.vscode.commons.java.IPrimitiveType;
import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.java.IVoidType;
import org.springframework.ide.vscode.commons.maven.java.MavenJavaProject;
import org.springframework.ide.vscode.commons.maven.java.MavenProjectClasspath;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@@ -24,41 +25,56 @@ import com.google.common.cache.LoadingCache;
public class JavaIndexTest {
private static LoadingCache<String, MavenJavaProject> projectsCache = CacheBuilder.newBuilder().build(new CacheLoader<String, MavenJavaProject>() {
private static LoadingCache<String, Path> projectsCache = CacheBuilder.newBuilder().build(new CacheLoader<String, Path>() {
@Override
public Path load(String projectName) throws Exception {
Path testProjectPath = Paths.get(DependencyTreeTest.class.getResource("/" + projectName).toURI());
MavenCore.buildMavenProject(testProjectPath);
return testProjectPath;
}
});
private static LoadingCache<String, MavenJavaProject> mavenProjectsCache = CacheBuilder.newBuilder().build(new CacheLoader<String, MavenJavaProject>() {
@Override
public MavenJavaProject load(String projectName) throws Exception {
Path testProjectPath = Paths.get(DependencyTreeTest.class.getResource("/" + projectName).toURI());
MavenCore.buildMavenProject(testProjectPath);
return new MavenJavaProject(testProjectPath.resolve(MavenCore.POM_XML).toFile());
return createMavenProject(testProjectPath);
}
});
private static MavenJavaProject createMavenProject(Path projectPath) throws Exception {
return new MavenJavaProject(projectPath.resolve(MavenCore.POM_XML).toFile());
}
@Test
public void findClassInJar() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
MavenJavaProject project = mavenProjectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
IType type = project.findType("org.springframework.test.web.client.ExpectedCount");
assertNotNull(type);
}
@Test
public void findClassInOutputFolder() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
MavenJavaProject project = mavenProjectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
IType type = project.findType("hello.Greeting");
assertNotNull(type);
}
@Test
public void classNotFound() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
MavenJavaProject project = mavenProjectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
IType type = project.findType("hello.NonExistentClass");
assertNull(type);
}
@Test
public void voidMethodNoParams() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
MavenJavaProject project = mavenProjectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
IType type = project.findType("java.util.ArrayList");
assertNotNull(type);
IMethod m = type.getMethod("clear", Stream.empty());
@@ -69,7 +85,7 @@ public class JavaIndexTest {
@Test
public void voidConstructor() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
MavenJavaProject project = mavenProjectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
IType type = project.findType("java.util.ArrayList");
assertNotNull(type);
IMethod m = type.getMethod("<init>", Stream.empty());
@@ -80,7 +96,7 @@ public class JavaIndexTest {
@Test
public void constructorMethodWithParams() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
MavenJavaProject project = mavenProjectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
IType type = project.findType("java.util.ArrayList");
assertNotNull(type);
IMethod m = type.getMethod("<init>", Stream.of(IPrimitiveType.INT));
@@ -90,8 +106,9 @@ public class JavaIndexTest {
}
@Test
public void testClassJavadocForOutputFolder() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
public void parser_testClassJavadocForOutputFolder() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = true;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("hello.Greeting");
assertNotNull(type);
@@ -122,8 +139,9 @@ public class JavaIndexTest {
}
@Test
public void testInnerClassJavadocForOutputFolder() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
public void parser_testInnerClassJavadocForOutputFolder() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = true;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("hello.Greeting$TestInnerClass");
assertNotNull(type);
assertEquals("/**\n * Comment for inner class\n */", type.getJavaDoc().raw().trim());
@@ -138,8 +156,9 @@ public class JavaIndexTest {
}
@Test
public void testClassJavadocForJar() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
public void parser_testClassJavadocForJar() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = true;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener");
assertNotNull(type);
@@ -160,8 +179,9 @@ public class JavaIndexTest {
}
@Test
public void testFieldAndMethodJavadocForJar() throws Exception {
MavenJavaProject project = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
public void parser_testFieldAndMethodJavadocForJar() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = true;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("org.springframework.boot.SpringApplication");
assertNotNull(type);
@@ -184,4 +204,73 @@ public class JavaIndexTest {
assertEquals(expected, method.getJavaDoc().raw().trim().substring(0, expected.length()));
}
@Test
public void roaster_testClassJavadocForOutputFolder() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = false;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("hello.Greeting");
assertNotNull(type);
assertEquals("Comment for Greeting class", type.getJavaDoc().plainText());
IField field = type.getField("id");
assertNotNull(field);
assertEquals("Comment for id field", field.getJavaDoc().plainText());
IMethod method = type.getMethod("getId", Stream.empty());
assertNotNull(method);
assertEquals("Comment for getId()", method.getJavaDoc().plainText());
}
@Test
public void roaster_testInnerClassJavadocForOutputFolder() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = false;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("hello.Greeting$TestInnerClass");
assertNotNull(type);
assertEquals("Comment for inner class", type.getJavaDoc().plainText());
IField field = type.getField("innerField");
assertNotNull(field);
assertEquals("Comment for inner field", field.getJavaDoc().plainText());
IMethod method = type.getMethod("getInnerField", Stream.empty());
assertNotNull(method);
assertEquals("Comment for method inside nested class", method.getJavaDoc().plainText());
}
@Test
public void roaster_testClassJavadocForJar() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = false;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener");
assertNotNull(type);
String expected = "{@link ApplicationListener} that replaces the liquibase {@link ServiceLocator} with a";
assertEquals(expected, type.getJavaDoc().plainText().substring(0, expected.length()));
type = project.findType("org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener$LiquibasePresent");
assertNotNull(type);
assertEquals("Inner class to prevent class not found issues.", type.getJavaDoc().plainText());
}
@Test
public void roaster_testFieldAndMethodJavadocForJar() throws Exception {
MavenProjectClasspath.USE_JAVA_PARSER = false;
MavenJavaProject project = createMavenProject(projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file"));
IType type = project.findType("org.springframework.boot.SpringApplication");
assertNotNull(type);
IField field = type.getField("BANNER_LOCATION_PROPERTY_VALUE");
assertNotNull(field);
assertEquals("Default banner location.", field.getJavaDoc().plainText());
IMethod method = type.getMethod("getListeners", Stream.empty());
assertNotNull(method);
String expected = "Returns read-only ordered Set of the {@link ApplicationListener} s that will be";
assertEquals(expected, method.getJavaDoc().plainText().substring(0, expected.length()));
}
}