8
8
import java .lang .reflect .Type ;
9
9
import java .lang .reflect .TypeVariable ;
10
10
import java .lang .reflect .WildcardType ;
11
- import java .util .HashMap ;
12
11
import java .util .HashSet ;
13
12
import java .util .Map ;
14
13
import java .util .Set ;
21
20
import org .hibernate .validator .internal .metadata .aggregated .ReturnValueMetaData ;
22
21
import org .hibernate .validator .internal .metadata .aggregated .ValidatableParametersMetaData ;
23
22
import org .hibernate .validator .internal .metadata .facets .Cascadable ;
24
- import org .hibernate .validator .internal .properties . Signature ;
23
+ import org .hibernate .validator .internal .metadata . facets . Validatable ;
25
24
import org .hibernate .validator .internal .util .CollectionHelper ;
26
25
27
26
public class PredefinedScopeProcessedBeansTrackingStrategy implements ProcessedBeansTrackingStrategy {
28
27
29
28
private final Map <Class <?>, Boolean > trackingEnabledForBeans ;
30
29
31
- // TODO: signature is just name and parameters so that can clash between different beans.
32
- // with that.. do we even need to track it per signature or since
33
- // we already built the `trackingEnabledForBeans` we can just "inspect" the cascadable as we go
34
- // and check against this `trackingEnabledForBeans` to see if tracking is required ?
35
- private final Map <Signature , Boolean > trackingEnabledForReturnValues ;
36
- private final Map <Signature , Boolean > trackingEnabledForParameters ;
37
-
38
30
public PredefinedScopeProcessedBeansTrackingStrategy (Map <Class <?>, BeanMetaData <?>> rawBeanMetaDataMap ) {
39
- // TODO: build the maps from the information inside the beanMetaDataManager
40
- // There is a good chance we will need a structure with the whole hierarchy of constraint classes.
41
- // That's something we could add to PredefinedScopeBeanMetaDataManager, as we are already doing similar things
42
- // there (see the ClassHierarchyHelper.getHierarchy call).
43
- // In the predefined scope case, we will have the whole hierarchy of constrained classes passed to
44
- // PredefinedScopeBeanMetaDataManager.
45
-
46
31
this .trackingEnabledForBeans = CollectionHelper .toImmutableMap (
47
32
new TrackingEnabledStrategyBuilder ( rawBeanMetaDataMap ).build ()
48
33
);
49
- this .trackingEnabledForReturnValues = CollectionHelper .toImmutableMap ( new HashMap <>() );
50
- this .trackingEnabledForParameters = CollectionHelper .toImmutableMap ( new HashMap <>() );
51
34
}
52
35
53
36
private static class TrackingEnabledStrategyBuilder {
@@ -223,16 +206,13 @@ private static void processSingleCascadable(Cascadable cascadable, Set<Class<?>>
223
206
final ContainerCascadingMetaData containerCascadingMetaData = cascadingMetaData .as ( ContainerCascadingMetaData .class );
224
207
processContainerCascadingMetaData ( containerCascadingMetaData , directCascadedBeanClasses );
225
208
}
226
- else if ( cascadingMetaData instanceof PotentiallyContainerCascadingMetaData potentiallyContainerCascadingMetaData ) {
227
- // if it's a potentially container cascading one, we are "in trouble" as thing can be "almost anything".
228
- // TODO: would it be enough to just take the type as defined ?
229
- // directCascadedBeanClasses.add( (Class<?>) cascadable.getCascadableType() );
230
- //
231
- // TODO: or be much more cautious and just assume that it can be "anything":
209
+ else if ( cascadingMetaData instanceof PotentiallyContainerCascadingMetaData ) {
210
+ // If it's a potentially container cascading one, we are "in trouble" as thing can be "almost anything".
211
+ // Let's be much more cautious and just assume that it can be "anything":
232
212
directCascadedBeanClasses .add ( Object .class );
233
213
}
234
214
else {
235
- // TODO: For now, assume non-container Cascadables are always beans. Truee ???
215
+ // TODO: For now, assume non-container Cascadables are always beans. True ???
236
216
directCascadedBeanClasses .add ( typeToClassToProcess ( cascadable .getCascadableType () ) );
237
217
}
238
218
}
@@ -260,8 +240,8 @@ else if ( typeArgument instanceof WildcardType wildcard ) {
260
240
}
261
241
}
262
242
else {
263
- // TODO: instead of failing, add an Object.class and assume it can be anything ?
264
- throw new UnsupportedOperationException ( typeArgument . getClass (). getSimpleName () + " type argument values are not supported." );
243
+ // In any unexpected case treat things as if they require tracking just to be on the safe side:
244
+ directCascadedBeanClasses . add ( Object . class );
265
245
}
266
246
}
267
247
}
@@ -288,9 +268,7 @@ else if ( type instanceof ParameterizedType parameterizedType ) {
288
268
return typeToClassToProcess ( parameterizedType .getRawType () );
289
269
}
290
270
else {
291
- // TODO: instead of failing, add an Object.class and assume it can be anything ?
292
- // return Object.class;
293
- throw new UnsupportedOperationException ( type .getClass ().getSimpleName () + " type values are not supported." );
271
+ return Object .class ;
294
272
}
295
273
}
296
274
@@ -303,55 +281,27 @@ public boolean isEnabledForBean(Class<?> rootBeanClass, boolean hasCascadables)
303
281
return trackingEnabledForBeans .get ( rootBeanClass );
304
282
}
305
283
306
- @ Override
307
- public boolean isEnabledForReturnValue (Signature signature , boolean hasCascadables ) {
308
- if ( !hasCascadables ) {
309
- return false ;
310
- }
311
-
312
- return trackingEnabledForReturnValues .getOrDefault ( signature , true );
313
- }
314
-
315
284
@ Override
316
285
public boolean isEnabledForReturnValue (ReturnValueMetaData returnValueMetaData ) {
317
- if ( !returnValueMetaData .isCascading () ) {
318
- return false ;
319
- }
320
-
321
- Set <Class <?>> directCascadedBeanClasses = new HashSet <>();
322
- for ( Cascadable cascadable : returnValueMetaData .getCascadables () ) {
323
- processSingleCascadable ( cascadable , directCascadedBeanClasses );
324
- }
325
- for ( Class <?> directCascadedBeanClass : directCascadedBeanClasses ) {
326
- if ( trackingEnabledForBeans .get ( directCascadedBeanClass ) ) {
327
- return true ;
328
- }
329
- }
330
-
331
- return false ;
286
+ return isEnabledForExecutableValidatable ( returnValueMetaData );
332
287
}
333
288
334
289
@ Override
335
- public boolean isEnabledForParameters (Signature signature , boolean hasCascadables ) {
336
- if ( !hasCascadables ) {
337
- return false ;
338
- }
339
-
340
- return trackingEnabledForParameters .getOrDefault ( signature , true );
290
+ public boolean isEnabledForParameters (ValidatableParametersMetaData parametersMetaData ) {
291
+ return isEnabledForExecutableValidatable ( parametersMetaData );
341
292
}
342
293
343
- @ Override
344
- public boolean isEnabledForParameters (ValidatableParametersMetaData parametersMetaData ) {
345
- if ( !parametersMetaData .hasCascadables () ) {
294
+ private boolean isEnabledForExecutableValidatable (Validatable validatable ) {
295
+ if ( !validatable .hasCascadables () ) {
346
296
return false ;
347
297
}
348
298
349
299
Set <Class <?>> directCascadedBeanClasses = new HashSet <>();
350
- for ( Cascadable cascadable : parametersMetaData .getCascadables () ) {
300
+ for ( Cascadable cascadable : validatable .getCascadables () ) {
351
301
processSingleCascadable ( cascadable , directCascadedBeanClasses );
352
302
}
353
303
for ( Class <?> directCascadedBeanClass : directCascadedBeanClasses ) {
354
- if ( trackingEnabledForBeans .get ( directCascadedBeanClass ) ) {
304
+ if ( Boolean . TRUE . equals ( trackingEnabledForBeans .get ( directCascadedBeanClass ) ) ) {
355
305
return true ;
356
306
}
357
307
}
@@ -362,7 +312,5 @@ public boolean isEnabledForParameters(ValidatableParametersMetaData parametersMe
362
312
@ Override
363
313
public void clear () {
364
314
trackingEnabledForBeans .clear ();
365
- trackingEnabledForReturnValues .clear ();
366
- trackingEnabledForParameters .clear ();
367
315
}
368
316
}
0 commit comments