8
8
import org .springframework .data .domain .Page ;
9
9
import org .springframework .data .domain .Persistable ;
10
10
11
+ import java .beans .PropertyDescriptor ;
11
12
import java .io .Serializable ;
12
13
import java .lang .reflect .InvocationTargetException ;
13
14
import java .lang .reflect .Method ;
@@ -21,35 +22,24 @@ public class RelationsFetcher<T extends Persistable<ID>, ID extends Serializable
21
22
22
23
private final Set <RelationsConfig <ID , ? extends Serializable >> configs ;
23
24
24
- private final Class <T > tClass ;
25
-
26
25
public RelationsFetcher (Set <RelationsConfig <ID , ? extends Serializable >> configs , Class <T > tClass ) {
27
26
this .configs = configs ;
28
- this .tClass = tClass ;
27
+ this .validate ( configs , tClass ) ;
29
28
}
30
29
31
- public T fetchRelations (
32
- final T one ,
33
- final ResourceRequest request
34
- ) throws InvocationTargetException , IllegalAccessException {
30
+ public T fetchRelations (final T one , final ResourceRequest request ) throws InvocationTargetException , IllegalAccessException {
35
31
if (!configs .isEmpty () && !request .getRelated ().isEmpty ()) {
36
32
Map <RequestRelation , RelationsConfig <ID , ? extends Serializable >> map = getExisted (request , configs );
37
33
for (Map .Entry <RequestRelation , RelationsConfig <ID , ? extends Serializable >> entry : map .entrySet ()) {
38
34
RelationsConfig <ID , ? extends Serializable > config = entry .getValue ();
39
35
Set <? extends Serializable > ids = config .getIds (one .getId ());
40
- if (config .getMethod () == null ) {
41
- config .setMethod (getSetter (tClass , config .getField ()));
42
- }
43
36
config .getMethod ().invoke (one , config .getItems (ids , entry .getKey ().getFields ()));
44
37
}
45
38
}
46
39
return one ;
47
40
}
48
41
49
- public Set <T > fetchRelations (
50
- final Page <T > page ,
51
- final ResourceRequest request
52
- ) throws InvocationTargetException , IllegalAccessException {
42
+ public Set <T > fetchRelations (final Page <T > page , final ResourceRequest request ) throws InvocationTargetException , IllegalAccessException {
53
43
Set <T > all = new LinkedHashSet <>();
54
44
if (!configs .isEmpty () && !request .getRelated ().isEmpty ()) {
55
45
HashSet <ID > ids = new HashSet <>();
@@ -59,9 +49,6 @@ public Set<T> fetchRelations(
59
49
Map <RequestRelation , RelationsConfig <ID , ? extends Serializable >> map = getExisted (request , configs );
60
50
for (Map .Entry <RequestRelation , RelationsConfig <ID , ? extends Serializable >> entry : map .entrySet ()) {
61
51
RelationsConfig <ID , ? extends Serializable > config = entry .getValue ();
62
- if (config .getMethod () == null ) {
63
- config .setMethod (getSetter (tClass , config .getField ()));
64
- }
65
52
Map <ID , ? extends Collection <? extends Serializable >> idMap = config .getIds (ids );
66
53
Set <Serializable > relatedIds = new HashSet <>();
67
54
for (Collection <? extends Serializable > collection : idMap .values ()) {
@@ -82,11 +69,22 @@ public Set<T> fetchRelations(
82
69
return all ;
83
70
}
84
71
72
+ private void validate (Set <RelationsConfig <ID , ? extends Serializable >> configs , Class <T > tClass ) {
73
+ for (RelationsConfig <ID , ? extends Serializable > config : configs ) {
74
+ config .setMethod (getSetter (tClass , config .getField ()));
75
+ }
76
+ }
77
+
85
78
@ NotNull
86
- private Method getSetter (final Class tClass , String field ) {
87
- Method writeMethod = BeanUtils .getPropertyDescriptor (tClass , field ).getWriteMethod ();
79
+ private Method getSetter (final Class tClass , String field ) throws RuntimeException {
80
+ RuntimeException ex = new RuntimeException ("Given class has no proper setter for a defined relationship." );
81
+ PropertyDescriptor descriptor = BeanUtils .getPropertyDescriptor (tClass , field );
82
+ if (descriptor == null ) {
83
+ throw ex ;
84
+ }
85
+ Method writeMethod = descriptor .getWriteMethod ();
88
86
if (writeMethod == null || writeMethod .getModifiers () != Modifier .PUBLIC ) {
89
- throw new RuntimeException ( "Given class has no proper setter for a relationship." ) ;
87
+ throw ex ;
90
88
}
91
89
92
90
Class <?>[] types = writeMethod .getParameterTypes ();
@@ -133,7 +131,6 @@ public Collection<Persistable<? extends Serializable>> getCollection(Serializabl
133
131
set .add (item );
134
132
}
135
133
}
136
-
137
134
}
138
135
}
139
136
return set ;
0 commit comments