Skip to content

Commit

Permalink
Refactor code to follow codebeat pattern (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
boretti committed Apr 22, 2018
1 parent fc819c4 commit 9e13445
Show file tree
Hide file tree
Showing 15 changed files with 481 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public String getSeeValue() {
return result;
}

public String getParam() {
String param = element.getParameters().stream()
.map(ve -> ve.asType().toString() + " " + ve.getSimpleName().toString()).collect(joining(","));
return element.isVarArgs() ? param.replaceAll(VAR_ARG_REGEX, "...") : param;
}

private String convertParameterForSee(VariableElement ve) {
Element e = processingEnv.getTypeUtils().asElement(ve.asType());
if (e == null || ve.asType().getKind() == TypeKind.TYPEVAR) {
Expand All @@ -80,35 +86,40 @@ private String convertParameterForSee(VariableElement ve) {
+ processingEnv.getTypeUtils().asElement(ve.asType()).getSimpleName();
}

public String generateFactory() {
StringBuilder sb = new StringBuilder();
sb.append(" /**\n * " + doc.map(t -> t.replaceAll("\n", "\n * ").replaceAll(" * $", "\n"))
private String getJavadoc() {
return new StringBuilder(" /**\n * " + doc.map(t -> t.replaceAll("\n", "\n * ").replaceAll(" * $", "\n"))
.orElse("No javadoc found from the source method.")).append("\n * @see " + getSeeValue() + "\n */")
.append("\n");
sb.append(" default ");
.append("\n").toString();
}

private String getGeneric() {
if (!element.getTypeParameters().isEmpty()) {
sb.append("<")
return new StringBuilder("<")
.append(element.getTypeParameters().stream()
.map((ve) -> ve.getSimpleName().toString() + (ve.getBounds().isEmpty() ? ""
: (" extends "
+ ve.getBounds().stream().map(Object::toString).collect(joining("&")))))
.map(ve -> ve.getSimpleName().toString()
+ (ve.getBounds().isEmpty() ? ""
: (" extends " + ve.getBounds().stream().map(Object::toString)
.collect(joining("&")))))
.collect(joining(",")))
.append("> ");
.append("> ").toString();
}
sb.append(element.getReturnType().toString()).append(" ").append(element.getSimpleName().toString())
.append("(");
String param = element.getParameters().stream()
.map((ve) -> ve.asType().toString() + " " + ve.getSimpleName().toString()).collect(joining(","));
sb.append(element.isVarArgs() ? param.replaceAll(VAR_ARG_REGEX, "...") : param);
sb.append(") {").append("\n")
.append(TypeKind.VOID != element.getReturnType().getKind() ? " return " : " ");
sb.append(processingEnv.getElementUtils().getPackageOf(element.getEnclosingElement()).getQualifiedName()
.toString()).append(".")
.append(element.getEnclosingElement().getSimpleName().toString())
.append(".").append(element.getSimpleName().toString()).append("(").append(element.getParameters()
.stream().map((ve) -> ve.getSimpleName().toString()).collect(joining(",")))
.append(");\n }\n\n");
return sb.toString();
return "";
}

private String getDeclaration() {
return String.format("%1$s%2$s %3$s(%4$s)", getGeneric(), element.getReturnType(), element.getSimpleName(),
getParam());
}

public String generateFactory() {
return new StringBuilder(getJavadoc()).append(" default ").append(getDeclaration()).append(" {\n")
.append(TypeKind.VOID != element.getReturnType().getKind() ? " return " : " ")
.append(processingEnv.getElementUtils().getPackageOf(element.getEnclosingElement()).getQualifiedName()
.toString())
.append(".").append(element.getEnclosingElement().getSimpleName().toString()).append(".")
.append(element.getSimpleName().toString()).append("(").append(element.getParameters().stream()
.map((ve) -> ve.getSimpleName().toString()).collect(joining(",")))
.append(");\n }\n\n").toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@FunctionalInterface
public interface ProvidesMatchersAnnotatedElementData {
ProvidesMatchersAnnotatedElementMirror getFullData();
ProvidesMatchersAnnotatedElementMatcherMirror getFullData();

default RoundMirror getRoundMirror() {
return getFullData().getRoundMirror();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Powerunit - A JDK1.8 test framework
* Copyright (C) 2014 Mathieu Boretti.
*
* This file is part of Powerunit
*
* Powerunit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Powerunit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
*/
package ch.powerunit.extensions.matchers.provideprocessor;

import java.util.Optional;

import javax.lang.model.element.TypeElement;

public abstract class ProvidesMatchersAnnotatedElementGeneralMirror
extends ProvidesMatchersAnnotatedElementGenericMirror {

protected final TypeElement typeElementForClassAnnotatedWithProvideMatcher;
protected final String methodShortClassName;
protected final Optional<String> fullyQualifiedNameOfSuperClassOfClassAnnotatedWithProvideMatcher;
protected final RoundMirror roundMirror;

public ProvidesMatchersAnnotatedElementGeneralMirror(TypeElement typeElement, RoundMirror roundMirror) {
super(typeElement, roundMirror);
this.roundMirror = roundMirror;
this.typeElementForClassAnnotatedWithProvideMatcher = typeElement;
this.methodShortClassName = simpleNameOfClassAnnotatedWithProvideMatcher.substring(0, 1).toLowerCase()
+ simpleNameOfClassAnnotatedWithProvideMatcher.substring(1);
if (!roundMirror.getProcessingEnv().getElementUtils().getTypeElement("java.lang.Object").asType()
.equals(typeElement.getSuperclass())) {
this.fullyQualifiedNameOfSuperClassOfClassAnnotatedWithProvideMatcher = Optional
.ofNullable(typeElement.getSuperclass().toString());
} else {
this.fullyQualifiedNameOfSuperClassOfClassAnnotatedWithProvideMatcher = Optional.empty();
}

}

public String getDefaultReturnMethod() {
return simpleNameOfClassAnnotatedWithProvideMatcher + "Matcher" + getGenericParent();
}

public TypeElement getTypeElementForClassAnnotatedWithProvideMatcher() {
return typeElementForClassAnnotatedWithProvideMatcher;
}

public String getMethodShortClassName() {
return methodShortClassName;
}

public String getSimpleNameOfGeneratedImplementationMatcher() {
return simpleNameOfClassAnnotatedWithProvideMatcher + "MatcherImpl";
}

public String getSimpleNameOfGeneratedImplementationMatcherWithGenericNoParent() {
return getSimpleNameOfGeneratedImplementationMatcher() + getGenericNoParent();
}

public String getSimpleNameOfGeneratedImplementationMatcherWithGenericParent() {
return getSimpleNameOfGeneratedImplementationMatcher() + getGenericParent();
}

public RoundMirror getRoundMirror() {
return roundMirror;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public abstract class ProvidesMatchersAnnotatedElementGenericMirror
protected final String generic;
protected final String fullGeneric;
protected final String simpleNameOfGeneratedInterfaceMatcher;
protected final String genericForChaining;

public ProvidesMatchersAnnotatedElementGenericMirror(TypeElement typeElement, RoundMirror roundMirror) {
super(typeElement, roundMirror);
this.generic = parseGeneric(typeElement);
this.fullGeneric = parseFullGeneric(typeElement);
this.simpleNameOfGeneratedInterfaceMatcher = simpleNameOfClassAnnotatedWithProvideMatcher + "Matcher";
this.genericForChaining = getGenericParent().replaceAll("^<_PARENT",
"<" + getFullyQualifiedNameOfGeneratedClass() + "." + simpleNameOfGeneratedInterfaceMatcher
+ getGenericNoParent());
}

private static String parseFullGeneric(TypeElement typeElement) {
Expand Down
Loading

0 comments on commit 9e13445

Please sign in to comment.