Package iu.type

Class TypeTemplate<D,T>

Type Parameters:
D - declaring type
T - raw or generic type
All Implemented Interfaces:
IuAnnotatedElement, IuDeclaredElement<D>, IuNamedElement<D>, IuParameterizedElement, IuType<D,T>, ParameterizedFacade

final class TypeTemplate<D,T> extends DeclaredElementBase<D,Class<T>> implements IuType<D,T>, ParameterizedFacade
Represents the internal structure of a TypeFacade.

Each template is a standalone representation of a single generic type, potentially paired with a raw template representing its erasure.

Note that TypeTemplate is not sterotyped as a hash key, but IuType is. This hash key behavior comes from same-instance identity default Object.hashCode() and Object.equals(Object) implementations of instances managed by TypeFactory. Since ClassLoading data is loaded exactly once and remains static once loaded, type introspection instances match that load-once static behavior. It is expected that each raw type (primitive, class, interface, enum, record, etc) has exactly one TypeTemplate instance, and that each TypeTemplate instance backed by a generic type contains a TypeFacade backed by a singleton raw type instances representing its type erasure. This behavior mirrors the hash key behavior of Type, so internal checks for equality (and inequality) may use == (and !=).

Note also that Java does not constrain the number of Type instances, and considers those instances other than Class to be disposable. TypeFactory does not manage TypeTemplate instances for generic type markers once returned to the application.

Initialization Order

Type templates are initialized in two phases, managed by TypeFactory.

  1. Declared elements
  2. Inherited elements

Declared Elements

Resolved in order upon instantiation:

  1. ElementBase(Consumer) preInitHook binds raw Class instances to TypeFactory#RAW_TYPES
  2. AnnotatedElementBase(AnnotatedElement, Consumer) binds AnnotatedElementBase.annotatedElement
  3. DeclaredElementBase(AnnotatedElement, Consumer, Type, TypeTemplate) binds:
  4. Apply actual type arguments from ParameterizedType
  5. erase()
  6. constructors()

Inherited Elements

