42
42
* declared directly (e.g. field declaration: {@code Outer<String>.Inner field}) and, in this case, direct
43
43
* declaration is used in priority.
44
44
* <p>
45
- * It is impossible to to perform incorrect generics resolution through context api: it will either change context
45
+ * It is impossible to perform incorrect generics resolution through context api: it will either change context
46
46
* automatically or fail with error (with detailed incompatibility explanation).
47
47
*
48
48
* @author Vyacheslav Rusakov
@@ -58,6 +58,10 @@ public abstract class AbstractGenericsContext {
58
58
protected final Map <String , Type > typeGenerics ;
59
59
60
60
public AbstractGenericsContext (final GenericsInfo genericsInfo , final Class <?> type ) {
61
+ if (type .isPrimitive ()) {
62
+ throw new IllegalArgumentException (String .format (
63
+ "Primitive type %s can't be used for generics context building" , type .getSimpleName ()));
64
+ }
61
65
this .genericsInfo = genericsInfo ;
62
66
this .currentType = type ;
63
67
// fail on wrong type
@@ -306,6 +310,8 @@ public Class<?> resolveFieldClass(final Field field) {
306
310
* Check if type containing generics, belonging to different context in current hierarchy and
307
311
* automatically change context to properly resolve generics. Fails when it is impossible to correctly
308
312
* resolve generics (preventing incorrect usage).
313
+ * <p>
314
+ * For primitive type empty list will be returned.
309
315
*
310
316
* @param type type to resolve generics
311
317
* @return resolved generic classes or empty list if type does not use generics
@@ -339,6 +345,8 @@ public List<Class<?>> resolveFieldGenerics(final Field field) {
339
345
* Check if type containing generics, belonging to different context in current hierarchy and
340
346
* automatically change context to properly resolve generics. Fails when it is impossible to correctly
341
347
* resolve generics (preventing incorrect usage).
348
+ * <p>
349
+ * For primitive type null will be returned.
342
350
*
343
351
* @param type type to resolve generic
344
352
* @return first resolved generic or null if type does not use generics
@@ -354,6 +362,8 @@ public Class<?> resolveGenericOf(final Type type) {
354
362
* Use for more informative error message on incorrect usage.
355
363
* <p>
356
364
* Automatically choose correct context or fail if field does not belong to current hierarchy.
365
+ * <p>
366
+ * For primitive field null will be returned.
357
367
*
358
368
* @param field field to resolve generic for
359
369
* @return resolved field generic or null if field does not declare generics
@@ -369,6 +379,8 @@ public Class<?> resolveFieldGeneric(final Field field) {
369
379
* (if T == String).
370
380
* <p>
371
381
* Automatically choose correct context or fail if field does not belong to current hierarchy.
382
+ * <p>
383
+ * If provided type is primitive then it will be returned as is.
372
384
*
373
385
* @param field field to resolve generics for
374
386
* @return field type with resolved generic variables
@@ -390,6 +402,8 @@ public Type resolveFieldType(final Field field) {
390
402
* Check if type containing generics, belonging to different context in current hierarchy and
391
403
* automatically change context to properly resolve generics. Fails when it is impossible to correctly
392
404
* resolve generics (preventing incorrect usage).
405
+ * <p>
406
+ * If provided type is primitive then it will be returned as is.
393
407
*
394
408
* @param type type to resolve named generics in
395
409
* @return type without named generics (replaced by known actual types)
@@ -404,13 +418,15 @@ public Type resolveType(final Type type) {
404
418
* {@code ParameterizedType Map<String, List<T>>} would become
405
419
* {@code [Class String, ParameterizedType List<String>]} (assumed generic T is defined as String).
406
420
* <p>
407
- * Note: if type is Class then return raw generics definition (for consistency).
421
+ * Note: if type is {@code Class} then return raw generics definition (for consistency).
408
422
* <p>
409
423
* Useful when complete generic types are required.
410
424
* <p>
411
425
* Check if type containing generics, belonging to different context in current hierarchy and
412
426
* automatically change context to properly resolve generics. Fails when it is impossible to correctly
413
427
* resolve generics (preventing incorrect usage).
428
+ * <p>
429
+ * For primitive type empty list would be returned.
414
430
*
415
431
* @param type type to return resolved generics of
416
432
* @return type generics without variables
@@ -494,6 +510,8 @@ public String toStringType(final Type type) {
494
510
* <p>
495
511
* Note that, in contrast to direct resolution {@code GenericsResolver.resolve(B.class)}, actual root generic
496
512
* would be counted for hierarchy resolution.
513
+ * <p>
514
+ * If field declares primitive type then wrapper class would be used instead.
497
515
*
498
516
* @param field field in current class hierarchy to resolve type from (may be field from superclass, relative to
499
517
* currently selected, in this case context type will be automatically switched)
@@ -549,6 +567,9 @@ public GenericsContext fieldTypeAs(final Field field, final Class<?> asType) {
549
567
* Returned context holds reference to original (root) context: {@link GenericsContext#rootContext()}.
550
568
* <p>
551
569
* Ignored types, used for context creation, are counted (will also be ignored for inlying context building).
570
+ * <p>
571
+ * If provided type is primitive then wrapper will be used instead (for example, {@link Integer} instead of
572
+ * {@link int}).
552
573
*
553
574
* @param type type to resolve hierarchy from (it must be generified type, resolved in current class)
554
575
* @return generics context of type (inlying context)
@@ -573,6 +594,9 @@ public GenericsContext fieldTypeAs(final Field field, final Class<?> asType) {
573
594
* are provided for simpler navigation: {@link #fieldTypeAs(Field, Class)},
574
595
* {@link MethodGenericsContext#returnTypeAs(Class)}, {@link MethodGenericsContext#parameterTypeAs(int, Class)}
575
596
* and {@link ConstructorGenericsContext#parameterTypeAs(int, Class)}.
597
+ * <p>
598
+ * If provided type is primitive then wrapper will be used instead (for example, {@link Integer} instead of
599
+ * {@link int}).
576
600
*
577
601
* @param type type to resolve actual generics from (it must be generified type, resolved in current class)
578
602
* @param asType required target type to build generics context for (must include declared type as base class)
0 commit comments