Skip to content

Commit 2eebc27

Browse files
committed
refactor: inline method and adjust code style
In addition, tests for the new `Reflector` method were added.
1 parent 7184ab8 commit 2eebc27

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

src/main/java/com/github/nylle/javafixture/Reflector.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,19 @@ public Stream<Field> getDeclaredFields() {
6161
return getDeclaredFields(clazz);
6262
}
6363

64-
private Stream<Field> getDeclaredFields(Class<?> type) {
65-
return Stream.concat(
66-
Stream.of(type.getDeclaredFields()).filter(field -> !Modifier.isStatic(field.getModifiers())),
67-
Optional.ofNullable(type.getSuperclass())
68-
.map(superclass -> getDeclaredFields(superclass))
69-
.orElse(Stream.of()));
70-
}
71-
7264
public Annotation[] getFieldAnnotations(Field field) {
7365
try {
74-
return getFieldAnnotations(field, clazz).toArray(Annotation[]::new);
66+
return Stream.concat(Arrays.stream(Introspector.getBeanInfo(clazz).getPropertyDescriptors())
67+
.filter(property -> !Modifier.isStatic(field.getModifiers()))
68+
.filter(property -> property.getName().equals(field.getName()))
69+
.flatMap(propertyDescriptor -> Stream.of(propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod())
70+
.filter(x -> Objects.nonNull(x))
71+
.flatMap(method -> Stream.of(method.getAnnotations()))), Arrays.stream(field.getAnnotations())).toArray(x -> new Annotation[x]);
7572
} catch (IntrospectionException e) {
7673
return field.getAnnotations();
7774
}
7875
}
7976

80-
public Stream<Annotation> getFieldAnnotations(Field field, Class<?> type) throws IntrospectionException {
81-
82-
return Stream.concat(Arrays.stream(Introspector.getBeanInfo(type).getPropertyDescriptors())
83-
.filter(property -> !Modifier.isStatic(field.getModifiers()))
84-
.filter(property -> property.getName().equals(field.getName()))
85-
.flatMap(propertyDescriptor -> Stream.of(propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod())
86-
.filter(Objects::nonNull)
87-
.flatMap(method -> Stream.of(method.getAnnotations()))
88-
), Arrays.stream(field.getAnnotations()));
89-
}
90-
91-
9277
public void setField(Field field, Object value) {
9378
try {
9479
field.setAccessible(true);
@@ -99,4 +84,12 @@ public void setField(Field field, Object value) {
9984
throw new SpecimenException(format("Unable to set field %s on object of type %s", field.getName(), clazz.getName()), e);
10085
}
10186
}
87+
88+
private Stream<Field> getDeclaredFields(Class<?> type) {
89+
return Stream.concat(
90+
Stream.of(type.getDeclaredFields()).filter(field -> !Modifier.isStatic(field.getModifiers())),
91+
Optional.ofNullable(type.getSuperclass())
92+
.map(superclass -> getDeclaredFields(superclass))
93+
.orElse(Stream.of()));
94+
}
10295
}

src/main/java/com/github/nylle/javafixture/specimen/ObjectSpecimen.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,14 @@ private T populate(CustomizationContext customizationContext) {
6161
var result = context.cached(type, instanceFactory.instantiate(type));
6262
var reflector = new Reflector<>(result).validateCustomization(customizationContext, type);
6363
try {
64-
6564
reflector.getDeclaredFields()
6665
.filter(field -> !customizationContext.getIgnoredFields().contains(field.getName()))
6766
.forEach(field -> reflector.setField(field,
6867
customizationContext.getCustomFields().getOrDefault(
6968
field.getName(),
7069
Map.<String, ISpecimen<?>>of().getOrDefault(
7170
field.getGenericType().getTypeName(),
72-
specimenFactory.build(SpecimenType.fromClass(field.getGenericType())))
73-
.create(new CustomizationContext(List.of(), Map.of(), false), reflector.getFieldAnnotations(field)))));
71+
specimenFactory.build(SpecimenType.fromClass(field.getGenericType()))).create(new CustomizationContext(List.of(), Map.of(), false), reflector.getFieldAnnotations(field)))));
7472
} catch (SpecimenException ex) {
7573
return context.overwrite(type, instanceFactory.construct(type, customizationContext));
7674
}

