Skip to content

Commit

Permalink
Merge pull request #42 from synyx/use-constructor-with-most-params
Browse files Browse the repository at this point in the history
Use constructor with highest number of params instead of lowest
  • Loading branch information
tknell authored Feb 21, 2024
2 parents 1caa7b6 + 25d94ed commit fa73fd4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public Object createObjectInternal(ObjectInformation parentInformation) throws F
+ " And we also could not find a constructor!");
}

declaredConstructors.sort(Comparator.comparingInt(Constructor::getParameterCount));

Constructor declaredConstructor = declaredConstructors.get(0);
// use constructor with most parameters to potentially set the most final fields
Constructor declaredConstructor = declaredConstructors.stream()
.max(Comparator.comparingInt(Constructor::getParameterCount))
.orElse(null);

try {
declaredConstructor.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public void testNoDefaultConstructor() throws FillingException {


@Test
public void testTakesTheConstructorWithTheLeastNumberOfParameters() throws FillingException {
public void testTakesTheConstructorWithTheHighestNumberOfParameters() throws FillingException {

JustAnotherBeanStrategy strategy = setupStrategy();

MultipleConstructorsObject object = (MultipleConstructorsObject) strategy.createObject(new ObjectInformation(
MultipleConstructorsObject.class, null, null, null, null, null));
assertThat(object, notNullValue());
assertThat(object.getFoo(), notNullValue());
assertThat(object.getBar(), nullValue());
assertThat(object.getBar(), notNullValue());
}


Expand Down

0 comments on commit fa73fd4

Please sign in to comment.