15
15
*/
16
16
package org .springframework .data .jpa .mapping ;
17
17
18
- import java .beans .PropertyDescriptor ;
19
18
import java .lang .annotation .Annotation ;
20
- import java .lang .reflect .Field ;
21
19
import java .util .Collection ;
22
20
import java .util .Collections ;
23
21
import java .util .HashSet ;
49
47
import org .springframework .data .mapping .model .Property ;
50
48
import org .springframework .data .mapping .model .SimpleTypeHolder ;
51
49
import org .springframework .data .util .ClassTypeInformation ;
50
+ import org .springframework .data .util .Optionals ;
52
51
import org .springframework .data .util .TypeInformation ;
53
52
import org .springframework .util .Assert ;
54
53
@@ -91,8 +90,8 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
91
90
UPDATEABLE_ANNOTATIONS = Collections .unmodifiableSet (annotations );
92
91
}
93
92
94
- private final Boolean usePropertyAccess ;
95
- private final TypeInformation <?> associationTargetType ;
93
+ private final Optional < Boolean > usePropertyAccess ;
94
+ private final Optional < TypeInformation <?> > associationTargetType ;
96
95
private final boolean updateable ;
97
96
private final JpaMetamodel metamodel ;
98
97
@@ -112,7 +111,7 @@ public JpaPersistentPropertyImpl(Metamodel metamodel, Property property,
112
111
Assert .notNull (metamodel , "Metamodel must not be null!" );
113
112
114
113
this .usePropertyAccess = detectPropertyAccess ();
115
- this .associationTargetType = isAssociation () ? detectAssociationTargetType () : null ;
114
+ this .associationTargetType = detectAssociationTargetType ();
116
115
this .updateable = detectUpdatability ();
117
116
this .metamodel = new JpaMetamodel (metamodel );
118
117
}
@@ -123,7 +122,7 @@ public JpaPersistentPropertyImpl(Metamodel metamodel, Property property,
123
122
*/
124
123
@ Override
125
124
public Class <?> getActualType () {
126
- return associationTargetType == null ? super . getActualType () : associationTargetType . getType ();
125
+ return associationTargetType . isPresent () ? associationTargetType . get (). getType () : super . getActualType ();
127
126
}
128
127
129
128
/*
@@ -132,8 +131,8 @@ public Class<?> getActualType() {
132
131
*/
133
132
@ Override
134
133
public Iterable <? extends TypeInformation <?>> getPersistentEntityType () {
135
- return associationTargetType == null ? super . getPersistentEntityType ( )
136
- : Collections . singleton ( associationTargetType );
134
+ return associationTargetType . isPresent () ? Collections . singleton ( associationTargetType . get () )
135
+ : super . getPersistentEntityType ( );
137
136
}
138
137
139
138
/*
@@ -142,14 +141,7 @@ public Iterable<? extends TypeInformation<?>> getPersistentEntityType() {
142
141
*/
143
142
@ Override
144
143
public boolean isIdProperty () {
145
-
146
- for (Class <? extends Annotation > annotation : ID_ANNOTATIONS ) {
147
- if (isAnnotationPresent (annotation )) {
148
- return true ;
149
- }
150
- }
151
-
152
- return false ;
144
+ return ID_ANNOTATIONS .stream ().anyMatch (it -> isAnnotationPresent (it ));
153
145
}
154
146
155
147
/*
@@ -168,17 +160,11 @@ public boolean isEntity() {
168
160
@ Override
169
161
public boolean isAssociation () {
170
162
171
- for (Class <? extends Annotation > annotationType : ASSOCIATION_ANNOTATIONS ) {
172
- if (findAnnotation (annotationType ) != null ) {
173
- return true ;
174
- }
175
- }
176
-
177
- if (getType ().isAnnotationPresent (Embeddable .class )) {
163
+ if (ASSOCIATION_ANNOTATIONS .stream ().anyMatch (it -> findAnnotation (it ).isPresent ())) {
178
164
return true ;
179
165
}
180
166
181
- return false ;
167
+ return getType (). isAnnotationPresent ( Embeddable . class ) ;
182
168
}
183
169
184
170
/*
@@ -205,7 +191,7 @@ protected Association<JpaPersistentProperty> createAssociation() {
205
191
*/
206
192
@ Override
207
193
public boolean usePropertyAccess () {
208
- return usePropertyAccess != null ? usePropertyAccess : super .usePropertyAccess ();
194
+ return usePropertyAccess . orElseGet (() -> super .usePropertyAccess () );
209
195
}
210
196
211
197
/*
@@ -234,72 +220,61 @@ public boolean isWritable() {
234
220
*
235
221
* @return
236
222
*/
237
- private Boolean detectPropertyAccess () {
223
+ private Optional < Boolean > detectPropertyAccess () {
238
224
239
225
Optional <org .springframework .data .annotation .AccessType > accessType = findAnnotation (
240
226
org .springframework .data .annotation .AccessType .class );
241
227
242
228
if (accessType .isPresent ()) {
243
- return Type .PROPERTY .equals (accessType . get (). value ());
229
+ return accessType . map ( it -> Type .PROPERTY .equals (it . value () ));
244
230
}
245
231
246
232
Optional <Access > access = findAnnotation (Access .class );
247
233
248
234
if (access .isPresent ()) {
249
- return AccessType .PROPERTY .equals (access . get (). value ());
235
+ return access . map ( it -> AccessType .PROPERTY .equals (it . value () ));
250
236
}
251
237
252
238
accessType = findPropertyOrOwnerAnnotation (org .springframework .data .annotation .AccessType .class );
253
239
254
240
if (accessType .isPresent ()) {
255
- return Type .PROPERTY .equals (accessType . get (). value ());
241
+ return accessType . map ( it -> Type .PROPERTY .equals (it . value () ));
256
242
}
257
243
258
244
access = findPropertyOrOwnerAnnotation (Access .class );
259
- return access .map (t -> AccessType .PROPERTY .equals (t .value ())). orElse ( null ) ;
245
+ return access .map (t -> AccessType .PROPERTY .equals (t .value ()));
260
246
}
261
247
262
248
/**
263
249
* Inspects the association annotations on the property and returns the target entity type if specified.
264
250
*
265
251
* @return
266
252
*/
267
- private TypeInformation <?> detectAssociationTargetType () {
268
-
269
- for (Class <? extends Annotation > associationAnnotation : ASSOCIATION_ANNOTATIONS ) {
270
-
271
- Optional <? extends Annotation > annotation = findAnnotation (associationAnnotation );
272
- if (annotation .isPresent ()) {
253
+ private Optional <TypeInformation <?>> detectAssociationTargetType () {
273
254
274
- Object targetEntity = AnnotationUtils .getValue (annotation .get (), "targetEntity" );
275
-
276
- if (targetEntity != null && !void .class .equals (targetEntity )) {
277
- return ClassTypeInformation .from ((Class <?>) targetEntity );
278
- }
279
- }
255
+ if (!isAssociation ()) {
256
+ return Optional .empty ();
280
257
}
281
258
282
- return null ;
259
+ return ASSOCIATION_ANNOTATIONS .stream ()//
260
+ .flatMap (it -> Optionals .toStream (findAnnotation (it )))//
261
+ .map (it -> AnnotationUtils .getValue (it , "targetEntity" ))//
262
+ .filter (it -> it != null && !void .class .equals (it ))//
263
+ .map (it -> (Class <?>) it )//
264
+ .findFirst ().map (it -> (TypeInformation <?>) ClassTypeInformation .from (it ));
283
265
}
284
266
285
267
/**
286
- * Checks whether {@code updateable } attribute of any of the {@link #UPDATEABLE_ANNOTATIONS} is configured to
268
+ * Checks whether {@code updatable } attribute of any of the {@link #UPDATEABLE_ANNOTATIONS} is configured to
287
269
* {@literal true}.
288
270
*
289
271
* @return
290
272
*/
291
273
private final boolean detectUpdatability () {
292
274
293
- for (Class <? extends Annotation > annotationType : UPDATEABLE_ANNOTATIONS ) {
294
-
295
- Optional <? extends Annotation > annotation = findAnnotation (annotationType );
296
-
297
- if (annotation .isPresent () && AnnotationUtils .getValue (annotation .get (), "updatable" ).equals (Boolean .FALSE )) {
298
- return false ;
299
- }
300
- }
301
-
302
- return true ;
275
+ return !UPDATEABLE_ANNOTATIONS .stream ()//
276
+ .flatMap (it -> Optionals .toStream (findAnnotation (it )))//
277
+ .map (it -> AnnotationUtils .getValue (it , "updatable" ))//
278
+ .anyMatch (it -> it .equals (Boolean .FALSE ));
303
279
}
304
-
305
280
}
0 commit comments