3
3
import java .io .Serializable ;
4
4
import java .lang .annotation .Annotation ;
5
5
import java .lang .reflect .Field ;
6
+ import java .lang .reflect .Modifier ;
6
7
import java .util .ArrayList ;
7
8
import java .util .Arrays ;
8
9
import java .util .Comparator ;
12
13
import java .util .Map ;
13
14
import java .util .Map .Entry ;
14
15
import java .util .Optional ;
16
+ import java .util .Set ;
15
17
import java .util .function .Predicate ;
16
18
import java .util .stream .Collectors ;
17
19
20
+ import org .reflections .ReflectionUtils ;
21
+
18
22
import com .fasterxml .jackson .databind .JsonMappingException ;
19
23
import com .fasterxml .jackson .databind .JsonNode ;
20
24
import com .fasterxml .jackson .databind .ObjectMapper ;
@@ -43,7 +47,8 @@ public final class UiFormSchemaGenerator {
43
47
private static UiFormSchemaGenerator instance ;
44
48
45
49
public UiForm generate (Class <? extends Serializable > formDto ) throws JsonMappingException {
46
- Field [] declaredFields = formDto .getDeclaredFields ();
50
+ Set <Field > declaredFields = ReflectionUtils .getAllFields (formDto ,
51
+ field -> !Modifier .isStatic (field .getModifiers ()));
47
52
ObjectMapper mapper = new ObjectMapper ();
48
53
49
54
JsonSchemaGenerator schemaGen = initSchemaGen (mapper );
@@ -105,13 +110,13 @@ private ObjectNode buildActionNode(ObjectMapper mapper, Action action) {
105
110
return node ;
106
111
}
107
112
108
- private ObjectNode handlerGroupedFields (ObjectMapper mapper , Field [] declaredFields ,
113
+ private ObjectNode handlerGroupedFields (ObjectMapper mapper , Set < Field > declaredFields ,
109
114
Map <Field , JsonNode > sortedNodes ) {
110
115
Predicate <? super Field > checkFieldSetAnnotation = field -> field .isAnnotationPresent (FieldSet .class );
111
116
112
117
Map <String , List <JsonNode >> groupedFields = new LinkedHashMap <>();
113
118
114
- Arrays .stream (declaredFields ).filter (checkFieldSetAnnotation )
119
+ declaredFields .stream ().filter (checkFieldSetAnnotation )
115
120
.forEach (field -> groupFieldsByTab (sortedNodes , field , groupedFields ));
116
121
117
122
ArrayNode groups = mapper .createArrayNode ();
@@ -123,7 +128,7 @@ private ObjectNode handlerGroupedFields(ObjectMapper mapper, Field[] declaredFie
123
128
124
129
}
125
130
126
- private ObjectNode handleTabbedFields (ObjectMapper mapper , Field [] declaredFields , Map <Field , JsonNode > nodes ) {
131
+ private ObjectNode handleTabbedFields (ObjectMapper mapper , Set < Field > declaredFields , Map <Field , JsonNode > nodes ) {
127
132
Predicate <? super Field > checkTabAnnotation = field -> field .isAnnotationPresent (Tab .class );
128
133
129
134
Comparator <? super Field > tabIndexComparator = (field1 , field2 ) -> Integer
@@ -138,7 +143,7 @@ private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredField
138
143
139
144
Map <String , List <JsonNode >> groupedFieldsByTab = new LinkedHashMap <>();
140
145
141
- Arrays .stream (declaredFields ).filter (checkTabAnnotation ).sorted (fieldIndexComparator ).sorted (tabIndexComparator )
146
+ declaredFields .stream ().filter (checkTabAnnotation ).sorted (fieldIndexComparator ).sorted (tabIndexComparator )
142
147
.forEach (field -> groupFieldsByTab (nodes , field , groupedFieldsByTab ));
143
148
144
149
ArrayNode tabs = mapper .createArrayNode ();
@@ -161,10 +166,10 @@ private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredField
161
166
162
167
}
163
168
164
- private Map <Field , JsonNode > initFieldsFormDefinition (ObjectMapper mapper , Field [] declaredFields ) {
169
+ private Map <Field , JsonNode > initFieldsFormDefinition (ObjectMapper mapper , Set < Field > declaredFields ) {
165
170
Map <Field , JsonNode > nodes = new HashMap <>();
166
171
167
- Arrays . stream ( declaredFields ) .forEach (field -> buildFormDefinition (nodes , mapper , field ));
172
+ declaredFields .forEach (field -> buildFormDefinition (nodes , mapper , field ));
168
173
169
174
return nodes ;
170
175
}
0 commit comments