src/test/java/com/github/nylle/javafixture/ReflectorTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
package com.github.nylle.javafixture;
22

3+
import com.github.nylle.javafixture.testobjects.TestObjectWithJakartaValidationAnnotations;
4+
import com.github.nylle.javafixture.testobjects.TestObjectWithJakartaValidationAnnotationsOnMethod;
35
import com.github.nylle.javafixture.testobjects.inheritance.GenericChild;
46
import org.junit.jupiter.api.DisplayName;
57
import org.junit.jupiter.api.Nested;
68
import org.junit.jupiter.api.Test;
79
import org.mockito.Mockito;
810

11+
import jakarta.validation.constraints.Size;
12+
13+
import java.beans.IntrospectionException;
914
import java.lang.reflect.Field;
1015
import java.lang.reflect.InaccessibleObjectException;
16+
import java.util.Arrays;
1117
import java.util.List;
1218
import java.util.Map;
19+
import java.util.stream.Collectors;
1320

21+
import static org.assertj.core.api.Assertions.assertThat;
1422
import static org.assertj.core.api.Assertions.assertThatCode;
1523
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1624
import static org.mockito.ArgumentMatchers.any;
1725
import static org.mockito.Mockito.doThrow;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
1828

1929
class ReflectorTest {
2030

@@ -120,4 +130,46 @@ void catchInaccessibleObjectException() {
120130
}
121131
}
122132

133+
@Nested
134+
class GetFieldAnnotations {
135+
136+
@Test
137+
void returnsSizeAnnotationOnPrivateField() {
138+
var sut = new Reflector<>(new TestObjectWithJakartaValidationAnnotations());
139+
140+
var field = sut.getDeclaredFields().filter(x -> x.getName().equals("withMinMaxAnnotation")).findFirst().get();
141+
142+
var actual = Arrays.stream(sut.getFieldAnnotations(field)).collect(Collectors.toList());
143+
144+
assertThat(actual).hasSize(1);
145+
assertThat(actual.get(0).annotationType()).isEqualTo(Size.class);
146+
}
147+
148+
@Test
149+
void returnsSizeAnnotationOnGetter() {
150+
var sut = new Reflector<>(new TestObjectWithJakartaValidationAnnotationsOnMethod());
151+
152+
var field = sut.getDeclaredFields().filter(x -> x.getName().equals("withMinMaxAnnotation")).findFirst().get();
153+
154+
var actual = Arrays.stream(sut.getFieldAnnotations(field)).collect(Collectors.toList());
155+
156+
assertThat(actual).hasSize(1);
157+
assertThat(actual.get(0).annotationType()).isEqualTo(Size.class);
158+
}
159+
160+
@Test
161+
void returnsSizeAnnotationOnFieldIfGetterThrowsIntrospectionException() {
162+
var sut = new Reflector<>(new TestObjectWithJakartaValidationAnnotations());
163+
var expected = sut.getDeclaredFields().filter(x -> x.getName().equals("withMinMaxAnnotation")).findFirst().get().getAnnotations();
164+
165+
var throwingField = mock(Field.class);
166+
when(throwingField.getModifiers()).thenAnswer(x -> { throw new IntrospectionException("expected for test"); });
167+
when(throwingField.getAnnotations()).thenReturn(expected);
168+
169+
var actual = Arrays.stream(sut.getFieldAnnotations(throwingField)).collect(Collectors.toList());
170+
171+
assertThat(actual).hasSize(1);
172+
assertThat(actual.get(0).annotationType()).isEqualTo(Size.class);
173+
}
174+
}
123175
}

0 commit comments

Comments
 (0)