Remove github java parser

Seem like it simply was not being used, except in test code.
This commit is contained in:
Kris De Volder
2018-02-28 16:03:14 -08:00
parent d1b214a7ad
commit b148969f17
7 changed files with 8 additions and 449 deletions

View File

@@ -28,7 +28,6 @@ import org.springframework.ide.vscode.commons.jandex.JandexIndex;
import org.springframework.ide.vscode.commons.java.ClasspathData;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavadocProvider;
import org.springframework.ide.vscode.commons.java.parser.ParserJavadocProvider;
import org.springframework.ide.vscode.commons.javadoc.HtmlJavadocProvider;
import org.springframework.ide.vscode.commons.javadoc.SourceUrlProviderFromSourceContainer;
import org.springframework.ide.vscode.commons.util.Log;
@@ -139,28 +138,6 @@ public class GradleProjectClasspath extends JandexClasspath {
return project != null;
}
@Override
protected IJavadocProvider createParserJavadocProvider(File classpathResource) {
if (project != null) {
if (classpathResource.isDirectory()) {
Optional<File> classpathFolder = project.getSourceDirectories().stream()
.map(dir -> dir.getDirectory())
.filter(dir -> classpathResource.toPath().startsWith(dir.toPath()))
.findFirst();
if (classpathFolder.isPresent()) {
return new ParserJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(classpathFolder.get().toURI().toURL(), type.getFullyQualifiedName());
});
}
} else {
}
}
return null;
}
@Override
protected IJavadocProvider createHtmlJavdocProvider(File classpathResource) {
return null;

View File

@@ -40,11 +40,6 @@
<artifactId>remark</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.2.12</version>
</dependency>
<!-- Reactor -->
<dependency>
<groupId>io.projectreactor</groupId>

View File

@@ -39,7 +39,8 @@ public abstract class JandexClasspath implements IClasspath {
public static JavadocProviderTypes providerType = JavadocProviderTypes.HTML;
public enum JavadocProviderTypes {
JAVA_PARSER,
// JAVA_PARSER, //Used to be based on githb java parser. If need something back that can extract docs from source code, we have to implement
// based on JDT parser. But at the moment this wasn't being used so just got removed.
HTML
}
@@ -58,10 +59,12 @@ public abstract class JandexClasspath implements IClasspath {
}
return new JandexIndex(classpathEntries.map(p -> p.toFile()).collect(Collectors.toList()), jarFile -> findIndexFile(jarFile), classpathResource -> {
switch (providerType) {
case JAVA_PARSER:
return createParserJavadocProvider(classpathResource);
default:
// case JAVA_PARSER:
// return createParserJavadocProvider(classpathResource);
case HTML:
return createHtmlJavdocProvider(classpathResource);
default:
throw new IllegalStateException("Missing switch case?");
}
}, getBaseIndices());
}
@@ -108,8 +111,6 @@ public abstract class JandexClasspath implements IClasspath {
this.javaIndex = Suppliers.synchronizedSupplier(Suppliers.memoize(() -> createIndex()));
}
abstract protected IJavadocProvider createParserJavadocProvider(File classpathResource);
abstract protected IJavadocProvider createHtmlJavdocProvider(File classpathResource);
}

View File

@@ -1,51 +0,0 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.java.parser;
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.ast.CompilationUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
public interface CompilationUnitIndex {
static final CompilationUnitIndex DEFAULT = new CompilationUnitIndex() {
private LoadingCache<URL, CompilationUnit> cache = CacheBuilder.newBuilder().build(new CacheLoader<URL, CompilationUnit>() {
@Override
public CompilationUnit load(URL url) throws Exception {
return JavaParser.parse(url.openStream());
}
});
@Override
public CompilationUnit getCompilationUnit(URL url) {
try {
return cache.get(url);
} catch (ExecutionException e) {
Log.log(e);
}
return null;
}
};
CompilationUnit getCompilationUnit(URL url);
}

View File

