Skip to content

Commit

Permalink
quality
Browse files Browse the repository at this point in the history
  • Loading branch information
boretti committed Apr 22, 2018
1 parent 426033b commit acfe729
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Collections;
import java.util.List;

import javax.lang.model.element.TypeElement;

import ch.powerunit.extensions.matchers.provideprocessor.ProvidesMatchersAnnotatedElementData;
import ch.powerunit.extensions.matchers.provideprocessor.fields.lang.BuilderDeclaration;

Expand All @@ -37,48 +39,42 @@ public DefaultFieldDescription(ProvidesMatchersAnnotatedElementData containingEl
protected Collection<FieldDSLMethod> getSpecificFieldDslMethodFor() {
return Collections.emptyList();
}

protected final BuilderDeclaration getDslMethodBuilder() {
return FieldDSLMethodBuilder.of(this);
}

@Override
protected final Collection<FieldDSLMethod> getFieldDslMethodFor() {
String ft = mirror.getFieldType();
String fn = mirror.getFieldName();
List<FieldDSLMethod> tmp = new ArrayList<>();

tmp.add(FieldDSLMethodBuilder.of(this)
.withDeclaration("org.hamcrest.Matcher<? super " + mirror.getFieldType() + "> matcher")
tmp.add(FieldDSLMethodBuilder.of(this).withDeclaration("org.hamcrest.Matcher<? super " + ft + "> matcher")
.withJavaDoc("", "matcher a Matcher on the field", SEE_TEXT_FOR_HAMCREST_MATCHER)
.havingImplementation(mirror.getFieldName() + "= new " + mirror.getMethodFieldName()
+ "Matcher(matcher);\nreturn this;"));

tmp.add(FieldDSLMethodBuilder.of(this).withDeclaration(mirror.getFieldType() + " value")
.havingImplementation(fn + "= new " + mirror.getMethodFieldName() + "Matcher(matcher);\nreturn this;"));
tmp.add(FieldDSLMethodBuilder.of(this).withDeclaration(ft + " value")
.withJavaDoc("", "value an expected value for the field, which will be compared using the is matcher",
SEE_TEXT_FOR_IS_MATCHER)
.havingDefault(MATCHERS + ".is(value)"));

tmp.add(FieldDSLMethodBuilder.of(this)
.withGenericDeclaration("<_TARGETFIELD>", "As",
"java.util.function.Function<" + mirror.getFieldType()
"java.util.function.Function<" + ft
+ ",_TARGETFIELD> converter,org.hamcrest.Matcher<? super _TARGETFIELD> matcher")
.withJavaDoc("by converting the received field before validat it",
"converter a function to convert the field\nmatcher a matcher on the resulting\n<_TARGETFIELD> The type which this field must be converter")
.havingDefault("asFeatureMatcher(\" <field is converted> \",converter,matcher)"));

if (fullyQualifiedNameMatcherInSameRound != null
&& mirror.getFieldTypeAsTypeElement().getTypeParameters().isEmpty()) {
String name = mirror.getFieldTypeAsTypeElement().getSimpleName().toString();
TypeElement te = mirror.getFieldTypeAsTypeElement();
if (fullyQualifiedNameMatcherInSameRound != null && te.getTypeParameters().isEmpty()) {
String name = te.getSimpleName().toString();
String lname = name.substring(0, 1).toLowerCase() + name.substring(1);

tmp.add(FieldDSLMethodBuilder.of(this)
.withExplicitDeclaration(fullyQualifiedNameMatcherInSameRound + "." + name + "Matcher" + "<"
+ defaultReturnMethod + "> " + mirror.getFieldName() + "With()")
+ defaultReturnMethod + "> " + fn + "With()")
.withJavaDoc("by starting a matcher for this field")
.havingImplementation(fullyQualifiedNameMatcherInSameRound + "." + name + "Matcher tmp = "
+ fullyQualifiedNameMatcherInSameRound + "." + lname + "WithParent(this);\n"
+ mirror.getFieldName() + "(tmp);\nreturn tmp;"));
+ fullyQualifiedNameMatcherInSameRound + "." + lname + "WithParent(this);\n" + fn
+ "(tmp);\nreturn tmp;"));
}

tmp.addAll(getSpecificFieldDslMethodFor());
return tmp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

public class FieldDescriptionMirror {

protected final String fieldName;
protected final String fieldType;
protected final TypeElement fieldTypeAsTypeElement;
protected final Element fieldElement;
private final String fieldName;
private final String fieldType;
private final TypeElement fieldTypeAsTypeElement;
private final Element fieldElement;

public FieldDescriptionMirror(ProvidesMatchersAnnotatedElementData containingElementMirror, String fieldName,
String fieldType, Element fieldElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package ch.powerunit.extensions.matchers.provideprocessor.fields;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
Expand Down Expand Up @@ -93,11 +94,12 @@ public Type visitUnknown(TypeMirror t, ProcessingEnvironment processingEnv) {

public static AbstractFieldDescription of(ProvidesMatchersAnnotatedElementData containingElementMirror,
FieldDescriptionMirror mirror) {
Element te = mirror.getFieldElement();
ProcessingEnvironment processingEnv = containingElementMirror.getRoundMirror().getProcessingEnv();
Type type = new ExtracTypeVisitor().visit((mirror.getFieldElement() instanceof ExecutableElement)
? ((ExecutableElement) mirror.getFieldElement()).getReturnType() : mirror.getFieldElement().asType(),
Type type = new ExtracTypeVisitor().visit(
(te instanceof ExecutableElement) ? ((ExecutableElement) te).getReturnType() : te.asType(),
processingEnv);
if (mirror.getFieldElement().getAnnotation(IgnoreInMatcher.class) != null) {
if (te.getAnnotation(IgnoreInMatcher.class) != null) {
return new IgoreFieldDescription(containingElementMirror, mirror);
}

Expand Down

0 comments on commit acfe729

Please sign in to comment.