diff --git a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMirror.java b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMirror.java index 33280241f..d23d70b6c 100644 --- a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMirror.java +++ b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/ProvidesMatchersAnnotatedElementMirror.java @@ -64,13 +64,13 @@ public ProvidesMatchersAnnotatedElementMirror(TypeElement typeElement, RoundMirr Arrays.asList(this::generateDefaultDSLStarter, this::generateDefaultForChainingDSLStarter)); if (hasSuperClass()) { tmp.add(this::generateParentDSLStarter); - tmp.addAll(ProvidesMatchersWithSameValueHelper.generateParentValueDSLStarter(this, true)); + tmp.addAll(ProvidesMatchersWithSameValueHelper.generateParentValueDSLStarter(this)); if (((TypeElement) roundMirror.getTypeUtils().asElement(element.getSuperclass())).getTypeParameters() .isEmpty()) { tmp.add(this::generateParentInSameRoundWithChaningDSLStarter); } } else { - tmp.addAll(ProvidesMatchersWithSameValueHelper.generateParentValueDSLStarter(this, false)); + tmp.addAll(ProvidesMatchersWithSameValueHelper.generateNoParentValueDSLStarter(this)); } tmp.addAll(ofNullable(getDSLExtension()).orElseGet(Collections::emptyList).stream() .map(t -> t.getDSLMethodFor(() -> this)).flatMap(Collection::stream).collect(toList())); diff --git a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/helper/ProvidesMatchersWithSameValueHelper.java b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/helper/ProvidesMatchersWithSameValueHelper.java index 96833ffa4..54c6d3ef4 100644 --- a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/helper/ProvidesMatchersWithSameValueHelper.java +++ b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/helper/ProvidesMatchersWithSameValueHelper.java @@ -2,11 +2,11 @@ import static ch.powerunit.extensions.matchers.common.CommonUtils.addPrefix; import static ch.powerunit.extensions.matchers.provideprocessor.dsl.DSLMethod.of; +import static java.lang.String.format; import static java.util.Arrays.asList; -import static java.util.stream.Collectors.toList; +import static java.util.Arrays.stream; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map.Entry; @@ -48,73 +48,72 @@ private static DSLMethod generateWithSameValueWithParentMatcherIgnore(ProvidesMa List lines = new ArrayList<>(); lines.add("java.util.Set ignored = new java.util.HashSet<>(java.util.Arrays.asList(ignoredFields));"); lines.add(genericNoParent + " m=new " + simpleNameGenericNoParent + "(" + argumentForParentBuilder + ");"); - - lines.addAll( - target.getFields().stream().flatMap(ProvidesMatchersWithSameValueHelper::copyField).collect(toList())); + target.getFields().stream().flatMap(ProvidesMatchersWithSameValueHelper::copyField).forEach(lines::add); lines.add("return m;"); - return of(fullGeneric + " " + fullyQualified + "." + genericNoParent + " " + withSameValueMethodName) + return of(format("%1$s %2$s.%3$s %4$s", fullGeneric, fullyQualified, genericNoParent, withSameValueMethodName)) .withArguments( new String[][] { { simpleNameGenericWithParent, "other" }, { "String...", "ignoredFields" } }) .withImplementation(lines).withJavadoc(javadoc); } private static Stream copyField(AbstractFieldDescription f) { - return Arrays.stream(String.format( + return stream(format( "if(!ignored.contains(\"%1$s\")) {\n String localIgnored[] = ignored.stream().filter(s->s.startsWith(\"%1$s.\")).map(s->s.replaceFirst(\"%1$s\\\\.\",\"\")).toArray(String[]::new);\n%2$s\n}", f.getFieldName(), addPrefix(" ", f.getFieldCopy("m", "other", ",localIgnored"))).split("\n")); } private static DSLMethod generateWithSameValueWithParentMatcherAndNoIgnore( ProvidesMatchersAnnotatedElementMirror target) { - String genericNoParent = target.getSimpleNameOfGeneratedInterfaceMatcherWithGenericNoParent(); - String simpleNameGenericWithParent = target - .getFullyQualifiedNameOfClassAnnotatedWithProvideMatcherWithGeneric(); - String fullyQualified = target.getFullyQualifiedNameOfGeneratedClass(); String withSameValueMethodName = target.getMethodNameDSLWithSameValue(); - String fullGeneric = target.getFullGeneric(); - String javadoc = target.generateDefaultJavaDocWithoutDSLStarter( - Optional.of("other the other object to be used as a reference."), "the DSL matcher", false); - return of(fullGeneric + " " + fullyQualified + "." + genericNoParent + " " + withSameValueMethodName) - .withOneArgument(simpleNameGenericWithParent, "other") - .withImplementation("return " + withSameValueMethodName + "(other,new String[]{});") - .withJavadoc(javadoc); + return of(format("%1$s %2$s.%3$s %4$s", target.getFullGeneric(), target.getFullyQualifiedNameOfGeneratedClass(), + target.getSimpleNameOfGeneratedInterfaceMatcherWithGenericNoParent(), withSameValueMethodName)) + .withOneArgument(target.getFullyQualifiedNameOfClassAnnotatedWithProvideMatcherWithGeneric(), + "other") + .withImplementation(format("return %1$s(other,new String[]{});", withSameValueMethodName)) + .withJavadoc(target.generateDefaultJavaDocWithoutDSLStarter( + Optional.of("other the other object to be used as a reference."), "the DSL matcher", + false)); } - public static boolean isWeakAllowed(ProvidesMatchersAnnotatedElementMirror target) { - if (target.getRealAnnotation().allowWeakWithSameValue()) { - Optional am = target.getAnnotationMirror(); - Optional av = am.map(a -> a.getElementValues().entrySet().stream() - .filter(kv -> kv.getKey().getSimpleName().toString().equals("allowWeakWithSameValue")) - .map(Entry::getValue).findAny().orElse(null)); - target.getMessager().printMessage(Kind.MANDATORY_WARNING, - "This class use the option allowWeakWithSameValue and a weak WithSameValue is detected. The generated WithSameValue DSL may not be able to fully control all the field of this class", - target.getElement(), am.orElse(null), av.orElse(null)); - return true; - } - return false; + private static boolean isWeakAllowed(ProvidesMatchersAnnotatedElementMirror target) { + return target.getRealAnnotation().allowWeakWithSameValue(); + } + + private static void logWeak(ProvidesMatchersAnnotatedElementMirror target) { + Optional am = target.getAnnotationMirror(); + Optional av = am.map(a -> a.getElementValues().entrySet().stream() + .filter(kv -> kv.getKey().getSimpleName().toString().equals("allowWeakWithSameValue")) + .map(Entry::getValue).findAny().orElse(null)); + target.getMessager().printMessage(Kind.MANDATORY_WARNING, + "This class use the option allowWeakWithSameValue and a weak WithSameValue is detected. The generated WithSameValue DSL may not be able to fully control all the field of this class", + target.getElement(), am.orElse(null), av.orElse(null)); } public static Collection> generateParentValueDSLStarter( - ProvidesMatchersAnnotatedElementMirror target, boolean hasSuper) { - if (hasSuper) { - return asList(() -> target.getParentMirror() - .map(parentMirror -> generateWithSameValueWithParentMatcherIgnore(target, true)).orElseGet(() -> { - if (isWeakAllowed(target)) { - return generateWithSameValueWithParentMatcherIgnore(target, true); - } else { - return null; - } - }), - () -> target.getParentMirror() - .map(parentMirror -> generateWithSameValueWithParentMatcherAndNoIgnore(target)) - .orElseGet(() -> { - if (isWeakAllowed(target)) { - return generateWithSameValueWithParentMatcherAndNoIgnore(target); - } else { - return null; - } - })); - } + ProvidesMatchersAnnotatedElementMirror target) { + return asList(() -> target.getParentMirror() + .map(parentMirror -> generateWithSameValueWithParentMatcherIgnore(target, true)).orElseGet(() -> { + if (isWeakAllowed(target)) { + logWeak(target); + return generateWithSameValueWithParentMatcherIgnore(target, true); + } else { + return null; + } + }), + () -> target.getParentMirror() + .map(parentMirror -> generateWithSameValueWithParentMatcherAndNoIgnore(target)) + .orElseGet(() -> { + if (isWeakAllowed(target)) { + logWeak(target); + return generateWithSameValueWithParentMatcherAndNoIgnore(target); + } else { + return null; + } + })); + } + + public static Collection> generateNoParentValueDSLStarter( + ProvidesMatchersAnnotatedElementMirror target) { return asList(() -> generateWithSameValueWithParentMatcherIgnore(target, false), () -> generateWithSameValueWithParentMatcherAndNoIgnore(target)); }