generateFields(TypeElement typeElement,
+ ProvidesMatchersSubElementVisitor providesMatchersSubElementVisitor) {
+ return typeElement.getEnclosedElements().stream()
+ .map(ie -> ie.accept(providesMatchersSubElementVisitor, this)).filter(
+ Optional::isPresent)
+ .map(Optional::get).collect(
+ collectingAndThen(
+ groupingBy(FieldDescriptionMetaData::getFieldName,
+ reducing(null,
+ (v1, v2) -> v1 == null ? v2
+ : v1 instanceof IgoreFieldDescription ? v1 : v2)),
+ c -> c == null ? emptyList() : c.values().stream().collect(toList())));
+ }
+
+ public ProvidesMatchersAnnotatedElementFieldMatcherMirror(TypeElement typeElement, RoundMirror roundMirror) {
+ super(typeElement, roundMirror);
+ this.fields = generateFields(typeElement, new ProvidesMatchersSubElementVisitor(roundMirror));
+ this.fieldsMatcher = fields.stream().map(FieldDescriptionMetaData::getMatcherForField)
+ .map(f -> addPrefix(" ", f)).collect(joining("\n")) + "\n";
+ }
+
+ public String generateMatchers() {
+ return new StringBuilder(DEFAULT_FEATUREMATCHER_FORCONVERTER).append(fieldsMatcher)
+ .append(fullyQualifiedNameOfSuperClassOfClassAnnotatedWithProvideMatcher
+ .map(this::generateParentMatcher).orElse(""))
+ .toString();
+ }
+
+ public String generateParentMatcher(String parent) {
+ return String.format(
+ " private static class SuperClassMatcher%1$s extends org.hamcrest.FeatureMatcher<%2$s,%3$s> {\n\n public SuperClassMatcher(org.hamcrest.Matcher super %3$s> matcher) {\n super(matcher,\"parent\",\"parent\");\n }\n\n\n protected %3$s featureValueOf(%2$s actual) {\n return actual;\n }\n\n }\n\n\n",
+ fullGeneric, fullyQualifiedNameOfClassAnnotatedWithProvideMatcher, parent);
+ }
+
+}
diff --git a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMatcherMirror.java b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMatcherMirror.java
index 47703b67f..c10ef488d 100644
--- a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMatcherMirror.java
+++ b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMatcherMirror.java
@@ -20,25 +20,17 @@
package ch.powerunit.extensions.matchers.provideprocessor;
import static ch.powerunit.extensions.matchers.common.CommonUtils.addPrefix;
-import static java.util.Collections.emptyList;
-import static java.util.stream.Collectors.collectingAndThen;
-import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
-import static java.util.stream.Collectors.reducing;
-import static java.util.stream.Collectors.toList;
import java.util.Arrays;
-import java.util.List;
import java.util.Optional;
import javax.lang.model.element.TypeElement;
import ch.powerunit.extensions.matchers.provideprocessor.fields.AbstractFieldDescription;
-import ch.powerunit.extensions.matchers.provideprocessor.fields.FieldDescriptionMetaData;
-import ch.powerunit.extensions.matchers.provideprocessor.fields.IgoreFieldDescription;
public abstract class ProvidesMatchersAnnotatedElementMatcherMirror
- extends ProvidesMatchersAnnotatedElementGeneralMirror {
+ extends ProvidesMatchersAnnotatedElementFieldMatcherMirror {
private static final String PRIVATE_IMPLEMENTATION_END = "\n\n @Override\n public _PARENT end() {\n return _parentBuilder;\n }\n\n\n";
@@ -54,47 +46,8 @@ public abstract class ProvidesMatchersAnnotatedElementMatcherMirror
private static final String JAVADOC_ANDWITH = " /**\n * Add a matcher on the object itself and not on a specific field.\n * \n * This method, when used more than once, just add more matcher to the list.\n * @param otherMatcher the matcher on the object itself.\n * @return the DSL to continue\n */\n";
- private static final String DEFAULT_FEATUREMATCHER_FORCONVERTER = "\n private static <_TARGET,_SOURCE> org.hamcrest.Matcher<_SOURCE> asFeatureMatcher(String msg,java.util.function.Function<_SOURCE,_TARGET> converter,org.hamcrest.Matcher super _TARGET> matcher) {\n return new org.hamcrest.FeatureMatcher<_SOURCE,_TARGET>(matcher, msg, msg) {\n protected _TARGET featureValueOf(_SOURCE actual) {\n return converter.apply(actual);\n }};\n }\n\n";
-
- protected final List fields;
-
- private List generateFields(TypeElement typeElement,
- ProvidesMatchersSubElementVisitor providesMatchersSubElementVisitor) {
- return typeElement.getEnclosedElements().stream()
- .map(ie -> ie.accept(providesMatchersSubElementVisitor, this)).filter(
- Optional::isPresent)
- .map(Optional::get).collect(
- collectingAndThen(
- groupingBy(FieldDescriptionMetaData::getFieldName,
- reducing(null,
- (v1, v2) -> v1 == null ? v2
- : v1 instanceof IgoreFieldDescription ? v1 : v2)),
- c -> c == null ? emptyList() : c.values().stream().collect(toList())));
- }
-
public ProvidesMatchersAnnotatedElementMatcherMirror(TypeElement typeElement, RoundMirror roundMirror) {
super(typeElement, roundMirror);
- this.fields = generateFields(typeElement, new ProvidesMatchersSubElementVisitor(roundMirror));
- }
-
- public String generateMatchers() {
- StringBuilder sb = new StringBuilder();
- sb.append(DEFAULT_FEATUREMATCHER_FORCONVERTER);
- sb.append(generateFieldsMatcher());
- sb.append(fullyQualifiedNameOfSuperClassOfClassAnnotatedWithProvideMatcher.map(this::generateParentMatcher)
- .orElse(""));
- return sb.toString();
- }
-
- public String generateFieldsMatcher() {
- return fields.stream().map(FieldDescriptionMetaData::getMatcherForField).map(f -> addPrefix(" ", f))
- .collect(joining("\n")) + "\n";
- }
-
- public String generateParentMatcher(String parent) {
- return String.format(
- " private static class SuperClassMatcher%1$s extends org.hamcrest.FeatureMatcher<%2$s,%3$s> {\n\n public SuperClassMatcher(org.hamcrest.Matcher super %3$s> matcher) {\n super(matcher,\"parent\",\"parent\");\n }\n\n\n protected %3$s featureValueOf(%2$s actual) {\n return actual;\n }\n\n }\n\n\n",
- fullGeneric, fullyQualifiedNameOfClassAnnotatedWithProvideMatcher, parent);
}
public String generatePublicInterface() {
diff --git a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersSubElementVisitor.java b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersSubElementVisitor.java
index 2f9fdfa7a..2a03ae0e5 100644
--- a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersSubElementVisitor.java
+++ b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersSubElementVisitor.java
@@ -39,7 +39,7 @@
*
*/
public class ProvidesMatchersSubElementVisitor extends
- SimpleElementVisitor8, ProvidesMatchersAnnotatedElementMatcherMirror> {
+ SimpleElementVisitor8, ProvidesMatchersAnnotatedElementFieldMatcherMirror> {
private final Function removeFromIgnoreList;
private final NameExtractorVisitor extractNameVisitor;
@@ -59,7 +59,7 @@ public Optional removeIfNeededAndThenReturn(
@Override
public Optional visitVariable(VariableElement e,
- ProvidesMatchersAnnotatedElementMatcherMirror p) {
+ ProvidesMatchersAnnotatedElementFieldMatcherMirror p) {
if (e.getModifiers().contains(Modifier.PUBLIC) && !e.getModifiers().contains(Modifier.STATIC)) {
String fieldName = e.getSimpleName().toString();
return createFieldDescriptionIfApplicableAndRemoveElementFromListWhenApplicable(e, p, fieldName);
@@ -70,7 +70,7 @@ public Optional visitVariable(VariableElement e,
@Override
public Optional visitExecutable(ExecutableElement e,
- ProvidesMatchersAnnotatedElementMatcherMirror p) {
+ ProvidesMatchersAnnotatedElementFieldMatcherMirror p) {
if (e.getModifiers().contains(Modifier.PUBLIC) && e.getParameters().size() == 0
&& !e.getModifiers().contains(Modifier.STATIC)) {
String simpleName = e.getSimpleName().toString();
@@ -91,7 +91,7 @@ private void generateIfNeededWarningForNotSupportedElementAndRemoveIt(String des
}
private Optional visiteExecutableGet(ExecutableElement e, String prefix,
- ProvidesMatchersAnnotatedElementMatcherMirror p) {
+ ProvidesMatchersAnnotatedElementFieldMatcherMirror p) {
String methodName = e.getSimpleName().toString();
String fieldNameDirect = methodName.replaceFirst(prefix, "");
String fieldName = fieldNameDirect.substring(0, 1).toLowerCase() + fieldNameDirect.substring(1);
@@ -99,7 +99,7 @@ private Optional visiteExecutableGet(ExecutableElement
}
public Optional createFieldDescriptionIfApplicableAndRemoveElementFromListWhenApplicable(
- Element e, ProvidesMatchersAnnotatedElementMatcherMirror p, String fieldName) {
+ Element e, ProvidesMatchersAnnotatedElementFieldMatcherMirror p, String fieldName) {
return removeIfNeededAndThenReturn(
((e instanceof ExecutableElement) ? ((ExecutableElement) e).getReturnType() : e.asType())
.accept(extractNameVisitor, false).map(f -> FieldDescriptionProvider.of(() -> p,
@@ -108,7 +108,7 @@ public Optional createFieldDescriptionIfApplicableAndR
@Override
protected Optional defaultAction(Element e,
- ProvidesMatchersAnnotatedElementMatcherMirror p) {
+ ProvidesMatchersAnnotatedElementFieldMatcherMirror p) {
return Optional.empty();
}