Skip to content

Commit

Permalink
Upgrade to JavaParser 3.6.3 #769
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Apr 30, 2018
1 parent c4359ab commit 2e6c233
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 143 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>2.3.0</version>
<version>3.6.3</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Date;
import java.util.List;
import java.util.Map;

import io.lettuce.core.KillArgs;
import io.lettuce.core.protocol.CommandType;
Expand Down
123 changes: 83 additions & 40 deletions src/test/java/io/lettuce/apigenerator/CompilationUnitFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,25 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import com.github.javaparser.ASTHelper;
import org.springframework.util.StringUtils;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.TypeParameter;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.ModifierSet;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

/**
Expand All @@ -53,7 +51,7 @@ public class CompilationUnitFactory {
private String targetName;

private Function<String, String> typeDocFunction;
private Function<MethodDeclaration, Type> methodReturnTypeFunction;
private Map<Predicate<MethodDeclaration>, Function<MethodDeclaration, Type>> methodReturnTypeMutation;
private Predicate<MethodDeclaration> methodFilter;
private Supplier<List<String>> importSupplier;
private Consumer<ClassOrInterfaceDeclaration> typeMutator;
Expand All @@ -73,47 +71,52 @@ public CompilationUnitFactory(File templateFile, File sources, String targetPack
this.targetPackage = targetPackage;
this.targetName = targetName;
this.typeDocFunction = typeDocFunction;
this.methodReturnTypeFunction = methodReturnTypeFunction;
this.methodFilter = methodFilter;
this.importSupplier = importSupplier;
this.typeMutator = typeMutator;
this.methodCommentMutator = methodCommentMutator;
this.methodReturnTypeMutation = new LinkedHashMap<>();

this.methodReturnTypeMutation.put(it -> true, methodReturnTypeFunction);

this.target = new File(sources, targetPackage.replace('.', '/') + "/" + targetName + ".java");
}

public void createInterface() throws Exception {

result.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(targetPackage)));
result.setPackageDeclaration(new PackageDeclaration(new Name(targetPackage)));

template = JavaParser.parse(templateFile);

ClassOrInterfaceDeclaration templateTypeDeclaration = (ClassOrInterfaceDeclaration) template.getTypes().get(0);
resultType = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, true, targetName);
if (templateTypeDeclaration.getExtends() != null) {
resultType.setExtends(templateTypeDeclaration.getExtends());
resultType = new ClassOrInterfaceDeclaration(EnumSet.of(Modifier.PUBLIC), true, targetName);
if (templateTypeDeclaration.getExtendedTypes() != null) {
resultType.setExtendedTypes(templateTypeDeclaration.getExtendedTypes());
}

if (!templateTypeDeclaration.getTypeParameters().isEmpty()) {
resultType.setTypeParameters(new ArrayList<>());
resultType.setTypeParameters(new NodeList<>());
for (TypeParameter typeParameter : templateTypeDeclaration.getTypeParameters()) {
resultType.getTypeParameters().add(new TypeParameter(typeParameter.getName(), typeParameter.getTypeBound()));
resultType.getTypeParameters().add(
new TypeParameter(typeParameter.getName().getIdentifier(), typeParameter.getTypeBound()));
}
}

resultType.setComment(new JavadocComment(typeDocFunction.apply(templateTypeDeclaration.getComment().getContent())));
result.setComment(template.getComment());
resultType.setComment(new JavadocComment(typeDocFunction.apply(templateTypeDeclaration.getComment().orElse(null)
.getContent())));
result.setComment(template.getComment().orElse(null));

result.setImports(new ArrayList<>());
ASTHelper.addTypeDeclaration(result, resultType);
result.setImports(new NodeList<>());

result.addType(resultType);
resultType.setParentNode(result);

if (template.getImports() != null) {
result.getImports().addAll(template.getImports());
}
List<String> importLines = importSupplier.get();
for (String importLine : importLines) {
result.getImports().add(new ImportDeclaration(new NameExpr(importLine), false, false));
result.getImports().add(new ImportDeclaration(importLine, false, false));
}

new MethodVisitor().visit(template, null);
Expand All @@ -126,49 +129,89 @@ public void createInterface() throws Exception {

}

public void keepMethodSignaturesFor(Set<String> methodSignaturesToKeep) {

this.methodReturnTypeMutation.put(methodDeclaration -> contains(methodSignaturesToKeep, methodDeclaration),
MethodDeclaration::getType);
}

protected void writeResult() throws IOException {

FileOutputStream fos = new FileOutputStream(target);
fos.write(result.toString().getBytes());
fos.close();
}

public static Type createParametrizedType(String baseType, String... typeArguments) {

NodeList<Type> args = new NodeList<>();

Arrays.stream(typeArguments).map(it -> {

if (it.contains("[]")) {
return it;
}

return StringUtils.capitalize(it);
}).map(it -> new ClassOrInterfaceType(null, it)).forEach(args::add);

return new ClassOrInterfaceType(null, new SimpleName(baseType), args);
}

public static boolean contains(Collection<String> haystack, MethodDeclaration needle) {

ClassOrInterfaceDeclaration declaringClass = (ClassOrInterfaceDeclaration) needle.getParentNode().get();

return haystack.contains(needle.getNameAsString())
|| haystack.contains(declaringClass.getNameAsString() + "." + needle.getNameAsString());
}

/**
* Simple visitor implementation for visiting MethodDeclaration nodes.
*/
private class MethodVisitor extends VoidVisitorAdapter<Object> {

@Override
public void visit(MethodDeclaration n, Object arg) {
public void visit(MethodDeclaration parsedDeclaration, Object arg) {

if (!methodFilter.test(n)) {
if (!methodFilter.test(parsedDeclaration)) {
return;
}

MethodDeclaration method = new MethodDeclaration(n.getModifiers(), methodReturnTypeFunction.apply(n), n.getName());
if (parsedDeclaration.getNameAsString().equals("close")) {
System.out.println();
}
Type returnType = getMethodReturnType(parsedDeclaration);

MethodDeclaration method = new MethodDeclaration(parsedDeclaration.getModifiers(),
parsedDeclaration.getAnnotations(), parsedDeclaration.getTypeParameters(), returnType,
parsedDeclaration.getName(), parsedDeclaration.getParameters(), parsedDeclaration.getThrownExceptions(),
null);

if (methodCommentMutator != null) {
method.setComment(methodCommentMutator.apply(n.getComment()));
method.setComment(methodCommentMutator.apply(parsedDeclaration.getComment().orElse(null)));
} else {
method.setComment(n.getComment());
method.setComment(parsedDeclaration.getComment().orElse(null));
}

for (Parameter parameter : n.getParameters()) {
Parameter param = ASTHelper.createParameter(parameter.getType(), parameter.getId().getName());
param.setVarArgs(parameter.isVarArgs());
resultType.addMember(method);
}

ASTHelper.addParameter(method, param);
}
private Type getMethodReturnType(MethodDeclaration parsedDeclaration) {

if (n.getTypeParameters() != null) {
method.setTypeParameters(new ArrayList<>());
method.getTypeParameters().addAll(n.getTypeParameters());
}
List<Map.Entry<Predicate<MethodDeclaration>, Function<MethodDeclaration, Type>>> entries = new ArrayList<>(
methodReturnTypeMutation.entrySet());

Collections.reverse(entries);

for (Map.Entry<Predicate<MethodDeclaration>, Function<MethodDeclaration, Type>> entry : entries) {

if (n.getAnnotations() != null) {
method.setAnnotations(n.getAnnotations());
if (entry.getKey().test(parsedDeclaration)) {
return entry.getValue().apply(parsedDeclaration);
}
}

ASTHelper.addMember(resultType, method);
return null;
}
}
}
25 changes: 6 additions & 19 deletions src/test/java/io/lettuce/apigenerator/CreateAsyncApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.Type;

import io.lettuce.core.internal.LettuceSets;
Expand All @@ -43,8 +40,9 @@
@RunWith(Parameterized.class)
public class CreateAsyncApi {

private Set<String> KEEP_METHOD_RESULT_TYPE = LettuceSets.unmodifiableSet("shutdown", "debugOom", "debugSegfault", "digest",
"close", "isOpen", "BaseRedisCommands.reset", "getStatefulConnection", "setAutoFlushCommands", "flushCommands");
private Set<String> KEEP_METHOD_RESULT_TYPE = LettuceSets.unmodifiableSet("shutdown", "debugOom", "debugSegfault",
"digest", "close", "isOpen", "BaseRedisCommands.reset", "getStatefulConnection", "setAutoFlushCommands",
"flushCommands");

private CompilationUnitFactory factory;

Expand Down Expand Up @@ -77,6 +75,8 @@ public CreateAsyncApi(String templateName) {

factory = new CompilationUnitFactory(templateFile, Constants.SOURCES, targetPackage, targetName, commentMutator(),
methodTypeMutator(), methodDeclaration -> true, importSupplier(), null, null);

factory.keepMethodSignaturesFor(KEEP_METHOD_RESULT_TYPE);
}

/**
Expand All @@ -95,20 +95,7 @@ protected Function<String, String> commentMutator() {
* @return
*/
protected Function<MethodDeclaration, Type> methodTypeMutator() {
return method -> {
ClassOrInterfaceDeclaration classOfMethod = (ClassOrInterfaceDeclaration) method.getParentNode();
if (KEEP_METHOD_RESULT_TYPE.contains(method.getName())
|| KEEP_METHOD_RESULT_TYPE.contains(classOfMethod.getName() + "." + method.getName())) {
return method.getType();
}

String typeAsString = method.getType().toStringWithoutComments().trim();
if (typeAsString.equals("void")) {
typeAsString = "Void";
}

return new ReferenceType(new ClassOrInterfaceType("RedisFuture<" + typeAsString + ">"));
};
return method -> CompilationUnitFactory.createParametrizedType("RedisFuture", method.getType().toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.Type;

import io.lettuce.core.internal.LettuceSets;
Expand Down Expand Up @@ -74,6 +71,7 @@ public CreateAsyncNodeSelectionClusterApi(String templateName) {

factory = new CompilationUnitFactory(templateFile, Constants.SOURCES, targetPackage, targetName, commentMutator(),
methodTypeMutator(), methodFilter(), importSupplier(), null, null);
factory.keepMethodSignaturesFor(FILTER_METHODS);
}

/**
Expand All @@ -92,15 +90,7 @@ protected Function<String, String> commentMutator() {
* @return
*/
protected Predicate<MethodDeclaration> methodFilter() {
return method -> {
ClassOrInterfaceDeclaration classOfMethod = (ClassOrInterfaceDeclaration) method.getParentNode();
if (FILTER_METHODS.contains(method.getName())
|| FILTER_METHODS.contains(classOfMethod.getName() + "." + method.getName())) {
return false;
}

return true;
};
return method -> !CompilationUnitFactory.contains(FILTER_METHODS, method);
}

/**
Expand All @@ -110,18 +100,7 @@ protected Predicate<MethodDeclaration> methodFilter() {
*/
protected Function<MethodDeclaration, Type> methodTypeMutator() {
return method -> {
ClassOrInterfaceDeclaration classOfMethod = (ClassOrInterfaceDeclaration) method.getParentNode();
if (FILTER_METHODS.contains(method.getName())
|| FILTER_METHODS.contains(classOfMethod.getName() + "." + method.getName())) {
return method.getType();
}

String typeAsString = method.getType().toStringWithoutComments().trim();
if (typeAsString.equals("void")) {
typeAsString = "Void";
}

return new ReferenceType(new ClassOrInterfaceType("AsyncExecutions<" + typeAsString + ">"));
return CompilationUnitFactory.createParametrizedType("AsyncExecutions", method.getType().toString());
};
}

Expand Down
Loading

0 comments on commit 2e6c233

Please sign in to comment.