Skip to content

Commit f0b9d13

Browse files
committed
Merge branch 'develop'
2 parents 7acab70 + ccf395e commit f0b9d13

File tree

8 files changed

+129
-14
lines changed

8 files changed

+129
-14
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>jre-utils</artifactId>
13-
<version>0.3.13</version>
13+
<version>0.3.14</version>
1414
<name>JreUtils</name>
1515
<packaging>jar</packaging>
1616

src/main/java/info/unterrainer/commons/jreutils/FieldParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,17 @@ private Object resolveInstanceOf(final Object instance, final Field field)
7272
throws IllegalArgumentException, IllegalAccessException {
7373
Object fieldInstance = field.get(instance);
7474
Class<?> currentType = field.getType();
75-
if (currentIndex == null)
76-
currentIndex = 0;
7775

7876
if (List.class.isAssignableFrom(currentType)) {
77+
if (currentIndex == null)
78+
return (fieldInstance);
7979
int size = ((List<?>) fieldInstance).size();
8080
currentIndex = cap(currentIndex, size);
8181
return ((List<?>) fieldInstance).get(currentIndex);
8282
}
8383
if (currentType.isArray()) {
84+
if (currentIndex == null)
85+
return (fieldInstance);
8486
int size = ((Object[]) fieldInstance).length;
8587
currentIndex = cap(currentIndex, size);
8688
return ((Object[]) fieldInstance)[currentIndex];

src/main/java/info/unterrainer/commons/jreutils/Reflecting.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@ public class Reflecting {
1919
* 'myField.myArray:0.myVar')
2020
*
2121
* @param classToScan the class to start scanning in
22-
* @param fieldTypeToFind the type to search for
22+
* @param annotationToScanFor the annotation the fields we should scan are
23+
* annotated with
24+
* @return a list of paths where the given fields have been found
25+
*/
26+
public List<String> getPathsOf(final Class<?> classToScan, final Class<? extends Annotation> annotationToScanFor) {
27+
return getPathsOf(classToScan, null, annotationToScanFor, "");
28+
}
29+
30+
/**
31+
* Scans object tree connected by annotated fields for a given class using
32+
* reflection and returns found paths.<br>
33+
* <br>
34+
* Resolves generic Lists and Arrays as well (example for indexing:
35+
* 'myField.myArray:0.myVar')
36+
*
37+
* @param classToScan the class to start scanning in
38+
* @param fieldTypeToFind the type to search for or null, if you have no
39+
* type-restriction for your search
2340
* @param annotationToScanFor the annotation the fields we should scan are
2441
* annotated with
2542
* @return a list of paths where the given fields have been found
@@ -43,9 +60,8 @@ private void getFields(final Class<?> classToScan, final Class<?> typeToFind,
4360

4461
if (field.isAnnotationPresent(annotation)) {
4562
Class<?> currentType = resolveTypeOf(field);
46-
if (typeToFind.isAssignableFrom(currentType)) {
63+
if (typeToFind == null || typeToFind.isAssignableFrom(currentType)) {
4764
paths.add(currentPath + field.getName());
48-
continue;
4965
}
5066
paths.addAll(getPathsOf(currentType, typeToFind, annotation, currentPath + field.getName() + "."));
5167
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package info.unterrainer.commons.jreutils.logging;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import info.unterrainer.commons.jreutils.Reflecting;
8+
import lombok.experimental.UtilityClass;
9+
10+
@UtilityClass
11+
public class FieldLogger {
12+
13+
public static String log(final Object instance, final Class<?> clazz) {
14+
List<String> paths = Reflecting.getPathsOf(clazz, LogField.class);
15+
StringBuilder sb = new StringBuilder();
16+
for (String path : paths) {
17+
try {
18+
Object o = Reflecting.getFieldByPath(path, instance, LogField.class);
19+
sb.append(path);
20+
sb.append(": ");
21+
if (o == null)
22+
sb.append("null");
23+
else if (o instanceof List) {
24+
String s = ((List<?>) o).stream().map(Object::toString).collect(Collectors.joining(", "));
25+
sb.append("[");
26+
sb.append(s);
27+
sb.append("]");
28+
} else if (o.getClass().isArray()) {
29+
String s = Arrays.stream((Object[]) o).map(Object::toString).collect(Collectors.joining(", "));
30+
sb.append("[");
31+
sb.append(s);
32+
sb.append("]");
33+
} else
34+
sb.append(o.toString());
35+
sb.append(System.lineSeparator());
36+
} catch (IllegalArgumentException | IllegalAccessException e) {
37+
// NOOP
38+
}
39+
}
40+
return sb.toString();
41+
}
42+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package info.unterrainer.commons.jreutils.logging;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.FIELD)
10+
public @interface LogField {
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package info.unterrainer.commons.jreutils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import info.unterrainer.commons.jreutils.dtos.Test1Class;
6+
import info.unterrainer.commons.jreutils.logging.FieldLogger;
7+
8+
public class FieldLoggerTests {
9+
10+
@Test
11+
public void TestReadingFields() {
12+
Test1Class t = new Test1Class();
13+
t.setName("name");
14+
t.setVendor("vendor");
15+
t.setType(2L);
16+
t.setAddress("This is an address.");
17+
t.setClassName("Classname should not be visible because it's not annotated correctly.");
18+
StringBuilder sb = new StringBuilder();
19+
sb.append("=========================================");
20+
sb.append(System.lineSeparator());
21+
sb.append(FieldLogger.log(t, Test1Class.class));
22+
sb.append("=========================================");
23+
sb.append(System.lineSeparator());
24+
System.out.println(sb.toString());
25+
}
26+
27+
@Test
28+
public void TestReadingLists() {
29+
Test1Class t = new Test1Class();
30+
StringBuilder sb = new StringBuilder();
31+
sb.append("=========================================");
32+
sb.append(System.lineSeparator());
33+
sb.append(FieldLogger.log(t, Test1Class.class));
34+
sb.append("=========================================");
35+
sb.append(System.lineSeparator());
36+
System.out.println(sb.toString());
37+
}
38+
}

src/test/java/info/unterrainer/commons/jreutils/ReflectingTests.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public void TestReadingFields() {
2020
assertThat(results).containsAll(List.of("myType", "usedClass.myType"));
2121
}
2222

23+
@Test
24+
public void TestReadingFieldsWithNullTypeGetsAll() {
25+
List<String> results = Reflecting.getPathsOf(Test1Class.class, ContainsMyType.class);
26+
assertThat(results).containsAll(List.of("myType", "usedClass.myType"));
27+
}
28+
2329
@Test
2430
public void TestReadingFieldsContainsFieldsInArrays() {
2531
List<String> results = Reflecting.getPathsOf(Test1Class.class, MyType.class, ContainsMyType.class);
@@ -89,14 +95,6 @@ public void TestWritingOfFieldInUsedClassInList() throws IllegalArgumentExceptio
8995
assertThat(tc.getUsedClassList().get(2).getMyType().getName()).isEqualTo("gluppy");
9096
}
9197

92-
@Test
93-
public void TestIndexOfListEmptyReturnsFirstEntry() throws IllegalArgumentException, IllegalAccessException {
94-
Test1Class tc = new Test1Class();
95-
MyType mt = Reflecting.getFieldByPath("usedClassList.myType", tc, ContainsMyType.class);
96-
mt.setName("blubb");
97-
assertThat(tc.getUsedClassList().get(0).getMyType().getName()).isEqualTo("blubb");
98-
}
99-
10098
@Test
10199
public void TestIndexOfListOverflowJustStartsFromBeginning()
102100
throws IllegalArgumentException, IllegalAccessException {

src/test/java/info/unterrainer/commons/jreutils/dtos/Test1Class.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22

33
import java.util.List;
44

5+
import info.unterrainer.commons.jreutils.logging.LogField;
56
import lombok.Data;
67

78
@Data
89
public class Test1Class {
910

11+
@LogField
1012
private String name;
1113
@ContainsMyType
14+
@LogField
1215
private List<MyType> myTypeList = List.of(new MyType(), new MyType(), new MyType(), new MyType());
16+
@LogField
1317
private String vendor;
18+
@LogField
1419
private Long type;
1520
@ContainsMyType
21+
@LogField
1622
private MyType myType = new MyType();
23+
@LogField
1724
private String address;
1825
@ContainsMyType
26+
@LogField
1927
private MyType[] myTypeArray = List.of(new MyType(), new MyType(), new MyType(), new MyType(), new MyType())
2028
.toArray(new MyType[0]);
2129
private String className;

0 commit comments

Comments
 (0)