@@ -1,189 +0,0 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.java.parser;
import java.net.URL;
import java.util.Optional;
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.RawJavadoc;
import org.springframework.ide.vscode.commons.javadoc.SourceUrlProvider;
import org.springframework.ide.vscode.commons.util.Log;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.EnumConstantDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.visitor.GenericVisitorAdapter;
public class ParserJavadocProvider implements IJavadocProvider {
private SourceUrlProvider sourceUrlProvider;
public ParserJavadocProvider(SourceUrlProvider sourceUrlProvider) {
this.sourceUrlProvider = sourceUrlProvider;
}
public IJavadoc getJavadoc(IType type) {
if (type.isEnum()) {
EnumDeclaration declaration = getEnumDeclaration(type);
return declaration.getJavadoc() == null ? null : new RawJavadoc(declaration.getJavadocComment().get().toString());
} else {
ClassOrInterfaceDeclaration declaration = getClassOrInterfaceDeclaration(type);
return declaration.getJavadoc() == null ? null : new RawJavadoc(declaration.getJavadocComment().get().toString());
}
}
public IJavadoc getJavadoc(IField field) {
IType declaringType = field.getDeclaringType();
if (declaringType.isEnum()) {
EnumConstantDeclaration declaration = createVisitorToFindEnumConstant(field).visit(getEnumDeclaration(declaringType), null);
return declaration.getJavadoc() == null ? null : new RawJavadoc(declaration.getJavadocComment().get().toString());
} else {
FieldDeclaration declaration = createVisitorToFindField(field).visit(getClassOrInterfaceDeclaration(declaringType), null);
return declaration.getJavadoc() == null ? null : new RawJavadoc(declaration.getJavadocComment().get().toString());
}
}
public IJavadoc getJavadoc(IMethod method) {
if (method.parameters().findFirst().isPresent()) {
throw new UnsupportedOperationException("Only methods with no parameters are supported");
}
IType declaringType = method.getDeclaringType();
if (declaringType.isEnum()) {
MethodDeclaration declaration = createVisitorToFindMethod(method).visit(getEnumDeclaration(declaringType), null);
return declaration.getJavadoc() == null ? null : new RawJavadoc(declaration.getJavadocComment().get().toString());
} else {
MethodDeclaration declaration = createVisitorToFindMethod(method).visit(getClassOrInterfaceDeclaration(declaringType), null);
return declaration.getJavadoc() == null ? null : new RawJavadoc(declaration.getJavadocComment().get().toString());
}
}
public IJavadoc getJavadoc(IAnnotation annotation) {
throw new UnsupportedOperationException("Not yet implemented");
}
private CompilationUnit getCompilationUnit(IType type) {
try {
URL sourceUrl = sourceUrlProvider.sourceUrl(type);
return CompilationUnitIndex.DEFAULT.getCompilationUnit(sourceUrl);
} catch (Exception e) {
Log.log("Invalid source URL for type " + type, e);
return null;
}
}
private EnumDeclaration getEnumDeclaration(IType type) {
IType parent = type.getDeclaringType();
if (parent == null) {
CompilationUnit cu = getCompilationUnit(type);
return createVisitorToFindEnum(type).visit(cu, null);
} else {
if (parent.isEnum()) {
EnumDeclaration declaration = getEnumDeclaration(parent);
return createVisitorToFindEnum(type).visit(declaration, null);
} else {
ClassOrInterfaceDeclaration declaration = getClassOrInterfaceDeclaration(parent);
return createVisitorToFindEnum(type).visit(declaration, null);
}
}
}
private ClassOrInterfaceDeclaration getClassOrInterfaceDeclaration(IType type) {
IType parent = type.getDeclaringType();
if (parent == null) {
CompilationUnit cu = getCompilationUnit(type);
return createVisitorToFindClassOrInterface(type).visit(cu, null);
} else {
if (parent.isEnum()) {
EnumDeclaration declaration = getEnumDeclaration(parent);
return createVisitorToFindClassOrInterface(type).visit(declaration, null);
} else {
ClassOrInterfaceDeclaration declaration = getClassOrInterfaceDeclaration(parent);
return createVisitorToFindClassOrInterface(type).visit(declaration, null);
}
}
}
private GenericVisitorAdapter<ClassOrInterfaceDeclaration, Object> createVisitorToFindClassOrInterface(IType type) {
return new GenericVisitorAdapter<ClassOrInterfaceDeclaration, Object>() {
@Override
public ClassOrInterfaceDeclaration visit(ClassOrInterfaceDeclaration n, Object arg) {
if (n.getName().toString().equals(type.getElementName())) {
return n;
} else {
return super.visit(n, arg);
}
}
};
}
private GenericVisitorAdapter<EnumDeclaration, Object> createVisitorToFindEnum(IType type) {
return new GenericVisitorAdapter<EnumDeclaration, Object>() {
@Override
public EnumDeclaration visit(EnumDeclaration n, Object arg) {
if (n.getName().toString().equals(type.getElementName())) {
return n;
} else {
return super.visit(n, arg);
}
}
};
}
private GenericVisitorAdapter<MethodDeclaration, Object> createVisitorToFindMethod(IMethod method) {
return new GenericVisitorAdapter<MethodDeclaration, Object>() {
@Override
public MethodDeclaration visit(MethodDeclaration n, Object arg) {
if (n.getParameters().isEmpty() && n.getName().toString().equals(method.getElementName())) {
return n;
}
return null;
}
};
}
private GenericVisitorAdapter<FieldDeclaration, Object> createVisitorToFindField(IField field) {
return new GenericVisitorAdapter<FieldDeclaration, Object>() {
@Override
public FieldDeclaration visit(FieldDeclaration n, Object arg) {
Optional<VariableDeclarator> variable = n.getVariables().stream().filter(v -> v.getName().toString().equals(field.getElementName())).findFirst();
return variable.isPresent() ? n : null;
}
};
}
private GenericVisitorAdapter<EnumConstantDeclaration, Object> createVisitorToFindEnumConstant(IField field) {
return new GenericVisitorAdapter<EnumConstantDeclaration, Object>() {
@Override
public EnumConstantDeclaration visit(EnumConstantDeclaration n, Object arg) {
if (n.getName().toString().equals(field.getElementName())) {
return n;
} else {
return null;
}
}
};
}
}

