Class ClassInfo

java.lang.Object
org.apache.juneau.commons.reflect.ElementInfo
org.apache.juneau.commons.reflect.ClassInfo
All Implemented Interfaces:
Comparable<ClassInfo>, Type, Annotatable
Direct Known Subclasses:
ClassInfoTyped

public class ClassInfo extends ElementInfo implements Annotatable, Type, Comparable<ClassInfo>
Lightweight utility class for introspecting information about a class.

Provides various convenience methods for introspecting fields/methods/annotations that aren't provided by the standard Java reflection APIs.

Objects are designed to be lightweight to create and threadsafe.

Example:

// Wrap our class inside a ClassInfo. ClassInfo classInfo = ClassInfo.of(MyClass.class); // Get all methods in parent-to-child order, sorted alphabetically per class. for (MethodInfo methodInfo : classInfo.getAllMethods()) { // Do something with it. } // Get all class-level annotations in parent-to-child order. for (MyAnnotation annotation : classInfo.getAnnotations(MyAnnotation.class)) { // Do something with it. }

  • Field Details

    • OBJECT

      public static final ClassInfo OBJECT
      Reusable ClassInfo for Object class.
  • Constructor Details

    • ClassInfo

      protected ClassInfo(Class<?> inner, Type innerType)
      Constructor.
      Parameters:
      inner - The class type.
      innerType - The generic type (if parameterized type).
  • Method Details

    • of

      public static ClassInfo of(Class<?> inner, Type innerType)
      Returns a class info wrapper around the specified class type.
      Parameters:
      inner - The class type.
      innerType - The generic type (if parameterized type).
      Returns:
      The constructed class info.
    • of

      public static <T> ClassInfoTyped<T> of(Class<T> inner)
      Returns a class info wrapper around the specified class type.
      Type Parameters:
      T - The class type.
      Parameters:
      inner - The class type.
      Returns:
      The constructed class info.
    • of

      public static ClassInfo of(Object object)
      Same as using the constructor, but operates on an object instance.
      Parameters:
      object - The class instance.
      Returns:
      The constructed class info.
    • of

      public static ClassInfo of(Type innerType)
      Returns a class info wrapper around the specified class type.
      Parameters:
      innerType - The class type.
      Returns:
      The constructed class info.
    • ofProxy

      public static ClassInfo ofProxy(Object object)
      Same as of(Object) but attempts to deproxify the object if it's wrapped in a CGLIB proxy.
      Parameters:
      object - The class instance.
      Returns:
      The constructed class info.
    • appendNameFormatted

      public StringBuilder appendNameFormatted(StringBuilder sb, ClassNameFormat nameFormat, boolean includeTypeParams, char separator, ClassArrayFormat arrayFormat)
      Appends a formatted class name to a StringBuilder with configurable options.

      This is the core implementation method used by all other name formatting methods. Using this method directly avoids String allocations when building complex strings. The method is recursive to handle nested generic type parameters.

      Example:

      StringBuilder sb = new StringBuilder(); sb.append("Class: "); ClassInfo.of(HashMap.class).appendFormattedName(sb, ClassNameFormat.FULL, true, '$', ClassArrayFormat.BRACKETS); // sb now contains: "Class: java.util.HashMap"

      Parameters:
      sb - The StringBuilder to append to.
      nameFormat - Controls which parts of the class name to include (package, outer classes).
      includeTypeParams - If true, include generic type parameters recursively.
      separator - Character to use between outer and inner class names.
      Ignored when nameFormat is ClassNameFormat.SIMPLE.
      arrayFormat - How to format array dimensions.
      Returns:
      The same StringBuilder for method chaining.
    • arrayType

      public ClassInfo arrayType()
      Returns a ClassInfo for an array type whose component type is this class.
      Returns:
      A ClassInfo representing an array type whose component type is this class.
    • asSubclass

      public <U> ClassInfo asSubclass(Class<U> clazz)
      Casts this ClassInfo object to represent a subclass of the class represented by the specified class object.
      Type Parameters:
      U - The type to cast to.
      Parameters:
      clazz - The class of the type to cast to.
      Returns:
      This ClassInfo object, cast to represent a subclass of the specified class object.
      Throws:
      ClassCastException - If this class is not assignable to the specified class.
    • canAcceptArg

      public boolean canAcceptArg(Object child)
      Returns true if this type can be used as a parameter for the specified object.

      For null values, returns true unless this type is a primitive (since primitives cannot accept null values in Java).

      Examples:
      • ClassInfo.of(String.class).canAcceptArg("foo") - returns true
      • ClassInfo.of(String.class).canAcceptArg(null) - returns true
      • ClassInfo.of(int.class).canAcceptArg(5) - returns true
      • ClassInfo.of(int.class).canAcceptArg(null) - returns false (primitives can't be null)
      • ClassInfo.of(Integer.class).canAcceptArg(null) - returns true
      Parameters:
      child - The argument to check.
      Returns:
      true if this type can be used as a parameter for the specified object.
    • cast

      public <T> T cast(Object obj)
      Casts an object to the class represented by this ClassInfo object.
      Type Parameters:
      T - The type to cast to.
      Parameters:
      obj - The object to be cast.
      Returns:
      The object after casting, or null if obj is null.
      Throws:
      ClassCastException - If the object is not null and is not assignable to this class.
    • componentType

      Returns the component type of this class if it is an array type.

      This is equivalent to Class.getComponentType() but returns a ClassInfo instead. Note that getComponentType() also exists and returns the base component type for multi-dimensional arrays.

      Returns:
      The ClassInfo representing the component type, or null if this class does not represent an array type.
    • descriptorString

      Returns the descriptor string of this class.

      The descriptor is a string representing the type of the class, as specified in JVMS 4.3.2. For example, the descriptor for String is "Ljava/lang/String;".

      Returns:
      The descriptor string of this class.
    • equals

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

      public int compareTo(ClassInfo o)
      Specified by:
      compareTo in interface Comparable<ClassInfo>
    • getAllFields

      Returns all fields on this class and all parent classes.

      Results are ordered parent-to-child, and then alphabetical per class.

      Returns:
      All declared fields on this class.
      List is unmodifiable.
    • getAllMethods

      Returns all declared methods on this class and all parent classes in child-to-parent order.

      This method returns methods of all visibility levels (public, protected, package-private, and private).

      Methods are returned in child-to-parent order - methods from the current class appear first, followed by methods from the immediate parent, then grandparent, etc. Within each class, methods are sorted alphabetically.

      Example:

      // Given class hierarchy: class Parent { void method1() {} void method2() {} } class Child extends Parent { void method3() {} void method4() {} } // getAllMethods() returns in child-to-parent order: ClassInfo ci = ClassInfo.of(Child.class); List<MethodInfo> methods = ci.getAllMethods(); // Returns: [method3, method4, method1, method2] // ^Child methods^ ^Parent methods^

      Comparison with Similar Methods:
      Notes:
      • Unlike Java's Class.getMethods(), this returns methods of all visibility levels, not just public ones.
      • Methods from Object class are excluded from the results.
      • Use getAllMethodsTopDown() if you need parent methods to appear before child methods.
      Returns:
      All declared methods on this class and all parent classes (all visibility levels).
      Results are ordered child-to-parent, and then alphabetically per class.
      List is unmodifiable.
    • getAllMethodsTopDown

      Returns all declared methods on this class and all parent classes in parent-to-child order.

      This method returns methods of all visibility levels (public, protected, package-private, and private).

      Methods are returned in parent-to-child order - methods from the most distant ancestor appear first, followed by methods from each subsequent child class, ending with methods from the current class. Within each class, methods are sorted alphabetically.

      This ordering is useful for initialization hooks and lifecycle methods where parent methods should execute before child methods (e.g., @RestInit, @PostConstruct, etc.).

      Example:

      // Given class hierarchy: class Parent { void method1() {} void method2() {} } class Child extends Parent { void method3() {} void method4() {} } // getAllMethodsParentFirst() returns in parent-to-child order: ClassInfo ci = ClassInfo.of(Child.class); List<MethodInfo> methods = ci.getAllMethodsParentFirst(); // Returns: [method1, method2, method3, method4] // ^Parent methods^ ^Child methods^

      Comparison with Similar Methods:
      Notes:
      • Methods from Object class are excluded from the results.
      • Use getAllMethods() if you need child methods to appear before parent methods.
      Returns:
      All declared methods on this class and all parent classes (all visibility levels).
      Results are ordered parent-to-child, and then alphabetically per class.
      List is unmodifiable.
    • getAllParents

      Returns a list including this class and all parent classes and interfaces.

      Results are classes-before-interfaces, then child-to-parent order.

      Returns:
      An unmodifiable list including this class and all parent classes.
      Results are ordered child-to-parent order with classes listed before interfaces.
    • getAnnotatableType

      Description copied from interface: Annotatable
      Returns the type of this annotatable object.
      Specified by:
      getAnnotatableType in interface Annotatable
      Returns:
      The type of annotatable object this represents.
    • getAnnotatedInterfaces

      Returns a list of AnnotatedType objects that represent the annotated interfaces implemented by this class.

      Returns a cached, unmodifiable list. If this class represents a class or interface whose superinterfaces are annotated, the returned objects reflect the annotations used in the source code to declare the superinterfaces.

      Returns:
      An unmodifiable list of AnnotatedType objects representing the annotated superinterfaces.
      Returns an empty list if this class implements no interfaces.
    • getAnnotatedSuperclass

      Returns an AnnotatedType object that represents the annotated superclass of this class.

      If this class represents a class type whose superclass is annotated, the returned object reflects the annotations used in the source code to declare the superclass.

      Returns:
      An AnnotatedType object representing the annotated superclass, or null if this class represents Object, an interface, a primitive type, or void.
    • getAnnotations

      Returns all annotations on this class and parent classes/interfaces in child-to-parent order.

      This returns all declared annotations from:

      1. This class
      2. Parent classes in child-to-parent order
      3. For each class, interfaces declared on that class and their parent interfaces
      4. The package of this class

      This does NOT include runtime annotations. For runtime annotation support, use AnnotationProvider.

      Returns:
      An unmodifiable list of all annotation infos.
    • getAnnotations

      public <A extends Annotation> Stream<AnnotationInfo<A>> getAnnotations(Class<A> type)
      Returns all annotations of the specified type on this class and parent classes/interfaces in child-to-parent order.

      This returns all declared annotations from:

      1. This class
      2. Parent classes in child-to-parent order
      3. For each class, interfaces declared on that class and their parent interfaces
      4. The package of this class

      This does NOT include runtime annotations. For runtime annotation support, use AnnotationProvider.

      Type Parameters:
      A - The annotation type.
      Parameters:
      type - The annotation type to filter by.
      Returns:
      A stream of annotation infos of the specified type.
    • getClassLoader

      Returns the ClassLoader for this class.

      If this class represents a primitive type or void, null is returned.

      Returns:
      The class loader for this class, or null if it doesn't have one.
    • getComponentType

      Returns the base component type of this class.

      For array types (e.g., String[][]), returns the deepest component type (e.g., String).
      For non-array types, returns this class itself.

      Note: Unlike Class.getComponentType(), this method also handles generic array types (e.g., List<String>[]) and returns the full parameterized type information (e.g., List<String>). Additionally, this method never returns null - non-array types return this instead.

      Returns:
      The base component type of an array, or this class if not an array.
    • getDeclaredAnnotations

      Returns all annotations declared directly on this class, wrapped in AnnotationInfo objects.

      This includes annotations explicitly applied to the class declaration, but excludes inherited annotations from parent classes. Each annotation is wrapped for additional functionality such as annotation member access and metadata inspection.

      Note on Repeatable Annotations: Repeatable annotations (those marked with @Repeatable) are automatically expanded into their individual annotation instances. For example, if a class has multiple @Bean annotations, this method returns each @Bean annotation separately, rather than the container annotation.

      Returns:
      An unmodifiable list of AnnotationInfo wrappers for annotations declared directly on this class.
      List is empty if no annotations are declared.
      Results are in declaration order.
      Repeatable annotations are expanded into individual instances.
    • getDeclaredConstructor

      Returns the first matching declared constructor on this class.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The declared constructor that matches the specified predicate.
    • getDeclaredConstructors

      Returns all the constructors defined on this class.
      Returns:
      All constructors defined on this class.
      List is unmodifiable.
    • getDeclaredField

      Returns the first matching declared field on this class.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The declared field, or null if not found.
    • getDeclaredFields

      Returns all declared fields on this class.
      Returns:
      All declared fields on this class.
      Results are in alphabetical order.
      List is unmodifiable.
    • getDeclaredInterfaces

      Returns a list of interfaces declared on this class.

      Does not include interfaces declared on parent classes.

      Results are in the same order as Class.getInterfaces().

      Returns:
      An unmodifiable list of interfaces declared on this class.
      Results are in the same order as Class.getInterfaces().
    • getDeclaredMemberClasses

      Returns all classes and interfaces declared as members of this class.

      This includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces.

      Returns:
      An unmodifiable list of all classes and interfaces declared as members of this class.
      Returns an empty list if this class declares no classes or interfaces as members.
    • getDeclaredMethod

      Returns the first matching declared method on this class.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The first matching method, or null if no methods matched.
    • getDeclaredMethods

      Returns all methods declared on this class.

      This method returns methods of all visibility levels (public, protected, package-private, and private) declared directly on this class only (does not include inherited methods).

      Comparison with Similar Methods:
      • getDeclaredMethods() - Returns all declared methods on this class only (all visibility levels) ← This method
      • getAllMethods() - Returns all declared methods on this class and parents (all visibility levels)
      • getPublicMethods() - Returns public methods only on this class and parents
      Notes:
      Returns:
      All methods declared on this class (all visibility levels).
      Results are ordered alphabetically.
      List is unmodifiable.
    • getDeclaringClass

      Returns the Class object representing the class or interface that declares the member class represented by this class.

      This method returns the class in which this class is explicitly declared as a member. It only returns non-null for member classes (static and non-static nested classes).

      Examples:
      • class Outer { class Inner {} } - Inner.getDeclaringClass() returns Outer
      • class Outer { static class Nested {} } - Nested.getDeclaringClass() returns Outer
      • class Outer { void method() { class Local {} } } - Local.getDeclaringClass() returns null
      • Top-level class - getDeclaringClass() returns null
      • Anonymous class - getDeclaringClass() returns null
      See Also:
      • getEnclosingClass() - Returns the immediately enclosing class (works for local and anonymous classes too)
      Returns:
      The declaring class, or null if this class is not a member of another class.
    • getDimensions

      public int getDimensions()
      Returns the number of dimensions if this is an array type.
      Returns:
      The number of dimensions if this is an array type, or 0 if it is not.
    • getEnclosingClass

      Returns the immediately enclosing class of this class.

      This method returns the lexically enclosing class, regardless of whether this class is a member, local, or anonymous class. Unlike getDeclaringClass(), this method works for all types of nested classes.

      Examples:
      • class Outer { class Inner {} } - Inner.getEnclosingClass() returns Outer
      • class Outer { static class Nested {} } - Nested.getEnclosingClass() returns Outer
      • class Outer { void method() { class Local {} } } - Local.getEnclosingClass() returns Outer
      • class Outer { void method() { new Runnable() {...} } } - Anonymous class getEnclosingClass() returns Outer
      • Top-level class - getEnclosingClass() returns null
      Differences from getDeclaringClass():
      • getDeclaringClass() - Returns non-null only for member classes (static or non-static nested classes)
      • getEnclosingClass() - Returns non-null for all nested classes (member, local, and anonymous)
      See Also:
      Returns:
      The enclosing class, or null if this is a top-level class.
    • getEnclosingConstructor

      Returns the ConstructorInfo object representing the constructor that declares this class if this is a local or anonymous class declared within a constructor.

      Returns null if this class was not declared within a constructor.

      Returns:
      The enclosing constructor, or null if this class was not declared within a constructor.
    • getEnclosingMethod

      Returns the MethodInfo object representing the method that declares this class if this is a local or anonymous class declared within a method.

      Returns null if this class was not declared within a method.

      Returns:
      The enclosing method, or null if this class was not declared within a method.
    • getGenericInterfaces

      Returns the Types representing the interfaces directly implemented by this class.

      Returns a cached, unmodifiable list. If a superinterface is a parameterized type, the Type returned for it reflects the actual type parameters used in the source code.

      Returns:
      An unmodifiable list of Types representing the interfaces directly implemented by this class.
      Returns an empty list if this class implements no interfaces.
    • getGenericSuperclass

      Returns the Type representing the direct superclass of this class.

      If the superclass is a parameterized type, the Type returned reflects the actual type parameters used in the source code.

      Returns:
      The superclass of this class as a Type, or null if this class represents Object, an interface, a primitive type, or void.
    • getInterfaces

      Returns a list of interfaces defined on this class and superclasses.

      Results are in child-to-parent order.

      Returns:
      An unmodifiable list of interfaces defined on this class and superclasses.
      Results are in child-to-parent order.
    • getLabel

      public String getLabel()
      Description copied from interface: Annotatable
      Returns a human-readable label for this annotatable element.

      The label format depends on the type of annotatable:

      • CLASS_TYPE - Simple class name (e.g., "MyClass")
      • METHOD_TYPE - Class and method with parameter types (e.g., "MyClass.myMethod(String,int)")
      • FIELD_TYPE - Class and field name (e.g., "MyClass.myField")
      • CONSTRUCTOR_TYPE - Class and constructor with parameter types (e.g., "MyClass.MyClass(String)")
      • PARAMETER_TYPE - Class, method/constructor, and parameter index (e.g., "MyClass.myMethod[0]")
      • PACKAGE_TYPE - Package name (e.g., "com.example.package")
      Specified by:
      getLabel in interface Annotatable
      Returns:
      The human-readable label for this annotatable element.
    • getMemberClasses

      Returns all public member classes and interfaces declared by this class and its superclasses.

      This includes public class and interface members inherited from superclasses and public class and interface members declared by the class.

      Returns:
      An unmodifiable list of all public member classes and interfaces declared by this class.
      Returns an empty list if this class has no public member classes or interfaces.
    • getMethod

      Returns the first matching method on this class.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The first matching method, or null if no methods matched.
    • getModule

      public Module getModule()
      Returns the module that this class is a member of.

      If this class is not in a named module, returns the unnamed module of the class loader for this class.

      Returns:
      The module that this class is a member of.
    • getName

      public String getName()
      Returns the name of the underlying class.

      Equivalent to calling Class.getName() or Type.getTypeName() depending on whether this is a class or type.

      This method returns the JVM internal format for class names:

      • Uses fully qualified package names
      • Uses '$' separator for nested classes
      • Uses JVM notation for arrays (e.g., "[Ljava.lang.String;")
      • Uses single letters for primitive arrays (e.g., "[I" for int[])
      Examples:
      • "java.lang.String" - Normal class
      • "[Ljava.lang.String;" - Array
      • "[[Ljava.lang.String;" - Multi-dimensional array
      • "java.util.Map$Entry" - Nested class
      • "int" - Primitive class
      • "[I" - Primitive array
      • "[[I" - Multi-dimensional primitive array
      See Also:
      Returns:
      The name of the underlying class in JVM format.
    • getNameCanonical

      Returns the canonical name of the underlying class.

      The canonical name is the name that would be used in Java source code to refer to the class. For example:

      • "java.lang.String" - Normal class
      • "java.lang.String[]" - Array
      • "java.util.Map.Entry" - Nested class
      • null - Local or anonymous class
      Returns:
      The canonical name of the underlying class, or null if this class doesn't have a canonical name.
    • getNameFormatted

      public String getNameFormatted(ClassNameFormat nameFormat, boolean includeTypeParams, char separator, ClassArrayFormat arrayFormat)
      Returns a formatted class name with configurable options.

      This is a unified method that can produce output equivalent to all other name methods by varying the parameters.

      Examples:

      // Given: java.util.HashMap<String,Integer>[] // Full name with generics getFormattedName(ClassNameFormat.FULL, true, '$', ClassArrayFormat.BRACKETS) // → "java.util.HashMap<java.lang.String,java.lang.Integer>[]" // Short name with generics getFormattedName(ClassNameFormat.SHORT, true, '$', ClassArrayFormat.BRACKETS) // → "HashMap<String,Integer>[]" // Simple name getFormattedName(ClassNameFormat.SIMPLE, false, '$', ClassArrayFormat.BRACKETS) // → "HashMap[]" // With dot separator getFormattedName(ClassNameFormat.SHORT, false, '.', ClassArrayFormat.BRACKETS) // → "Map.Entry" // Word format for arrays getFormattedName(ClassNameFormat.SIMPLE, false, '$', ClassArrayFormat.WORD) // → "HashMapArray"

      Equivalent Methods:
      • getName() = getFormattedName(FULL, false, '$', JVM)
      • getCanonicalName() = getFormattedName(FULL, false, '.', BRACKETS)
      • getSimpleName() = getFormattedName(SIMPLE, false, '$', BRACKETS)
      • getFullName() = getFormattedName(FULL, true, '$', BRACKETS)
      • getShortName() = getFormattedName(SHORT, true, '$', BRACKETS)
      • getReadableName() = getFormattedName(SIMPLE, false, '$', WORD)
      Parameters:
      nameFormat - Controls which parts of the class name to include (package, outer classes).
      includeTypeParams - If true, include generic type parameters recursively.
      For example: "HashMap<String,Integer>" instead of "HashMap"
      separator - Character to use between outer and inner class names.
      Typically '$' (JVM format) or '.' (canonical format).
      Ignored when nameFormat is ClassNameFormat.SIMPLE.
      arrayFormat - How to format array dimensions.
      Returns:
      The formatted class name.
    • getNameFull

      public String getNameFull()
      Returns the full name of this class.
      Examples:
      • "com.foo.MyClass" - Normal class
      • "com.foo.MyClass[][]" - Array.
      • "com.foo.MyClass$InnerClass" - Inner class.
      • "com.foo.MyClass$InnerClass[][]" - Inner class array.
      • "int" - Primitive class.
      • "int[][]" - Primitive class class.
      • "java.util.Map<java.lang.String,java.lang.Object>" - Parameterized type.
      • "java.util.AbstractMap<K,V>" - Parameterized generic type.
      • "V" - Parameterized generic type argument.
      Returns:
      The underlying class name.
    • getNameReadable

      Same as getNameSimple() but uses "Array" instead of "[]".
      Returns:
      The readable name for this class.
    • getNames

      public String[] getNames()
      Returns all possible names for this class.
      Returns:
      An array consisting of:
    • getNameShort

      public String getNameShort()
      Returns the short name of the underlying class.

      Similar to getNameSimple() but also renders local or member class name prefixes.

      Returns:
      The short name of the underlying class.
    • getNameSimple

      Returns the simple name of the underlying class.

      Returns either Class.getSimpleName() or Type.getTypeName() depending on whether this is a class or type.

      Returns:
      The simple name of the underlying class;
    • getNestHost

      Returns the nest host of this class.

      Every class belongs to exactly one nest. A class that is not a member of a nest is its own nest host.

      Returns:
      The nest host of this class.
    • getNestMembers

      Returns an array containing all the classes and interfaces that are members of the nest to which this class belongs.
      Returns:
      An unmodifiable list of all classes and interfaces in the same nest as this class.
      Returns an empty list if this object does not represent a class.
    • getNoArgConstructor

      Locates the no-arg constructor for this 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.

      Parameters:
      v - The minimum visibility.
      Returns:
      The constructor, or null if no no-arg constructor exists with the required visibility.
    • getPackage

      Returns the package of this class.

      Returns null in the following cases:

      • Primitive types (e.g., int.class, boolean.class)
      • Array types of primitives (e.g., int[].class)
      • Classes in the default (unnamed) package - while the default package is technically a package, Java's Class.getPackage() returns null for classes in the default package because it has no name or associated Package object
      Returns:
      The package of this class wrapped in a PackageInfo, or null if this class has no package or is in the default (unnamed) package.
    • getPackageAnnotation

      public <A extends Annotation> A getPackageAnnotation(Class<A> type)
      Returns the specified annotation only if it's been declared on the package of this class.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation class.
      Returns:
      The annotation, or null if not found.
    • getParameterType

      public Class<?> getParameterType(int index, Class<?> pt)
      Finds the real parameter type of this class.
      Parameters:
      index - The zero-based index of the parameter to resolve.
      pt - The parameterized type class containing the parameterized type to resolve (e.g. HashMap).
      Returns:
      The resolved real class.
    • getParents

      Returns a list including this class and all parent classes.

      Does not include interfaces.

      Results are in child-to-parent order.

      Returns:
      An unmodifiable list including this class and all parent classes.
      Results are in child-to-parent order.
    • getParentsAndInterfaces

      Returns all parent classes and interfaces in proper traversal order.

      This method returns a unique list of all parent classes (including this class) and all interfaces (including interface hierarchies) with proper handling of duplicates. The order is:

      1. This class
      2. Parent classes in child-to-parent order
      3. For each class, interfaces declared on that class and their parent interfaces

      This is useful for annotation processing where you need to traverse the complete type hierarchy without duplicates.

      Example:

      // Interface hierarchy: interface ISuperGrandParent {} interface IGrandParent extends ISuperGrandParent {} interface ISuperParent {} interface IParent extends ISuperParent {} interface IChild {} // Class hierarchy: class GrandParent implements IGrandParent {} class Parent extends GrandParent implements IParent {} class Child extends Parent implements IChild {} // For Child, returns (in this order): ClassInfo ci = ClassInfo.of(Child.class); List<ClassInfo> result = ci.getParentsAndInterfaces(); // Result: [ // Child, // 1. This class // IChild, // 2. Interface on Child // Parent, // 3. Parent class // IParent, // 4. Interface on Parent // ISuperParent, // 5. Parent interface of IParent // GrandParent, // 6. Grandparent class // IGrandParent, // 7. Interface on GrandParent // ISuperGrandParent // 8. Parent interface of IGrandParent // ]

      Returns:
      An unmodifiable list of all parent classes and interfaces, properly ordered without duplicates.
    • getPermittedSubclasses

      Returns the permitted subclasses of this sealed class.

      If this class is not sealed, returns an empty list.

      Returns:
      An unmodifiable list of permitted subclasses if this is a sealed class.
      Returns an empty list if this class is not sealed.
    • getPrimitiveDefault

      Returns the default value for this primitive class.
      Returns:
      The default value, or null if this is not a primitive class.
    • getPrimitiveForWrapper

      If this class is a primitive wrapper (e.g. Integer.class) returns it's primitive class (e.g. int.class).
      Returns:
      The primitive class, or null if class is not a primitive wrapper.
    • getPrimitiveWrapper

      If this class is a primitive (e.g. int.class) returns it's wrapper class (e.g. Integer.class).
      Returns:
      The wrapper class, or null if class is not a primitive.
    • getProtectionDomain

      Returns the ProtectionDomain of this class.

      If a security manager is installed, this method requires RuntimePermission("getProtectionDomain").

      Returns:
      The ProtectionDomain of this class, or null if the class does not have a protection domain.
    • getPublicConstructor

      Returns the first matching public constructor on this class.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The public constructor that matches the specified predicate.
    • getPublicConstructors

      Returns all the public constructors defined on this class.
      Returns:
      All public constructors defined on this class.
    • getPublicField

      Returns the first matching public field on this class.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The public field, or null if not found.
    • getPublicFields

      Returns all public fields on this class.

      Hidden fields are excluded from the results.

      Returns:
      All public fields on this class.
      Results are in alphabetical order.
      List is unmodifiable.
    • getPublicMethod

      Returns the first matching public method on this class.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The first matching method, or null if no methods matched.
    • getPublicMethods

      Returns all public methods on this class and parent classes.

      This method returns public methods only, from this class and all parent classes and interfaces.

      Comparison with Similar Methods:
      • getDeclaredMethods() - Returns all declared methods on this class only (all visibility levels)
      • getAllMethods() - Returns all declared methods on this class and parents (all visibility levels)
      • getPublicMethods() - Returns public methods only on this class and parents ← This method
      Notes:
      • This method behaves similarly to Java's Class.getMethods(), returning only public methods.
      • Methods defined on the Object class are excluded from the results.
      Returns:
      All public methods on this class and parent classes.
      Results are ordered alphabetically.
      List is unmodifiable.
    • getRecordComponents

      Returns the record components of this record class.

      Returns a cached, unmodifiable list of record components. The components are returned in the same order as they appear in the record declaration. If this class is not a record, returns an empty list.

      Returns:
      An unmodifiable list of record components, or an empty list if this class is not a record.
    • getRepeatedAnnotationMethod

      Returns the repeated annotation method on this class.

      The repeated annotation method is the value() method that returns an array of annotations who themselves are marked with the @Repeatable annotation of this class.

      Returns:
      The repeated annotation method on this class, or null if it doesn't exist.
    • getResource

      public URL getResource(String name)
      Finds a resource with a given name.

      The rules for searching resources associated with a given class are implemented by the defining class loader.

      Parameters:
      name - The resource name.
      Returns:
      A URL object for reading the resource, or null if the resource could not be found.
    • getResourceAsStream

      Finds a resource with a given name and returns an input stream for reading the resource.

      The rules for searching resources associated with a given class are implemented by the defining class loader.

      Parameters:
      name - The resource name.
      Returns:
      An input stream for reading the resource, or null if the resource could not be found.
    • getSigners

      public List<Object> getSigners()
      Returns the signers of this class.

      Returns a cached, unmodifiable list.

      Returns:
      An unmodifiable list of signers, or an empty list if there are no signers.
    • getSuperclass

      Returns the parent class.
      Returns:
      The parent class, or null if the class has no parent.
    • getTypeParameters

      Returns a list of TypeVariable objects that represent the type variables declared by this class.

      Returns a cached, unmodifiable list. The type variables are returned in the same order as they appear in the class declaration.

      Returns:
      An unmodifiable list of TypeVariable objects representing the type parameters of this class.
      Returns an empty list if this class declares no type parameters.
    • getWrapperIfPrimitive

      Returns the wrapper class if this is a primitive, otherwise returns this class.

      If this class is a primitive (e.g. int.class), returns its wrapper class (e.g. Integer.class) wrapped in a ClassInfo. Otherwise, returns this ClassInfo instance.

      Example:

      ClassInfo intClass = ClassInfo.of(int.class); ClassInfo wrapper = intClass.getWrapperIfPrimitive(); // Returns ClassInfo for Integer.class ClassInfo stringClass = ClassInfo.of(String.class); ClassInfo same = stringClass.getWrapperIfPrimitive(); // Returns same ClassInfo

      Returns:
      The wrapper ClassInfo if this is a primitive, or this ClassInfo if not a primitive.
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> type)
      Returns true if this class has the specified annotation.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation to look for.
      Returns:
      The true if annotation if found.
    • hashCode

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

      public boolean hasPackage()
      Returns true if this class is not in the root package.
      Returns:
      true if this class is not in the root package.
    • hasPrimitiveWrapper

      public boolean hasPrimitiveWrapper()
      Returns true if the getPrimitiveWrapper() method returns a value.
      Returns:
      true if the getPrimitiveWrapper() method returns a value.
    • inner

      public <T> Class<T> inner()
      Returns the wrapped class as a Class.
      Type Parameters:
      T - The inner class type.
      Returns:
      The wrapped class as a Class, or null if it's not a class (e.g. it's a ParameterizedType).
    • innerType

      public Type innerType()
      Returns the wrapped class as a Type.
      Returns:
      The wrapped class as a Type.
    • is

      public boolean is(Class<?> c)
      Checks for equality with the specified class.
      Parameters:
      c - The class to check equality with.
      Returns:
      true if the specified class is the same as this one.
    • is

      public boolean is(ClassInfo c)
      Checks for equality with the specified class.
      Parameters:
      c - The class to check equality with.
      Returns:
      true if the specified class is the same as this one.
    • is

      public boolean is(ElementFlag flag)
      Returns true if all specified flags are applicable to this class.
      Overrides:
      is in class ElementInfo
      Parameters:
      flag - The flag to test for.
      Returns:
      true if all specified flags are applicable to this class.
    • isAnnotation

      public boolean isAnnotation()
      Returns true if this class is an annotation.
      Returns:
      true if this class is an annotation.
    • isAnonymousClass

      public boolean isAnonymousClass()
      Returns true if this class is an anonymous class.

      An anonymous class is a local class declared within a method or constructor that has no name.

      Returns:
      true if this class is an anonymous class.
    • isAny

      public boolean isAny(Class<?>... types)
      Returns true if this class is any of the specified types.
      Parameters:
      types - The types to check against.
      Returns:
      true if this class is any of the specified types.
    • isAny

      public boolean isAny(Class<?> t1, Class<?> t2)
      Returns true if this class is any of the specified types.

      This is a high-performance overload that avoids array creation and iteration. Use this method instead of isAny(Class...) when checking against exactly 2 types.

      Parameters:
      t1 - The first type to check against.
      t2 - The second type to check against.
      Returns:
      true if this class is any of the specified types.
    • isAny

      public boolean isAny(Class<?> t1, Class<?> t2, Class<?> t3)
      Returns true if this class is any of the specified types.

      This is a high-performance overload that avoids array creation and iteration. Use this method instead of isAny(Class...) when checking against exactly 3 types.

      Parameters:
      t1 - The first type to check against.
      t2 - The second type to check against.
      t3 - The third type to check against.
      Returns:
      true if this class is any of the specified types.
    • isAny

      public boolean isAny(Class<?> t1, Class<?> t2, Class<?> t3, Class<?> t4)
      Returns true if this class is any of the specified types.

      This is a high-performance overload that avoids array creation and iteration. Use this method instead of isAny(Class...) when checking against exactly 4 types.

      Parameters:
      t1 - The first type to check against.
      t2 - The second type to check against.
      t3 - The third type to check against.
      t4 - The fourth type to check against.
      Returns:
      true if this class is any of the specified types.
    • isAny

      public boolean isAny(Class<?> t1, Class<?> t2, Class<?> t3, Class<?> t4, Class<?> t5)
      Returns true if this class is any of the specified types.

      This is a high-performance overload that avoids array creation and iteration. Use this method instead of isAny(Class...) when checking against exactly 5 types.

      Parameters:
      t1 - The first type to check against.
      t2 - The second type to check against.
      t3 - The third type to check against.
      t4 - The fourth type to check against.
      t5 - The fifth type to check against.
      Returns:
      true if this class is any of the specified types.
    • isArray

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

      public boolean isChildOf(Class<?> parent)
      Returns true if this class is a child or the same as parent.
      Parameters:
      parent - The parent class.
      Returns:
      true if this class is a child or the same as parent.
    • isChildOf

      public boolean isChildOf(ClassInfo parent)
      Returns true if this class is a child or the same as parent.
      Parameters:
      parent - The parent class.
      Returns:
      true if this class is a parent or the same as parent.
    • isChildOf

      public boolean isChildOf(Type parent)
      Returns true if this class is a child or the same as parent.
      Parameters:
      parent - The parent class.
      Returns:
      true if this class is a parent or the same as parent.
    • isChildOfAny

      public boolean isChildOfAny(Class<?>... parents)
      Returns true if this class is a child or the same as any of the parents.
      Parameters:
      parents - The parents class.
      Returns:
      true if this class is a child or the same as any of the parents.
    • isClass

      public boolean isClass()
      Returns true if this class is not an interface.
      Returns:
      true if this class is not an interface.
    • isCollectionOrArray

      public boolean isCollectionOrArray()
      Returns true if this class is a Collection or an array.
      Returns:
      true if this class is a Collection or an array.
    • isDeprecated

      public boolean isDeprecated()
      Returns true if this class has the @Deprecated annotation on it.
      Returns:
      true if this class has the @Deprecated annotation on it.
    • isEnum

      public boolean isEnum()
      Returns true if this class is an enum.
      Returns:
      true if this class is an enum.
    • isInstance

      public boolean isInstance(Object value)
      Returns true if the specified value is an instance of this class.
      Parameters:
      value - The value to check.
      Returns:
      true if the specified value is an instance of this class.
    • isLocalClass

      public boolean isLocalClass()
      Returns true if this class is a local class.
      Returns:
      true if this class is a local class.
    • isMemberClass

      public boolean isMemberClass()
      Returns true if this class is a member class.
      Returns:
      true if this class is a member class.
    • isNestmateOf

      public boolean isNestmateOf(Class<?> c)
      Determines if this class and the specified class are nestmates.

      Two classes are nestmates if they belong to the same nest.

      Parameters:
      c - The class to check.
      Returns:
      true if this class and the specified class are nestmates.
    • isNonStaticMemberClass

      public boolean isNonStaticMemberClass()
      Returns true if this class is a member class and not static.
      Returns:
      true if this class is a member class and not static.
    • isNotDeprecated

      public boolean isNotDeprecated()
      Returns true if this class doesn't have the @Deprecated annotation on it.
      Returns:
      true if this class doesn't have the @Deprecated annotation on it.
    • isNotLocalClass

      public boolean isNotLocalClass()
      Returns true if this class is a local class.
      Returns:
      true if this class is a local class.
    • isNotMemberClass

      public boolean isNotMemberClass()
      Returns true if this class is a member class.
      Returns:
      true if this class is a member class.
    • isNotNonStaticMemberClass

      public boolean isNotNonStaticMemberClass()
      Returns false if this class is a member class and not static.
      Returns:
      false if this class is a member class and not static.
    • isNotPrimitive

      public boolean isNotPrimitive()
      Returns true if this is not a primitive class.
      Returns:
      true if this is not a primitive class.
    • isVoid

      public boolean isVoid()
      Returns true if this class is void.class or Void or has the simple name "Void.
      Returns:
      true if this class is void.class or Void or has the simple name "Void.
    • isNotVoid

      public boolean isNotVoid()
      Returns true if this class is not void.class and not Void and does not have the simple name "Void.
      Returns:
      true if this class is not void.class and not Void and does not have the simple name "Void.
    • isParentOf

      public boolean isParentOf(Class<?> child)
      Returns true if this class is a parent or the same as child.
      Parameters:
      child - The child class.
      Returns:
      true if this class is a parent or the same as child.
    • isParentOf

      public boolean isParentOf(ClassInfo child)
      Returns true if this class is a parent or the same as child.

      Same as isParentOf(Class) but takes in a ClassInfo.

      Example:

      ClassInfo parent = ClassInfo.of(List.class); ClassInfo child = ClassInfo.of(ArrayList.class); boolean b = parent.isParentOf(child); // true

      Parameters:
      child - The child class.
      Returns:
      true if this class is a parent or the same as child.
    • isParentOf

      public boolean isParentOf(Type child)
      Returns true if this class is a parent or the same as child.
      Parameters:
      child - The child class.
      Returns:
      true if this class is a parent or the same as child.
    • isParentOfLenient

      public boolean isParentOfLenient(Class<?> child)
      Returns true if this class is a parent or the same as child.

      Primitive classes are converted to wrapper classes and compared.

      Examples:

      ClassInfo.of(String.class).isParentOfLenient(String.class); // true ClassInfo.of(CharSequence.class).isParentOfLenient(String.class); // true ClassInfo.of(String.class).isParentOfLenient(CharSequence.class); // false ClassInfo.of(int.class).isParentOfLenient(Integer.class); // true ClassInfo.of(Integer.class).isParentOfLenient(int.class); // true ClassInfo.of(Number.class).isParentOfLenient(int.class); // true ClassInfo.of(int.class).isParentOfLenient(Number.class); // false ClassInfo.of(int.class).isParentOfLenient(long.class); // false

      Parameters:
      child - The child class.
      Returns:
      true if this class is a parent or the same as child.
    • isParentOfLenient

      public boolean isParentOfLenient(ClassInfo child)
      Same as isParentOfLenient(Class) but takes in a ClassInfo.
      Parameters:
      child - The child class.
      Returns:
      true if this class is a parent or the same as child.
    • isParentOfLenient

      public boolean isParentOfLenient(Type child)
      Returns true if this class is a parent or the same as child.
      Parameters:
      child - The child class.
      Returns:
      true if this class is a parent or the same as child.
    • isPrimitive

      public boolean isPrimitive()
      Returns true if this is a primitive class.
      Returns:
      true if this is a primitive class.
    • isRecord

      public boolean isRecord()
      Returns true if this class is a record class.

      A record class is a final class that extends Record.

      Returns:
      true if this class is a record class.
    • isRepeatedAnnotation

      public boolean isRepeatedAnnotation()
      Returns true if this is a repeated annotation class.

      A repeated annotation has a single value() method that returns an array of annotations who themselves are marked with the @Repeatable annotation of this class.

      Returns:
      true if this is a repeated annotation class.
    • isRuntimeException

      public boolean isRuntimeException()
      Returns true if this class is a RuntimeException.
      Returns:
      true if this class is a RuntimeException.
    • isSealed

      public boolean isSealed()
      Returns true if this class is a sealed class.

      A sealed class is a class that can only be extended by a permitted set of subclasses.

      Returns:
      true if this class is a sealed class.
    • isStrictChildOf

      public boolean isStrictChildOf(Class<?> parent)
      Returns true if this class is a child of parent.
      Parameters:
      parent - The parent class.
      Returns:
      true if this class is a parent of child.
    • isSynthetic

      public boolean isSynthetic()
      Returns true if this class is a synthetic class.

      A synthetic class is one that is generated by the compiler and does not appear in source code.

      Returns:
      true if this class is synthetic.
    • isVisible

      public boolean isVisible(Visibility v)
      Identifies if the specified visibility matches this constructor.
      Parameters:
      v - The visibility to validate against.
      Returns:
      true if this visibility matches the modifier attribute of this constructor.
    • newInstance

      Shortcut for calling Class.getDeclaredConstructor().newInstance() on the underlying class.
      Returns:
      A new instance of the underlying class
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • unwrap

      public ClassInfo unwrap(Class<?>... wrapperTypes)
      Unwrap this class if it's a parameterized type of the specified type such as Value or Optional.
      Parameters:
      wrapperTypes - The parameterized types to unwrap if this class is one of those types.
      Returns:
      The class info on the unwrapped type, or just this type if this isn't one of the specified types.