Class ClassMeta<T>

Type Parameters:
T - The class type of the wrapped class.
All Implemented Interfaces:
Comparable<ClassInfo>, Type, Annotatable

@Bean(properties="innerClass,elementType,keyType,valueType,notABeanReason,initException,beanMeta") public class ClassMeta<T> extends ClassInfoTyped<T>
A wrapper class around the Class object that provides cached information about that class.

Instances of this class can be created through the BeanContext.getClassMeta(Class) method.

The BeanContext class will cache and reuse instances of this class except for the following class types:

  • Arrays
  • Maps with non-Object key/values.
  • Collections with non-Object key/values.

This class is tied to the BeanContext class because it's that class that makes the determination of what is a bean.

  • Method Details

    • findSwap

      protected ObjectSwap<?,?> findSwap(Class<?> c)
    • findUnswap

      protected ObjectSwap<?,?> findUnswap(Class<?> c)
    • canCreateNewBean

      public boolean canCreateNewBean(Object outer)
      Returns true if this class can be instantiated as a bean. Returns false if this is a non-static member class and the outer object does not match the class type of the defining class.
      Parameters:
      outer - The outer class object for non-static member classes. Can be null for non-member or static classes.
      Returns:
      true if a new instance of this bean can be created within the context of the specified outer object.
    • canCreateNewInstance

      public boolean canCreateNewInstance()
      Returns true if this class has a no-arg constructor or invocation handler.
      Returns:
      true if a new instance of this class can be constructed.
    • canCreateNewInstance

      public boolean canCreateNewInstance(Object outer)
      Returns true if this class has a no-arg constructor or invocation handler. Returns false if this is a non-static member class and the outer object does not match the class type of the defining class.
      Parameters:
      outer - The outer class object for non-static member classes. Can be null for non-member or static classes.
      Returns:
      true if a new instance of this class can be created within the context of the specified outer object.
    • canCreateNewInstanceFromString

      public boolean canCreateNewInstanceFromString(Object outer)
      Returns true if this class can call the newInstanceFromString(Object, String) method.
      Parameters:
      outer - The outer class object for non-static member classes. Can be null for non-member or static classes.
      Returns:
      true if this class has a no-arg constructor or invocation handler.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class ClassInfo
    • forEachAnnotation

      public <A extends Annotation> ClassMeta<T> forEachAnnotation(Class<A> type, Predicate<A> filter, Consumer<A> action)
      Performs an action on all matching annotations of the specified type defined on this class or parent classes/interfaces in parent-to-child order.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation to search for.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
      Returns:
      This object.
    • getArg

      public ClassMeta<?> getArg(int index)
      Returns the argument metadata at the specified index if this is an args metadata object.
      Parameters:
      index - The argument index.
      Returns:
      The The argument metadata. Never null.
      Throws:
      BeanRuntimeException - If this metadata object is not a list of arguments, or the index is out of range.
    • getArgs

      public List<ClassMeta<?>> getArgs()
      Returns the argument types of this meta.
      Returns:
      The argument types of this meta, or null if this isn't an array of argument types.
    • getBeanContext

      Returns the BeanContext that created this object.
      Returns:
      The bean context.
    • getBeanMeta

      public BeanMeta<T> getBeanMeta()
      Returns the BeanMeta associated with this class.
      Returns:
      The BeanMeta associated with this class, or null if there is no bean meta associated with this class.
    • getBeanRegistry

      Returns the bean registry for this class.

      This bean registry contains names specified in the @Bean(dictionary) annotation defined on the class, regardless of whether the class is an actual bean. This allows interfaces to define subclasses with type names.

      This is a shortcut for calling getBeanMeta().getBeanRegistry().

      Returns:
      The bean registry for this class, or null if no bean registry is associated with it.
    • getBuilderSwap

      public BuilderSwap<T,?> getBuilderSwap(BeanSession session)
      Returns the builder swap associated with this class.
      Parameters:
      session - The current bean session.
      Returns:
      The builder swap associated with this class, or null if it doesn't exist.
    • getBeanDictionaryName

      Returns the bean dictionary name associated with this class.

      The lexical name is defined by @Bean(typeName).

      Returns:
      The type name associated with this bean class, or null if there is no type name defined or this isn't a bean.
    • getElementType

      For array and Collection types, returns the class type of the components of the array or Collection.
      Returns:
      The element class type, or null if this class is not an array or Collection.
    • getExample

      public T getExample(BeanSession session, JsonParserSession jpSession)
      Returns the example of this class.
      Parameters:
      session - The bean session.
      Required because the example method may take it in as a parameter.
      jpSession - The JSON parser for parsing examples into POJOs.
      Returns:
      The serialized class type, or this object if no swap is associated with the class.
    • getFromMutater

      public <I> Mutater<I,T> getFromMutater(Class<I> c)
      Returns the transform for this class for creating instances from other object types.
      Type Parameters:
      I - The transform-from class.
      Parameters:
      c - The transform-from class.
      Returns:
      The transform, or null if no such transform exists.
    • getImplClassConstructor

      Returns the no-arg constructor for this class based on the Marshalled.implClass() value.
      Parameters:
      conVis - The constructor visibility.
      Returns:
      The no-arg constructor for this class, or null if it does not exist.
    • getInputStreamMutater

      Returns the transform for this class for creating instances from an InputStream.
      Returns:
      The transform, or null if no such transform exists.
    • getKeyType

      public ClassMeta<?> getKeyType()
      For Map types, returns the class type of the keys of the Map.
      Returns:
      The key class type, or null if this class is not a Map.
    • getNameProperty

      Returns the method or field annotated with @NameProperty.
      Returns:
      The method or field annotated with @NameProperty or null if method does not exist.
    • getNotABeanReason

      Returns the reason why this class is not a bean, or null if it is a bean.
      Returns:
      The reason why this class is not a bean, or null if it is a bean.
    • getOptionalDefault

      If this is an Optional, returns an empty optional.

      Note that if this is a nested optional, will recursively create empty optionals.

      Returns:
      An empty optional, or null if this isn't an optional.
    • getParentProperty

      Returns the method or field annotated with @ParentProperty.
      Returns:
      The method or field annotated with @ParentProperty or null if method does not exist.
    • getProperty

      public <T2> Optional<T2> getProperty(String name, Function<ClassMeta<?>,T2> function)
      Returns a lazily-computed, cached property value for this ClassMeta instance.

      This method provides a memoization mechanism for expensive computations. The property value is computed on the first call using the provided function, then cached for subsequent calls with the same property name.

      The function is only invoked once per property name per ClassMeta instance. Subsequent calls with the same name will return the cached value without re-invoking the function.

      Thread Safety:

      This method is thread-safe. If multiple threads call this method simultaneously with the same property name, the function may be invoked multiple times, but only one result will be cached and returned.

      Usage:

      // Compute and cache an expensive property Optional<String> computedValue = classMeta.getProperty("expensiveProperty", cm -> { // Expensive computation that only runs once return performExpensiveComputation(cm); }); // Subsequent calls return cached value Optional<String> cached = classMeta.getProperty("expensiveProperty", cm -> { // This function is NOT called again return performExpensiveComputation(cm); });

      Type Parameters:
      T2 - The type of the property value.
      Parameters:
      name - The unique name identifying this property. Used as the cache key.
      function - The function that computes the property value. Receives this ClassMeta instance as input. Only invoked if the property hasn't been computed yet. Can return null.
      Returns:
      An Optional containing the property value if the function returned a non-null value, otherwise an empty Optional. Never null.
    • getProxyInvocationHandler

      Returns the interface proxy invocation handler for this class.
      Returns:
      The interface proxy invocation handler, or null if it does not exist.
    • getReaderMutater

      Returns the transform for this class for creating instances from a Reader.
      Returns:
      The transform, or null if no such transform exists.
    • getSerializedClassMeta

      Returns the serialized (swapped) form of this class if there is an ObjectSwap associated with it.
      Parameters:
      session - The bean session.
      Required because the swap used may depend on the media type being serialized or parsed.
      Returns:
      The serialized class type, or this object if no swap is associated with the class.
    • getStringMutater

      Returns the transform for this class for creating instances from a String.
      Returns:
      The transform, or null if no such transform exists.
    • getSwap

      public ObjectSwap<T,?> getSwap(BeanSession session)
      Returns the ObjectSwap associated with this class that's the best match for the specified session.
      Parameters:
      session - The current bean session.
      If multiple swaps are associated with a class, only the first one with a matching media type will be returned.
      Returns:
      The ObjectSwap associated with this class, or null if there are no POJO swaps associated with this class.
    • getToMutater

      public <O> Mutater<T,O> getToMutater(Class<O> c)
      Returns the transform for this class for creating instances from other object types.
      Type Parameters:
      O - The transform-to class.
      Parameters:
      c - The transform-from class.
      Returns:
      The transform, or null if no such transform exists.
    • getValueType

      public ClassMeta<?> getValueType()
      For Map types, returns the class type of the values of the Map.
      Returns:
      The value class type, or null if this class is not a Map.
    • hasInputStreamMutater

      public boolean hasInputStreamMutater()
      Returns true if this class has a transform associated with it that allows it to be created from an InputStream.
      Returns:
      true if this class has a transform associated with it that allows it to be created from an InputStream.
    • hasMutaterFrom

      public boolean hasMutaterFrom(Class<?> c)
      Returns true if this class can be instantiated from the specified type.
      Parameters:
      c - The class type to convert from.
      Returns:
      true if this class can be instantiated from the specified type.
    • hasMutaterFrom

      public boolean hasMutaterFrom(ClassMeta<?> c)
      Returns true if this class can be instantiated from the specified type.
      Parameters:
      c - The class type to convert from.
      Returns:
      true if this class can be instantiated from the specified type.
    • hasMutaterTo

      public boolean hasMutaterTo(Class<?> c)
      Returns true if this class can be transformed to the specified type.
      Parameters:
      c - The class type to convert from.
      Returns:
      true if this class can be transformed to the specified type.
    • hasMutaterTo

      public boolean hasMutaterTo(ClassMeta<?> c)
      Returns true if this class can be transformed to the specified type.
      Parameters:
      c - The class type to convert from.
      Returns:
      true if this class can be transformed to the specified type.
    • hasReaderMutater

      public boolean hasReaderMutater()
      Returns true if this class has a transform associated with it that allows it to be created from a Reader.
      Returns:
      true if this class has a transform associated with it that allows it to be created from a Reader.
    • hasStringMutater

      public boolean hasStringMutater()
      Returns true if this class has a transform associated with it that allows it to be created from a String.
      Returns:
      true if this class has a transform associated with it that allows it to be created from a String.
    • isArgs

      public boolean isArgs()
      Returns true if this metadata represents an array of argument types.
      Returns:
      true if this metadata represents an array of argument types.
    • isBean

      public boolean isBean()
      Returns true if this class is a bean.
      Returns:
      true if this class is a bean.
    • isBeanMap

      public boolean isBeanMap()
      Returns true if this class is a subclass of BeanMap.
      Returns:
      true if this class is a subclass of BeanMap.
    • isBoolean

      public boolean isBoolean()
      Returns true if this class is a Boolean.
      Returns:
      true if this class is a Boolean.
    • isByteArray

      public boolean isByteArray()
      Returns true if this class is byte[].
      Returns:
      true if this class is byte[].
    • isCalendar

      public boolean isCalendar()
      Returns true if this class is a Calendar.
      Returns:
      true if this class is a Calendar.
    • isChar

      public boolean isChar()
      Returns true if this class is a Character.
      Returns:
      true if this class is a Character.
    • isCharSequence

      public boolean isCharSequence()
      Returns true if this class is a subclass of CharSequence.
      Returns:
      true if this class is a subclass of CharSequence.
    • isCollection

      public boolean isCollection()
      Returns true if this class is a subclass of Collection.
      Returns:
      true if this class is a subclass of Collection.
    • isCollectionOrArrayOrOptional

      Returns true if this class is a subclass of Collection or is an array or Optional.
      Returns:
      true if this class is a subclass of Collection or is an array or Optional.
    • isDate

      public boolean isDate()
      Returns true if this class is a Date.
      Returns:
      true if this class is a Date.
    • isDateOrCalendar

      public boolean isDateOrCalendar()
      Returns true if this class is a Date or Calendar.
      Returns:
      true if this class is a Date or Calendar.
    • isDateOrCalendarOrTemporal

      public boolean isDateOrCalendarOrTemporal()
      Returns true if this class is a Date or Calendar or Temporal.
      Returns:
      true if this class is a Date or Calendar or Temporal.
    • isDecimal

      public boolean isDecimal()
      Returns true if this class is a subclass of Float or Double.
      Returns:
      true if this class is a subclass of Float or Double.
    • isDelegate

      public boolean isDelegate()
      Returns true if this class implements Delegate, meaning it's a representation of some other object.
      Returns:
      true if this class implements Delegate.
    • isDouble

      public boolean isDouble()
      Returns true if this class is either Double or double.
      Returns:
      true if this class is either Double or double.
    • isFloat

      public boolean isFloat()
      Returns true if this class is either Float or float.
      Returns:
      true if this class is either Float or float.
    • isInputStream

      public boolean isInputStream()
      Returns true if this class is an InputStream.
      Returns:
      true if this class is an InputStream.
    • isInteger

      public boolean isInteger()
      Returns true if this class is either Integer or int.
      Returns:
      true if this class is either Integer or int.
    • isList

      public boolean isList()
      Returns true if this class extends from List.
      Returns:
      true if this class extends from List.
    • isLong

      public boolean isLong()
      Returns true if this class is either Long or long.
      Returns:
      true if this class is either Long or long.
    • isMap

      public boolean isMap()
      Returns true if this class is a subclass of Map.
      Returns:
      true if this class is a subclass of Map.
    • isMapOrBean

      public boolean isMapOrBean()
      Returns true if this class is a subclass of Map or it's a bean.
      Returns:
      true if this class is a subclass of Map or it's a bean.
    • isMethod

      public boolean isMethod()
      Returns true if this class is Method.
      Returns:
      true if this class is Method.
    • isNullable

      public boolean isNullable()
      Returns true if instance of this object can be null.

      Objects can be null, but primitives cannot, except for chars which can be represented by (char)0.

      Returns:
      true if instance of this class can be null.
    • isNumber

      public boolean isNumber()
      Returns true if this class is a subclass of Number.
      Returns:
      true if this class is a subclass of Number.
    • isObject

      public boolean isObject()
      Returns true if this class is Object.
      Returns:
      true if this class is Object.
    • isOptional

      public boolean isOptional()
      Returns true if this class is a subclass of Optional.
      Returns:
      true if this class is a subclass of Optional.
    • isReader

      public boolean isReader()
      Returns true if this class is a Reader.
      Returns:
      true if this class is a Reader.
    • isSet

      public boolean isSet()
      Returns true if this class extends from Set.
      Returns:
      true if this class extends from Set.
    • isShort

      public boolean isShort()
      Returns true if this class is either Short or short.
      Returns:
      true if this class is either Short or short.
    • isString

      public boolean isString()
      Returns true if this class is a String.
      Returns:
      true if this class is a String.
    • isTemporal

      public boolean isTemporal()
      Returns true if this class is a Temporal.
      Returns:
      true if this class is a Temporal.
    • isUri

      public boolean isUri()
      Returns true if this class is a URI or URL.
      Returns:
      true if this class is a URI or URL.
    • mutateFrom

      public T mutateFrom(Object o)
      Transforms the specified object into an instance of this class.
      Parameters:
      o - The object to transform.
      Returns:
      The transformed object.
    • mutateTo

      public <O> O mutateTo(Object o, Class<O> c)
      Transforms the specified object into an instance of this class.
      Type Parameters:
      O - The transform-to class.
      Parameters:
      o - The object to transform.
      c - The class
      Returns:
      The transformed object.
    • mutateTo

      public <O> O mutateTo(Object o, ClassMeta<O> c)
      Transforms the specified object into an instance of this class.
      Type Parameters:
      O - The transform-to class.
      Parameters:
      o - The object to transform.
      c - The class
      Returns:
      The transformed object.
    • newInstance

      Create a new instance of the main class of this declared type.
      Overrides:
      newInstance in class ClassInfo
      Returns:
      A new instance of the object, or null if there is no no-arg constructor on the object.
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • newInstance

      public T newInstance(Object outer) throws ExecutableException
      Same as newInstance() except for instantiating non-static member classes.
      Parameters:
      outer - The instance of the owning object of the member class instance. Can be null if instantiating a non-member or static class.
      Returns:
      A new instance of the object, or null if there is no no-arg constructor on the object.
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • newInstanceFromString

      Create a new instance of the main class of this declared type from a String input.

      In order to use this method, the class must have one of the following methods:

      • public static T valueOf(String in);
      • public static T fromString(String in);
      • public T(String in);
      Parameters:
      outer - The outer class object for non-static member classes. Can be null for non-member or static classes.
      arg - The input argument value.
      Returns:
      A new instance of the object, or null if there is no string constructor on the object.
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • same

      public boolean same(ClassMeta<?> cm)
      Similar to equals(Object) except primitive and Object types that are similar are considered the same. (e.g. boolean == Boolean).
      Parameters:
      cm - The class meta to compare to.
      Returns:
      true if the specified class-meta is equivalent to this one.
    • toString

      public String toString()
      Overrides:
      toString in class ClassInfo
    • toString

      public String toString(boolean simple)
      Same as toString() except use simple class names.
      Parameters:
      simple - Print simple class names only (no package).
      Returns:
      A new string.
    • toString

      public String toString(Object t)
      Converts the specified object to a string.
      Parameters:
      t - The object to convert.
      Returns:
      The object converted to a string, or null if the object was null.
    • getChildObjectSwapForSwap

      protected ObjectSwap<?,?> getChildObjectSwapForSwap(Class<?> normalClass)
      Returns the ObjectSwap where the specified class is the same/subclass of the normal class of one of the child POJO swaps associated with this class.
      Parameters:
      normalClass - The normal class being resolved.
      Returns:
      The resolved ObjectSwap or null if none were found.
    • getChildObjectSwapForUnswap

      protected ObjectSwap<?,?> getChildObjectSwapForUnswap(Class<?> swapClass)
      Returns the ObjectSwap where the specified class is the same/subclass of the swap class of one of the child POJO swaps associated with this class.
      Parameters:
      swapClass - The swap class being resolved.
      Returns:
      The resolved ObjectSwap or null if none were found.
    • hasChildSwaps

      protected boolean hasChildSwaps()
      Returns true if this class or any child classes has a ObjectSwap associated with it.

      Used when transforming bean properties to prevent having to look up transforms if we know for certain that no transforms are associated with a bean property.

      Returns:
      true if this class or any child classes has a ObjectSwap associated with it.
    • toString

      protected StringBuilder toString(StringBuilder sb, boolean simple)
      Appends this object as a readable string to the specified string builder.
      Parameters:
      sb - The string builder to append this object to.
      simple - Print simple class names only (no package).
      Returns:
      The passed-in string builder.