View File

@@ -31,7 +31,6 @@ import org.springframework.ide.vscode.commons.jandex.JandexClasspath;
import org.springframework.ide.vscode.commons.jandex.JandexIndex;
import org.springframework.ide.vscode.commons.java.ClasspathData;
import org.springframework.ide.vscode.commons.java.IJavadocProvider;
import org.springframework.ide.vscode.commons.java.parser.ParserJavadocProvider;
import org.springframework.ide.vscode.commons.javadoc.HtmlJavadocProvider;
import org.springframework.ide.vscode.commons.javadoc.SourceUrlProviderFromSourceContainer;
import org.springframework.ide.vscode.commons.maven.MavenCore;
@@ -186,39 +185,6 @@ public class MavenProjectClasspath extends JandexClasspath {
// }
@Override
protected IJavadocProvider createParserJavadocProvider(File classpathResource) {
if (cachedData == null) {
return null;
}
if (classpathResource.isDirectory()) {
if (classpathResource.toString().startsWith(cachedData.outputDirectory)) {
return new ParserJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(cachedData.sourceDirectory).toURI().toURL(), type.getFullyQualifiedName());
});
} else if (classpathResource.toString().startsWith(cachedData.testOutputDirectory)) {
return new ParserJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.SOURCE_FOLDER_URL_SUPPLIER
.sourceUrl(new File(cachedData.testSourceDirectory).toURI().toURL(), type.getFullyQualifiedName());
});
} else {
throw new IllegalArgumentException("Cannot find source folder for " + classpathResource);
}
} else {
// Assume it's a JAR file
return new ParserJavadocProvider(type -> sourceContainer(classpathResource).map(url -> {
try {
return SourceUrlProviderFromSourceContainer.JAR_SOURCE_URL_PROVIDER.sourceUrl(url,
type.getFullyQualifiedName());
} catch (Exception e) {
Log.log(e);
return null;
}
}).get());
}
}
@Override
public Optional<URL> sourceContainer(File classpathResource) {
if (cachedData == null) {
@@ -249,7 +215,7 @@ public class MavenProjectClasspath extends JandexClasspath {
.sourceUrl(new File(cachedData.reportingOutputDirectory, "apidocs").toURI().toURL(), type.getFullyQualifiedName());
});
} else if (classpathResource.toString().startsWith(cachedData.testOutputDirectory)) {
return new ParserJavadocProvider(type -> {
return new HtmlJavadocProvider(type -> {
return SourceUrlProviderFromSourceContainer.JAVADOC_FOLDER_URL_SUPPLIER
.sourceUrl(new File(cachedData.reportingOutputDirectory, "apidocs").toURI().toURL(), type.getFullyQualifiedName());
});

View File

@@ -1,140 +0,0 @@
/*******************************************************************************
* Copyright (c) 2016-2017 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.maven;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
import org.junit.Test;
import org.springframework.ide.vscode.commons.jandex.JandexClasspath;
import org.springframework.ide.vscode.commons.jandex.JandexClasspath.JavadocProviderTypes;
import org.springframework.ide.vscode.commons.java.IField;
import org.springframework.ide.vscode.commons.java.IMethod;
import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.maven.java.MavenJavaProject;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
public class SourceJavadocTest {
private static Supplier<MavenJavaProject> projectSupplier = Suppliers.memoize(() -> {
Path testProjectPath;
try {
JandexClasspath.providerType = JavadocProviderTypes.JAVA_PARSER;
testProjectPath = Paths.get(SourceJavadocTest.class.getResource("/gs-rest-service-cors-boot-1.4.1-with-classpath-file").toURI());
MavenBuilder.newBuilder(testProjectPath).clean().pack().skipTests().execute();
return new MavenJavaProject(MavenCore.getDefault(), testProjectPath.resolve(MavenCore.POM_XML).toFile());
} catch (Exception e) {
return null;
}
});
@Test
public void parser_testClassJavadocForJar() throws Exception {
MavenJavaProject project = projectSupplier.get();
IType type = project.getClasspath().findType("org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener");
assertNotNull(type);
String expected = String.join("\n",
"/**",
" * {@link ApplicationListener} that replaces the liquibase {@link ServiceLocator} with a"
);
assertEquals(expected, type.getJavaDoc().raw().trim().replace("\r", "").substring(0, expected.length()));
type = project.getClasspath().findType("org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener$LiquibasePresent");
assertNotNull(type);
expected = String.join("\n",
"/**",
" * Inner class to prevent class not found issues.",
" */"
);
assertEquals(expected, type.getJavaDoc().raw().trim().replace("\r", ""));
}
@Test
public void parser_testClassJavadocForOutputFolder() throws Exception {
MavenJavaProject project = projectSupplier.get();
IType type = project.getClasspath().findType("hello.Greeting");
assertNotNull(type);
String expected = String.join("\n",
"/**",
" * Comment for Greeting class ",
" */"
);
assertEquals(expected, type.getJavaDoc().raw().trim().replace("\r", ""));
IField field = type.getField("id");
assertNotNull(field);
expected = String.join("\n",
"/**",
" * Comment for id field",
" */"
);
assertEquals(expected, field.getJavaDoc().raw().trim().replace("\r", ""));
IMethod method = type.getMethod("getId", Stream.empty());
assertNotNull(method);
expected = String.join("\n",
"/**",
" * Comment for getId()",
" */"
);
assertEquals(expected, method.getJavaDoc().raw().trim().replace("\r", ""));
}
@Test
public void parser_testFieldAndMethodJavadocForJar() throws Exception {
MavenJavaProject project = projectSupplier.get();
IType type = project.getClasspath().findType("org.springframework.boot.SpringApplication");
assertNotNull(type);
IField field = type.getField("BANNER_LOCATION_PROPERTY_VALUE");
assertNotNull(field);
String expected = String.join("\n",
"/**",
" * Default banner location.",
" */"
);
assertEquals(expected, field.getJavaDoc().raw().trim().replace("\r", ""));
IMethod method = type.getMethod("getListeners", Stream.empty());
assertNotNull(method);
expected = String.join("\n",
"/**",
" * Returns read-only ordered Set of the {@link ApplicationListener}s that will be"
);
assertEquals(expected, method.getJavaDoc().raw().trim().replace("\r", "").substring(0, expected.length()));
}
@Test
public void parser_testInnerClassJavadocForOutputFolder() throws Exception {
MavenJavaProject project = projectSupplier.get();
IType type = project.getClasspath().findType("hello.Greeting$TestInnerClass");
assertNotNull(type);
assertEquals("/**\n * Comment for inner class\n */", type.getJavaDoc().raw().trim().replace("\r", ""));
IField field = type.getField("innerField");
assertNotNull(field);
assertEquals("/**\n \t * Comment for inner field\n \t */", field.getJavaDoc().raw().trim().replace("\r", ""));
IMethod method = type.getMethod("getInnerField", Stream.empty());
assertNotNull(method);
assertEquals("/**\n \t * Comment for method inside nested class\n \t */", method.getJavaDoc().raw().trim().replace("\r", ""));
}
}