Class B2TypeResolver


  • public class B2TypeResolver
    extends java.lang.Object
    Class used to resolve generic types of a class when supplied with the actual type arguments used to construct an instance of the class. For example, consider the following classes.
    
     class Item<T> {
         T value;
         List<T> values;
     }
    
     class Enclosing {
         Item<String> stringItem;
         Item<Integer> integerItem;
     }
     
    When considering just Item.class, we do not have enough information to resolve the type for .value and .values. However, if we are considering Enclosing.class, then even though it has fields that are Item.class instances, we have the additional context of the type arguments used along with the Item class. This allows us to resolve the types of Enclosing.class' fields. final B2TypeResolver typeResolver = new B2TypeResolver(Enclosing.class); // The following returns ResolvedParameterizedType(Item.class, new Type[]{ String.class }); typeResolver.resolveType(Enclosing.class.getDeclaredFields()[0]);
    • Constructor Summary

      Constructors 
      Constructor Description
      B2TypeResolver​(java.lang.Class<?> clazz)  
      B2TypeResolver​(java.lang.Class<?> clazz, java.lang.reflect.Type[] actualTypeArgumentsOrNull)
      Creates a type resolver for the supplied class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.reflect.Type getType()
      Returns the type this resolver is working over.
      java.lang.reflect.Type resolveType​(java.lang.reflect.Field field)
      Resolve the type of the supplied field.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • B2TypeResolver

        public B2TypeResolver​(java.lang.Class<?> clazz)
      • B2TypeResolver

        public B2TypeResolver​(java.lang.Class<?> clazz,
                              java.lang.reflect.Type[] actualTypeArgumentsOrNull)
        Creates a type resolver for the supplied class. If clazz is a parameterized class, then the code will throw if you do not supply the actual type arguments used with the class, or the actualTypeArguments is the wrong length for the number of type parameters clazz takes. If clazz is not parameterized, then the code will throw if you supply anything other than null or a 0-length array for actualTypeArguments.
    • Method Detail

      • getType

        public java.lang.reflect.Type getType()
        Returns the type this resolver is working over. That is, the class that encloses the fields this object knows how to resolve.
      • resolveType

        public java.lang.reflect.Type resolveType​(java.lang.reflect.Field field)
        Resolve the type of the supplied field. Will throw if field does not belong to the class this B2TypeResolver is for.