Resolved by sealHierarchy(Iterable), order incidental

  • Constructor Details

    • TypeTemplate

      TypeTemplate(Class<T> rawClass, Consumer<TypeTemplate<?,?>> preInitHook)
      Raw class constructor intended for use only by TypeFactory.
      Parameters:
      rawClass - raw class
      preInitHook - receives a handle to this after binding the annotated element but before initializing and members
    • TypeTemplate

      TypeTemplate(Consumer<TypeTemplate<?,?>> preInitHook, Type type, TypeTemplate<D,T> erasedType)
      Generic type constructor intended for use only by TypeFactory.
      Parameters:
      preInitHook - receives a handle to this after binding the annotated element but before initializing and members
      type - generic type; must not be a class
      erasedType - pre-calculated raw type template; a TypeTemplate cannot be created for a generic type without a fully-formed instance of its type erasure, provided as an argument to this parameter
  • Method Details

    • seal

      final void seal() throws UnsupportedOperationException
      Unsupported, use sealHierarchy(Iterable) to provide hierarchy when sealing.
      Overrides:
      seal in class ElementBase
      Throws:
      UnsupportedOperationException - when invoked
    • sealHierarchy

      void sealHierarchy(Iterable<? extends IuType<?,? super T>> hierarchy)
      Seals hierarchy() and resolves inherited elements.
      Parameters:
      hierarchy - Resolved type hierarchy
    • subscribe

      public Runnable subscribe(InstanceReference<T> instanceReference)
      Description copied from interface: IuType
      Subscribes a new instance reference.
      Specified by:
      subscribe in interface IuType<D,T>
      Parameters:
      instanceReference - will accept all observed instances until unsubscribed.
      Returns:
      thunk for unsubscribing the reference
    • observe

      public void observe(T instance)
      Description copied from interface: IuType
      Observes a new instance.

      Observing an instance registers it with the implementation module as an available target for type introspection, for example, for resource binding. Implementors of InstanceReference may use IuType.subscribe(InstanceReference) to be notified when new instances are observed.

      Once all InstanceReferences have been notified, all methods annotated by @PostConstruct will be invoked on the instance.

      Observing an instance that is already observed has no effect, nor does observing an instance of a type that has no subscribers. All instances provided via IuExecutable.exec(Object...) are observed automatically. This method is a no-op for those instances, however it is up to implementors to ensure @PostConstruct methods do not result in errors or repeat initialization steps when invoked repeatedly.

      Specified by:
      observe in interface IuType<D,T>
      Parameters:
      instance - to observe
    • destroy

      public void destroy(T instance)
      Description copied from interface: IuType
      Destroys an instance.

      Invokes all @PreDestroy methods on an instance. If the instance was observed, its state will be reverted and no futures actions will be taken on it.

      This method may be invoked to close the lifecycle of instances created via IuExecutable.exec(Object...).

      Destroying an instance that is already destroyed should have no effect, but it is up to implementors to ensure @PreDestroy methods do not result in errors or other side-effects when invoked repeatedly.

      Specified by:
      destroy in interface IuType<D,T>
      Parameters:
      instance - to destroy
    • typeParameters

      public Map<String,TypeFacade<?,?>> typeParameters()
      Description copied from interface: IuParameterizedElement
      Gets type parameters by name.
      Specified by:
      typeParameters in interface IuParameterizedElement
      Specified by:
      typeParameters in interface ParameterizedFacade
      Returns:
      map of type parameter facades
    • name

      public String name()
      Description copied from interface: IuNamedElement
      Gets the name.
      Specified by:
      name in interface IuNamedElement<D>
      Returns:
      name
    • reference

      public IuTypeReference<T,?> reference()
      Description copied from interface: IuType
      Gets the reference used to obtain this type.
      Specified by:
      reference in interface IuType<D,T>
      Returns:
      type reference
    • deref

      public Type deref()
      Description copied from interface: IuType
      Gets the generic type.
      Specified by:
      deref in interface IuType<D,T>
      Returns:
      generic type
    • erase

      public IuType<D,T> erase()
      Description copied from interface: IuType
      Gets the erased facade, which describing the Class representing the erasure of the generic type.

      The IuType.deref() of the erased facade must return a Class.

      Specified by:
      erase in interface IuType<D,T>
      Returns:
      erased type facade
      See Also:
    • erasedClass

      public Class<T> erasedClass()
      Description copied from interface: IuType
      Get the type erasure class.

      Shorthand for IuType.erase().IuType.deref()

      Specified by:
      erasedClass in interface IuType<D,T>
      Returns:
      type erasure class
      See Also:
    • referTo

      public IuType<?,? super T> referTo(Type referentType)
      Description copied from interface: IuType
      Refers to a type in the the described type's hierarchy.

      When the referent type declares type parameters, the resolved generic types associated with those parameters are described by the returned facade.

      Specified by:
      referTo in interface IuType<D,T>
      Parameters:
      referentType - type to refer to
      Returns:
      referent facade
    • enclosedTypes

      public Iterable<TypeFacade<T,?>> enclosedTypes()
      Description copied from interface: IuType
      Gets enclosed types.
      Specified by:
      enclosedTypes in interface IuType<D,T>
      Returns:
      enclosed types
    • constructors

      public Iterable<? extends IuConstructor<T>> constructors()
      Description copied from interface: IuType
      Gets all constructors defined by this type.
      Specified by:
      constructors in interface IuType<D,T>
      Returns:
      constructors
    • hierarchy

      public Iterable<TypeFacade<?,? super T>> hierarchy()
      Description copied from interface: IuType
      Iterates the type hierarchy, from most specific to least specific.
      1. All Class.getGenericInterfaces()
      2. Class.getGenericSuperclass()
      3. Iterate IuType.hierarchy() until Object is reached

      This type described by this facade is not included. Object is always the last element.

      Specified by:
      hierarchy in interface IuType<D,T>
      Returns:
      inherited and extended types
    • field

      public <F> FieldFacade<? super T,F> field(String name)
      Description copied from interface: IuType
      Gets a field declared by this type.

      When a private field has the same name as a different field declared by a super class, the "inherited" field is shadowed by this method. To retrieve all fields, including those shadowed by a superclass, use IuType.fields().

      Specified by:
      field in interface IuType<D,T>
      Type Parameters:
      F - field type
      Parameters:
      name - field name
      Returns:
      field
    • fields

      public Iterable<FieldFacade<? super T,?>> fields()
      Description copied from interface: IuType
      Gets all fields defined by this type, followed by all fields defined by all types in this type's hierarchy, in IuType.hierarchy() order.
      Specified by:
      fields in interface IuType<D,T>
      Returns:
      fields declared by this type and its hierarchy, in this followed by IuType.hierarchy() order
    • property

      public <P> PropertyFacade<? super T,P> property(String name)
      Description copied from interface: IuType
      Gets a property declared by this type.
      Specified by:
      property in interface IuType<D,T>
      Type Parameters:
      P - property type
      Parameters:
      name - property name
      Returns:
      property
    • properties

      public Iterable<PropertyFacade<? super T,?>> properties()
      Description copied from interface: IuType
      Gets all properties defined by this type, followed by all properties defined by all types in this type's hierarchy, in IuType.hierarchy() order.
      Specified by:
      properties in interface IuType<D,T>
      Returns:
      properties declared by this type and its hierarchy, in this followed by IuType.hierarchy() order
    • methods

      public Iterable<MethodFacade<? super T,?>> methods()
      Description copied from interface: IuType
      Gets all methods defined by this type.

      The result iterates all methods declared on all classes in the type erasure's hierarchy with private erasure for duplicately defined methods.

      Specified by:
      methods in interface IuType<D,T>
      Returns:
      methods
    • toString

      public String toString()
      Overrides:
      toString in class Object