Skip to content

Commit

Permalink
Issue #331 - Refactor according codebeat
Browse files Browse the repository at this point in the history
  • Loading branch information
boretti committed May 31, 2020
1 parent cc5b6d5 commit db26f05
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
Expand Up @@ -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()));
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -48,73 +48,72 @@ private static DSLMethod generateWithSameValueWithParentMatcherIgnore(ProvidesMa
List<String> lines = new ArrayList<>();
lines.add("java.util.Set<String> 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<String> 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<AnnotationMirror> am = target.getAnnotationMirror();
Optional<? extends AnnotationValue> 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<AnnotationMirror> am = target.getAnnotationMirror();
Optional<? extends AnnotationValue> 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<Supplier<DSLMethod>> 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<Supplier<DSLMethod>> generateNoParentValueDSLStarter(
ProvidesMatchersAnnotatedElementMirror target) {
return asList(() -> generateWithSameValueWithParentMatcherIgnore(target, false),
() -> generateWithSameValueWithParentMatcherAndNoIgnore(target));
}
Expand Down

0 comments on commit db26f05

Please sign in to comment.