Skip to content

Commit 5ef8276

Browse files
committed
Adjust GenericClassCreation task solution and tests
1 parent 1d9486c commit 5ef8276

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

src/main/java/by/andd3dfx/core/GenericClassCreation.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
package by.andd3dfx.core;
22

3-
import java.lang.reflect.InvocationTargetException;
3+
import lombok.SneakyThrows;
4+
45
import java.util.function.Supplier;
56

67
/**
8+
* <pre>
79
* Examples of object creation when it defined as generic type.
810
*
911
* According to: https://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java
12+
* </pre>
1013
*/
1114
public class GenericClassCreation {
1215

13-
public static class SomeContainer1<E> {
14-
15-
public E createObject(Class<E> clazz)
16-
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
16+
public static class CreatorUsingDeclaredConstructor<E> {
17+
@SneakyThrows
18+
public E createObject(Class<E> clazz) {
1719
return clazz.getDeclaredConstructor().newInstance();
1820
}
1921
}
2022

21-
public static class SomeContainer2<E> {
22-
23+
public static class CreatorUsingSupplier<E> {
2324
private Supplier<E> supplier;
2425

25-
public SomeContainer2(Supplier<E> supplier) {
26+
public CreatorUsingSupplier(Supplier<E> supplier) {
2627
this.supplier = supplier;
2728
}
2829

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
11
package by.andd3dfx.core;
22

3+
import by.andd3dfx.core.GenericClassCreation.CreatorUsingDeclaredConstructor;
4+
import by.andd3dfx.core.GenericClassCreation.CreatorUsingSupplier;
5+
import lombok.AllArgsConstructor;
6+
import org.junit.Test;
7+
8+
import static org.hamcrest.CoreMatchers.instanceOf;
39
import static org.hamcrest.CoreMatchers.is;
410
import static org.hamcrest.MatcherAssert.assertThat;
5-
6-
import by.andd3dfx.core.GenericClassCreation.SomeContainer1;
7-
import by.andd3dfx.core.GenericClassCreation.SomeContainer2;
8-
import java.lang.reflect.InvocationTargetException;
9-
import org.junit.Test;
11+
import static org.junit.Assert.assertThrows;
1012

1113
public class GenericClassCreationTest {
1214

1315
@Test
14-
public void createObject1()
15-
throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
16-
SomeContainer1<String> someContainer1 = new SomeContainer1<>();
16+
public void createStringBy_CreatorUsingDeclaredConstructor() {
17+
CreatorUsingDeclaredConstructor<String> container = new CreatorUsingDeclaredConstructor<>();
18+
19+
assertThat(container.createObject(String.class), is(""));
20+
}
21+
22+
@Test
23+
public void createStringBy_CreatorUsingSupplier() {
24+
CreatorUsingSupplier<String> container = new CreatorUsingSupplier<>(String::new);
25+
26+
assertThat(container.createObject(), is(""));
27+
}
28+
29+
@Test
30+
public void createClassWithoutFieldsBy_CreatorUsingDeclaredConstructor() {
31+
CreatorUsingDeclaredConstructor<CustomClassWithoutFields> container = new CreatorUsingDeclaredConstructor<>();
32+
33+
assertThat(container.createObject(CustomClassWithoutFields.class), instanceOf(CustomClassWithoutFields.class));
34+
}
1735

18-
String createdObject = someContainer1.createObject(String.class);
36+
@Test
37+
public void createClassWithoutFieldsBy_CreatorUsingSupplier() {
38+
CreatorUsingSupplier<CustomClassWithoutFields> container = new CreatorUsingSupplier<>(CustomClassWithoutFields::new);
1939

20-
assertThat("Empty string expected", createdObject, is(""));
40+
assertThat(container.createObject(), instanceOf(CustomClassWithoutFields.class));
2141
}
2242

2343
@Test
24-
public void createObject2() {
25-
SomeContainer2<String> someContainer2 = new SomeContainer2<>(String::new);
44+
public void createClassWithFieldBy_CreatorUsingDeclaredConstructor() {
45+
CreatorUsingDeclaredConstructor<CustomClassWithField> container = new CreatorUsingDeclaredConstructor<>();
46+
47+
assertThrows(NoSuchMethodException.class, () -> {
48+
container.createObject(CustomClassWithField.class);
49+
});
50+
}
2651

27-
String createdObject = someContainer2.createObject();
52+
@Test
53+
public void createClassWithFieldBy_CreatorUsingSupplier() {
54+
CreatorUsingSupplier<CustomClassWithField> container = new CreatorUsingSupplier<>(() -> new CustomClassWithField(0));
55+
56+
assertThat(container.createObject(), instanceOf(CustomClassWithField.class));
57+
}
58+
59+
public static class CustomClassWithoutFields {
60+
}
2861

29-
assertThat("Empty string expected", createdObject, is(""));
62+
@AllArgsConstructor
63+
public static class CustomClassWithField {
64+
private int value;
3065
}
3166
}

0 commit comments

Comments
 (0)