Class ClassMeta<T>

java.lang.Object
org.apache.juneau.ClassMeta<T>
Type Parameters:
T - The class type of the wrapped class.
All Implemented Interfaces:
Type

@Bean(properties="innerClass,classCategory,elementType,keyType,valueType,notABeanReason,initException,beanMeta") public final class ClassMeta<T> extends Object implements Type
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.

See Also:
  • Method Details

    • getInfo

      public ClassInfo getInfo()
      Returns the ClassInfo wrapper for the underlying class.
      Returns:
      The ClassInfo wrapper for the underlying class, never null.
    • getBeanTypePropertyName

      Returns the type property name associated with this class and subclasses.

      If null, "_type" should be assumed.

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

      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.
    • 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.

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

      public org.apache.juneau.ClassMeta.ClassCategory getClassCategory()
      Returns the category of this class.
      Returns:
      The category of this class.
    • isAssignableFrom

      public boolean isAssignableFrom(Class<?> c)
      Returns true if this class is a superclass of or the same as the specified class.
      Parameters:
      c - The comparison class.
      Returns:
      true if this class is a superclass of or the same as the specified class.
    • isInstanceOf

      public boolean isInstanceOf(Class<?> c)
      Returns true if this class is a subclass of or the same as the specified class.
      Parameters:
      c - The comparison class.
      Returns:
      true if this class is a subclass of or the same as the specified class.
    • 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.
    • 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.
    • findNoArgConstructor

      protected static <T> Constructor<? extends T> findNoArgConstructor(Class<?> c, Visibility v)
      Locates the no-arg constructor for the specified class.

      Constructor must match the visibility requirements specified by parameter 'v'. If class is abstract, always returns null. Note that this also returns the 1-arg constructor for non-static member classes.

      Type Parameters:
      T - The class from which to locate the no-arg constructor.
      Parameters:
      c - The class from which to locate the no-arg constructor.
      v - The minimum visibility.
      Returns:
      The constructor, or null if no no-arg constructor exists with the required visibility.
    • getInnerClass

      public Class<T> getInnerClass()
      Returns the Class object that this class type wraps.
      Returns:
      The wrapped class object.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • is

      public boolean is(Class<?> value)
      Returns true if the specified class is an exact match for this metadata.
      Parameters:
      value - The value to check against.
      Returns:
      true if the specified class is an exact match for this metadata.
    • isChildOf

      public boolean isChildOf(Class<?> c)
      Returns true if this metadata represents the specified type.
      Parameters:
      c - The class to test against.
      Returns:
      true if this metadata represents the specified type.
    • 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.
    • isBeanMap

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

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

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

      public boolean isCollectionOrArray()
      Returns true if this class is a subclass of Collection or is an array.
      Returns:
      true if this class is a subclass of Collection or is an array.
    • 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.
    • isSet

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

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

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

      public boolean isClass()
      Returns true if this class is Class.
      Returns:
      true if this class is Class.
    • isMethod

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

      public boolean isEnum()
      Returns true if this class is an Enum.
      Returns:
      true if this class is an Enum.
    • isArray

      public boolean isArray()
      Returns true if this class is an array.
      Returns:
      true if this class is an array.
    • isBean

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

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

      public boolean isNotObject()
      Returns true if this class is not Object.
      Returns:
      true if this class is not Object.
    • isNumber

      public boolean isNumber()
      Returns true if this class is a subclass of Number.
      Returns:
      true if this class is a subclass of Number.
    • 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.
    • isFloat

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

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

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

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

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

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

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

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

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

      public boolean isPrimitive()
      Returns true if this class is a primitive.
      Returns:
      true if this class is a primitive.
    • 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.
    • isDate

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

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

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

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

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

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

      public boolean isVoid()
      Returns true if this class is Void or void.
      Returns:
      true if this class is Void or void.
    • 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.
    • getArgs

      public 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.
    • 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.
    • 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.
    • isAbstract

      public boolean isAbstract()
      Returns true if this class is abstract.
      Returns:
      true if this class is abstract.
    • isMemberClass

      public boolean isMemberClass()
      Returns true if this class is an inner class.
      Returns:
      true if this class is an inner class.
    • getPublicMethods

      All public methods on this class including static methods.

      Keys are method signatures.

      Returns:
      The public methods on this class.
    • 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.
    • 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.
    • 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.
    • getConstructor

      Returns the no-arg constructor for this class.
      Returns:
      The no-arg constructor for this class, or null if it does not exist.
    • 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.
    • getProxyInvocationHandler

      Returns the interface proxy invocation handler for this class.
      Returns:
      The interface proxy invocation handler, or null if it does not exist.
    • 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.
    • 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.
    • 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.
    • getNameProperty

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

      Returns the method or field annotated with @ParentProperty.
      Returns:
      The method or field annotated with @ParentProperty 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.
    • getInitException

      Returns any exception that was throw in the init() method.
      Returns:
      The cached exception.
    • getBeanContext

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

      Returns the default value for primitives such as int or Integer.
      Returns:
      The default value, or null if this class type is not a primitive.
    • 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.
    • 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.
    • 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.
    • newInstance

      Create a new instance of the main class of this declared type.
      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.
    • 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 Object
    • 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

      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.
    • isInstance

      public boolean isInstance(Object o)
      Returns true if the specified object is an instance of this class.

      This is a simple comparison on the base class itself and not on any generic parameters.

      Parameters:
      o - The object to check.
      Returns:
      true if the specified object is an instance of this class.
    • getFullName

      public String getFullName()
      Returns a readable name for this class (e.g. "java.lang.String", "boolean[]").
      Returns:
      The readable name for this class.
    • getName

      public String getName()
      Shortcut for calling Class.getName() on the inner class of this metadata.
      Returns:
      The name of the inner class.
    • getSimpleName

      Shortcut for calling Class.getSimpleName() on the inner class of this metadata.
      Returns:
      The simple name of the inner class.
    • 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.
    • getReaderMutater

      Returns the transform for this class for creating instances from a Reader.
      Returns:
      The transform, or null if no such transform exists.
    • 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.
    • getInputStreamMutater

      Returns the transform for this class for creating instances from an InputStream.
      Returns:
      The transform, or null if no such transform exists.
    • 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.
    • getStringMutater

      Returns the transform for this class for creating instances from a String.
      Returns:
      The transform, or null if no such transform exists.
    • 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.
    • 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.
    • 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.
    • 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.
    • hasAnnotation

      public boolean hasAnnotation(Class<? extends Annotation> a)
      Shortcut for calling getInnerClass().getAnnotation(a) != null.
      Parameters:
      a - The annotation to check for.
      Returns:
      true if the inner class has the annotation.
    • getLastAnnotation

      public <A extends Annotation> A getLastAnnotation(Class<A> a)
      Shortcut for calling getInnerClass().getAnnotation(a).
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      a - The annotation to retrieve.
      Returns:
      The specified annotation, or null if the class does not have the specified annotation.
    • 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.
    • firstAnnotation

      public <A extends Annotation> Optional<A> firstAnnotation(Class<A> type, Predicate<A> filter)
      Returns the first matching annotation 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 annotation should be used. Can be null.
      Returns:
      This object.
    • lastAnnotation

      public <A extends Annotation> Optional<A> lastAnnotation(Class<A> type, Predicate<A> filter)
      Returns the last matching annotation 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 annotation should be used. Can be null.
      Returns:
      This object.
    • getProperty

      public <T2> Optional<T2> getProperty(String name, Function<ClassMeta<?>,T2> function)
      Returns a calculated property on this context.
      Type Parameters:
      T2 - The type to convert the property to.
      Parameters:
      name - The name of the property.
      function - The function used to create this property.
      Returns:
      The property value. Never null.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object