Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ private static <T> Constructor<T> getSuitableConstructor(Class<T> clazz, Object[
var argumentTypes = getArgumentTypes(arguments);

try {
return (Constructor<T>)findConstructorMatch(clazz, argumentTypes);
return findConstructorMatch(clazz, argumentTypes);
} catch (NoSuchMethodException e) {
if (argumentTypes == null || argumentTypes.length == 0) {
throw new ConstructorNotFoundException();
}

var types = new StringBuilder();
for (var type : argumentTypes) {
types.append(type.getName() + System.lineSeparator());
Expand Down Expand Up @@ -73,7 +77,7 @@ private static <T> Constructor<T> findVarArgsConstructor(Class clazz, Class[] ar
args[args.length - 1] = getVarArgsType(clazz, argumentTypes);

return clazz.getDeclaredConstructor(args);
} catch (NoSuchMethodException ignored) {
} catch (NoSuchMethodException|NullPointerException ignored) {
}

throw new NoSuchMethodException("No matching constructor found for the provided argument types.");
Expand Down Expand Up @@ -113,7 +117,9 @@ public ConstructorNotFoundException(int numberOfTypes, String types) {
Given argument types:\n%s
""").formatted(numberOfTypes, types));
}

public ConstructorNotFoundException() {
super("No default constructor was found." + System.lineSeparator());
}

public ConstructorNotFoundException(String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package factory;

import factory.data.Boss;
import factory.data.Employee;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import solutions.bellatrix.core.plugins.junit.BaseTest;
import solutions.bellatrix.core.utilities.InstanceFactory;
import solutions.bellatrix.core.utilities.ObjectFactory;

public class InstanceFactoryTests extends BaseTest {
@Test
Expand All @@ -28,7 +30,12 @@ public void objectReturned_When_UsedCustomArgsConstructor() {
}

@Test
public void objectNotReturned_When_UsedNonExistentConstructor() {
public void returnedNull_When_TriedUsingNonExistentConstructor() {
Assertions.assertNull(InstanceFactory.create(Employee.class, "John Doe"));
}

@Test
public void returnedNull_When_TriedUsingNonExistentNoArgsConstructor() {
Assertions.assertNull(InstanceFactory.create(Boss.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package factory.data;

public class Boss {
public Boss(String firstName, String lastName, String businessEmail) {
this.firstName = firstName;
this.lastName = lastName;
this.businessEmail = businessEmail;
}

public Boss(String firstName, String lastName, String businessEmail, String personalEmail, Object[] additionalData) {
this.firstName = firstName;
this.lastName = lastName;
this.businessEmail = businessEmail;
this.personalEmail = personalEmail;
this.additionalData = additionalData;
}

public String firstName;
public String lastName;
public String businessEmail;
public String personalEmail;
public Object[] additionalData;
}