Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make builder style setter compatible with the library #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static Method findSetterFor(final Class<?> clazz, final Field field) {
return Arrays.stream(clazz.getMethods())
.filter(methodHasOnlyOneParameter())
.filter(areParameterAndFieldTypeAssignable(field))
.filter(returnTypeIsVoid())
.filter(returnTypeIsVoidOrClass())
.filter(method -> prefixMatchesSettersPrefixAndHasExpectedLength(method, field.getName()))
.findAny()
.orElseThrow(() -> new SetterNotFoundException(clazz, field));
Expand All @@ -31,8 +31,8 @@ public static Method findGetterFor(final Class<?> clazz, final Field field) {
.orElseThrow(() -> new GetterNotFoundException(clazz, field));
}

private static Predicate<Method> returnTypeIsVoid() {
return method -> method.getReturnType() == void.class;
private static Predicate<Method> returnTypeIsVoidOrClass() {
return method -> Arrays.asList(method.getDeclaringClass(), void.class).contains(method.getReturnType());
}

private static Predicate<Method> areParameterAndFieldTypeAssignable(final Field field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void Should_Pass_All_Setter_Tests_Including_Fields() {
// given
final SetterTester setterTester = new SetterTester();
final Class<?> clazz = BadPojoSetter.class;
final List<String> includedFields = newArrayList("a", "b");
final List<String> includedFields = newArrayList("a", "b", "e");

// when
final Throwable result = catchThrowable(() -> setterTester.test(clazz, FieldPredicate.include(includedFields)));
Expand Down Expand Up @@ -76,6 +76,7 @@ private class BadPojoSetter {
private int b;
private int c;
private int d;
private int e;

public void setA(final int a) {
this.a = a;
Expand All @@ -88,6 +89,10 @@ public void setB(final int b) {
public void setX(final char x) {
}

public BadPojoSetter setE(int e) {
this.e = e;
return this;
}
}

@Setter
Expand All @@ -97,7 +102,12 @@ private class GoodPojoSetter {
private int b;
private int c;
private int d;
private int e;

public GoodPojoSetter setE(int e) {
this.e = e;
return this;
}
}

private class Setters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ private class Setters {
private int otherId;
private String name;
private String otherName;
private Object object;

public Setters setObject(Object object) {
this.object = object;
return this;
}

public void setOtherId(final int otherId) {
this.otherId = otherId;
Expand Down