Skip to content

Commit

Permalink
split class
Browse files Browse the repository at this point in the history
  • Loading branch information
boretti committed Apr 22, 2018
1 parent 9e13445 commit f8ec9d0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

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

default RoundMirror getRoundMirror() {
return getFullData().getRoundMirror();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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 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.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 ProvidesMatchersAnnotatedElementFieldMatcherMirror
extends ProvidesMatchersAnnotatedElementGeneralMirror {

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<AbstractFieldDescription> fields;

private final String fieldsMatcher;

private List<AbstractFieldDescription> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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 * <p>\n * <i>This method, when used more than once, just add more matcher to the list.</i>\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<AbstractFieldDescription> fields;

private List<AbstractFieldDescription> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
*/
public class ProvidesMatchersSubElementVisitor extends
SimpleElementVisitor8<Optional<AbstractFieldDescription>, ProvidesMatchersAnnotatedElementMatcherMirror> {
SimpleElementVisitor8<Optional<AbstractFieldDescription>, ProvidesMatchersAnnotatedElementFieldMatcherMirror> {

private final Function<Element, Boolean> removeFromIgnoreList;
private final NameExtractorVisitor extractNameVisitor;
Expand All @@ -59,7 +59,7 @@ public Optional<AbstractFieldDescription> removeIfNeededAndThenReturn(

@Override
public Optional<AbstractFieldDescription> 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);
Expand All @@ -70,7 +70,7 @@ public Optional<AbstractFieldDescription> visitVariable(VariableElement e,

@Override
public Optional<AbstractFieldDescription> 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();
Expand All @@ -91,15 +91,15 @@ private void generateIfNeededWarningForNotSupportedElementAndRemoveIt(String des
}

private Optional<AbstractFieldDescription> 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);
return createFieldDescriptionIfApplicableAndRemoveElementFromListWhenApplicable(e, p, fieldName);
}

public Optional<AbstractFieldDescription> 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,
Expand All @@ -108,7 +108,7 @@ public Optional<AbstractFieldDescription> createFieldDescriptionIfApplicableAndR

@Override
protected Optional<AbstractFieldDescription> defaultAction(Element e,
ProvidesMatchersAnnotatedElementMatcherMirror p) {
ProvidesMatchersAnnotatedElementFieldMatcherMirror p) {
return Optional.empty();
}

Expand Down

0 comments on commit f8ec9d0

Please sign in to comment.