Class ClassInfo
- All Implemented Interfaces:
Comparable<ClassInfo>,Type,Annotatable
- Direct Known Subclasses:
ClassInfoTyped
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:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappendNameFormatted(StringBuilder sb, ClassNameFormat nameFormat, boolean includeTypeParams, char separator, ClassArrayFormat arrayFormat) Appends a formatted class name to a StringBuilder with configurable options.Returns aClassInfofor an array type whose component type is this class.<U> ClassInfoasSubclass(Class<U> clazz) Casts thisClassInfoobject to represent a subclass of the class represented by the specified class object.booleancanAcceptArg(Object child) Returnstrue if this type can be used as a parameter for the specified object.<T> TCasts an object to the class represented by thisClassInfoobject.intReturns the component type of this class if it is an array type.Returns the descriptor string of this class.booleanReturns all fields on this class and all parent classes.Returns all declared methods on this class and all parent classes in child-to-parent order.Returns all declared methods on this class and all parent classes in parent-to-child order.Returns a list including this class and all parent classes and interfaces.Returns the type of this annotatable object.Returns a list ofAnnotatedTypeobjects that represent the annotated interfaces implemented by this class.Returns anAnnotatedTypeobject that represents the annotated superclass of this class.List<AnnotationInfo<? extends Annotation>>Returns all annotations on this class and parent classes/interfaces in child-to-parent order.<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.Returns theClassLoaderfor this class.Returns the base component type of this class.Returns all annotations declared directly on this class, wrapped inAnnotationInfoobjects.Returns the first matching declared constructor on this class.Returns all the constructors defined on this class.getDeclaredField(Predicate<FieldInfo> filter) Returns the first matching declared field on this class.Returns all declared fields on this class.Returns a list of interfaces declared on this class.Returns all classes and interfaces declared as members of this class.getDeclaredMethod(Predicate<MethodInfo> filter) Returns the first matching declared method on this class.Returns all methods declared on this class.Returns theClassobject representing the class or interface that declares the member class represented by this class.intReturns the number of dimensions if this is an array type.Returns the immediately enclosing class of this class.Returns theConstructorInfoobject representing the constructor that declares this class if this is a local or anonymous class declared within a constructor.Returns theMethodInfoobject representing the method that declares this class if this is a local or anonymous class declared within a method.Returns theTypes representing the interfaces directly implemented by this class.Returns theTyperepresenting the direct superclass of this class.Returns a list of interfaces defined on this class and superclasses.getLabel()Returns a human-readable label for this annotatable element.Returns all public member classes and interfaces declared by this class and its superclasses.getMethod(Predicate<MethodInfo> filter) Returns the first matching method on this class.Returns the module that this class is a member of.getName()Returns the name of the underlying class.Returns the canonical name of the underlying class.getNameFormatted(ClassNameFormat nameFormat, boolean includeTypeParams, char separator, ClassArrayFormat arrayFormat) Returns a formatted class name with configurable options.Returns the full name of this class.String[]getNames()Returns all possible names for this class.Returns the short name of the underlying class.Returns the simple name of the underlying class.Returns the nest host of this class.Returns an array containing all the classes and interfaces that are members of the nest to which this class belongs.Locates the no-arg constructor for this class.Returns the package of this class.<A extends Annotation>
AgetPackageAnnotation(Class<A> type) Returns the specified annotation only if it's been declared on the package of this class.Class<?>getParameterType(int index, Class<?> pt) Finds the real parameter type of this class.Returns a list including this class and all parent classes.Returns all parent classes and interfaces in proper traversal order.Returns the permitted subclasses of this sealed class.Returns the default value for this primitive class.Class<?>If this class is a primitive wrapper (e.g.Class<?>If this class is a primitive (e.g.Returns theProtectionDomainof this class.getPublicConstructor(Predicate<ConstructorInfo> filter) Returns the first matching public constructor on this class.Returns all the public constructors defined on this class.getPublicField(Predicate<FieldInfo> filter) Returns the first matching public field on this class.Returns all public fields on this class.getPublicMethod(Predicate<MethodInfo> filter) Returns the first matching public method on this class.Returns all public methods on this class and parent classes.Returns the record components of this record class.Returns the repeated annotation method on this class.getResource(String name) Finds a resource with a given name.getResourceAsStream(String name) Finds a resource with a given name and returns an input stream for reading the resource.Returns the signers of this class.Returns the parent class.List<TypeVariable<?>>Returns a list ofTypeVariableobjects that represent the type variables declared by this class.Returns the wrapper class if this is a primitive, otherwise returns this class.<A extends Annotation>
booleanhasAnnotation(Class<A> type) Returnstrue if this class has the specified annotation.inthashCode()booleanReturnstrue if this class is not in the root package.booleanReturnstrue if thegetPrimitiveWrapper()method returns a value.<T> Class<T>inner()Returns the wrapped class as aClass.Returns the wrapped class as aType.booleanChecks for equality with the specified class.booleanChecks for equality with the specified class.booleanis(ElementFlag flag) Returnstrue if all specified flags are applicable to this class.booleanReturnstrue if this class is an annotation.booleanReturnstrue if this class is an anonymous class.booleanReturnstrue if this class is any of the specified types.booleanReturnstrue if this class is any of the specified types.booleanReturnstrue if this class is any of the specified types.booleanReturnstrue if this class is any of the specified types.booleanReturnstrue if this class is any of the specified types.booleanisArray()Returnstrue if this class is an array.booleanReturnstrue if this class is a child or the same asparent .booleanReturnstrue if this class is a child or the same asparent .booleanReturnstrue if this class is a child or the same asparent .booleanisChildOfAny(Class<?>... parents) Returnstrue if this class is a child or the same as any of theparents .booleanisClass()Returnstrue if this class is not an interface.booleanReturnstrue if this class is aCollectionor an array.booleanReturnstrue if this class has the@Deprecatedannotation on it.booleanisEnum()Returnstrue if this class is an enum.booleanisInstance(Object value) Returnstrue if the specified value is an instance of this class.booleanReturnstrue if this class is a local class.booleanReturnstrue if this class is a member class.booleanisNestmateOf(Class<?> c) Determines if this class and the specified class are nestmates.booleanReturnstrue if this class is a member class and not static.booleanReturnstrue if this class doesn't have the@Deprecatedannotation on it.booleanReturnstrue if this class is a local class.booleanReturnstrue if this class is a member class.booleanReturnsfalse if this class is a member class and not static.booleanReturnstrue if this is not a primitive class.booleanbooleanisParentOf(Class<?> child) Returnstrue if this class is a parent or the same aschild .booleanisParentOf(Type child) Returnstrue if this class is a parent or the same aschild .booleanisParentOf(ClassInfo child) Returnstrue if this class is a parent or the same aschild .booleanisParentOfLenient(Class<?> child) Returnstrue if this class is a parent or the same aschild .booleanisParentOfLenient(Type child) Returnstrue if this class is a parent or the same aschild .booleanisParentOfLenient(ClassInfo child) Same asisParentOfLenient(Class)but takes in aClassInfo.booleanReturnstrue if this is a primitive class.booleanisRecord()Returnstrue if this class is a record class.booleanReturnstrue if this is a repeated annotation class.booleanReturnstrue if this class is aRuntimeException.booleanisSealed()Returnstrue if this class is a sealed class.booleanisStrictChildOf(Class<?> parent) Returnstrue if this class is a child ofparent .booleanReturnstrue if this class is a synthetic class.booleanIdentifies if the specified visibility matches this constructor.booleanisVoid()Shortcut for callingClass.getDeclaredConstructor().newInstance() on the underlying class.static ClassInfoReturns a class info wrapper around the specified class type.static <T> ClassInfoTyped<T>Returns a class info wrapper around the specified class type.static ClassInfoSame as using the constructor, but operates on an object instance.static ClassInfoReturns a class info wrapper around the specified class type.static ClassInfoSame asof(Object)but attempts to deproxify the object if it's wrapped in a CGLIB proxy.toString()Methods inherited from class org.apache.juneau.commons.reflect.ElementInfo
ai, getModifiers, isAbstract, isAll, isAny, isFinal, isInterface, isNative, isNotAbstract, isNotFinal, isNotInterface, isNotNative, isNotPrivate, isNotProtected, isNotPublic, isNotStatic, isNotSynchronized, isNotTransient, isNotVolatile, isPrivate, isProtected, isPublic, isStatic, isSynchronized, isTransient, isVolatileMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.reflect.Type
getTypeName
-
Field Details
-
OBJECT
Reusable ClassInfo for Object class.
-
-
Constructor Details
-
ClassInfo
Constructor.- Parameters:
inner- The class type.innerType- The generic type (if parameterized type).
-
-
Method Details
-
of
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
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
Same as using the constructor, but operates on an object instance.- Parameters:
object- The class instance.- Returns:
- The constructed class info.
-
of
Returns a class info wrapper around the specified class type.- Parameters:
innerType- The class type.- Returns:
- The constructed class info.
-
ofProxy
Same asof(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- Iftrue , include generic type parameters recursively.separator- Character to use between outer and inner class names.
Ignored whennameFormat isClassNameFormat.SIMPLE.arrayFormat- How to format array dimensions.- Returns:
- The same StringBuilder for method chaining.
-
arrayType
Returns aClassInfofor an array type whose component type is this class.- Returns:
- A
ClassInforepresenting an array type whose component type is this class.
-
asSubclass
Casts thisClassInfoobject 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
ClassInfoobject, cast to represent a subclass of the specified class object. - Throws:
ClassCastException- If this class is not assignable to the specified class.
-
canAcceptArg
Returnstrue 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") - returnstrue ClassInfo.of(String.class).canAcceptArg(null) - returnstrue ClassInfo.of(int.class).canAcceptArg(5) - returnstrue ClassInfo.of(int.class).canAcceptArg(null) - returnsfalse (primitives can't be null)ClassInfo.of(Integer.class).canAcceptArg(null) - returnstrue
- Parameters:
child- The argument to check.- Returns:
true if this type can be used as a parameter for the specified object.
-
cast
Casts an object to the class represented by thisClassInfoobject.- Type Parameters:
T- The type to cast to.- Parameters:
obj- The object to be cast.- Returns:
- The object after casting, or
null if obj isnull . - Throws:
ClassCastException- If the object is notnull 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 aClassInfoinstead. Note thatgetComponentType()also exists and returns the base component type for multi-dimensional arrays.- Returns:
- The
ClassInforepresenting the component type, ornull 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
-
compareTo
- Specified by:
compareToin interfaceComparable<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 Childextends Parent {void method3() {}void method4() {} }// getAllMethods() returns in child-to-parent order: ClassInfoci = ClassInfo.of (Child.class ); List<MethodInfo>methods =ci .getAllMethods();// Returns: [method3, method4, method1, method2] // ^Child methods^ ^Parent methods^ Comparison with Similar Methods:
getDeclaredMethods()- Returns methods declared on this class only (all visibility levels)getAllMethods()- Returns all methods in child-to-parent order ← This methodgetAllMethodsTopDown()- Returns all methods in parent-to-child ordergetPublicMethods()- Returns public methods only on this class and parents
Notes:
- Unlike Java's
Class.getMethods(), this returns methods of all visibility levels, not just public ones. - Methods from
Objectclass 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 Childextends Parent {void method3() {}void method4() {} }// getAllMethodsParentFirst() returns in parent-to-child order: ClassInfoci = ClassInfo.of (Child.class ); List<MethodInfo>methods =ci .getAllMethodsParentFirst();// Returns: [method1, method2, method3, method4] // ^Parent methods^ ^Child methods^ Comparison with Similar Methods:
getDeclaredMethods()- Returns methods declared on this class only (all visibility levels)getAllMethods()- Returns all methods in child-to-parent ordergetAllMethodsTopDown()- Returns all methods in parent-to-child order ← This methodgetPublicMethods()- Returns public methods only on this class and parents
Notes:
- Methods from
Objectclass 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:AnnotatableReturns the type of this annotatable object.- Specified by:
getAnnotatableTypein interfaceAnnotatable- Returns:
- The type of annotatable object this represents.
-
getAnnotatedInterfaces
Returns a list ofAnnotatedTypeobjects 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
AnnotatedTypeobjects representing the annotated superinterfaces.
Returns an empty list if this class implements no interfaces.
-
getAnnotatedSuperclass
Returns anAnnotatedTypeobject 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
AnnotatedTypeobject representing the annotated superclass, ornull if this class representsObject, 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:
- This class
- Parent classes in child-to-parent order
- For each class, interfaces declared on that class and their parent interfaces
- 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
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:
- This class
- Parent classes in child-to-parent order
- For each class, interfaces declared on that class and their parent interfaces
- 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 theClassLoaderfor 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 returnsnull - non-array types returnthis 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 inAnnotationInfoobjects.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@Beanannotations, this method returns each@Beanannotation separately, rather than the container annotation.- Returns:
- An unmodifiable list of
AnnotationInfowrappers 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 benull .- 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 benull .- 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 asClass.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 benull .- 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 methodgetAllMethods()- Returns all declared methods on this class and parents (all visibility levels)getPublicMethods()- Returns public methods only on this class and parents
Notes:
- Unlike Java's
Class.getDeclaredMethods(), results are filtered to exclude synthetic methods like$jacocoInit .
- Returns:
- All methods declared on this class (all visibility levels).
Results are ordered alphabetically.
List is unmodifiable.
-
getDeclaringClass
Returns theClassobject 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() returnsOuter class Outer { static class Nested {} } -Nested.getDeclaringClass() returnsOuter class Outer { void method() { class Local {} } } -Local.getDeclaringClass() returnsnull - Top-level class -
getDeclaringClass() returnsnull - Anonymous class -
getDeclaringClass() returnsnull
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
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() returnsOuter class Outer { static class Nested {} } -Nested.getEnclosingClass() returnsOuter class Outer { void method() { class Local {} } } -Local.getEnclosingClass() returnsOuter class Outer { void method() { new Runnable() {...} } } - Anonymous classgetEnclosingClass() returnsOuter - Top-level class -
getEnclosingClass() returnsnull
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:
getDeclaringClass()- Returns the declaring class (only for member classes)getEnclosingConstructor()- Returns the enclosing constructor (for classes defined in constructors)getEnclosingMethod()- Returns the enclosing method (for classes defined in methods)
- Returns:
- The enclosing class, or
null if this is a top-level class.
-
getEnclosingConstructor
Returns theConstructorInfoobject 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 theMethodInfoobject 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 theTypes representing the interfaces directly implemented by this class.Returns a cached, unmodifiable list. If a superinterface is a parameterized type, the
Typereturned 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
-
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
Description copied from interface:AnnotatableReturns 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:
getLabelin interfaceAnnotatable- 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 benull .- Returns:
- The first matching method, or
null if no methods matched.
-
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
Returns the name of the underlying class.Equivalent to calling
Class.getName()orType.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" forint[] )
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:
- Java source code format (usesgetNameCanonical()'.' and"[]" ) - Simple class name without packagegetNameSimple() - Full name with type parametersgetNameFull() - Short name with type parametersgetNameShort() - Human-readable name (usesgetNameReadable()"Array" suffix)
- 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 classnull - 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- Iftrue , 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 whennameFormat isClassNameFormat.SIMPLE.arrayFormat- How to format array dimensions.- Returns:
- The formatted class name.
-
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
- Returns:
- The readable name for this class.
-
getNames
Returns all possible names for this class.- Returns:
- An array consisting of:
getNameFull()Class.getName()- Note that this might be a dup.getNameShort()getNameSimple()
-
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()orType.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() returnsnull for classes in the default package because it has no name or associatedPackage object
- Returns:
- The package of this class wrapped in a
PackageInfo, ornull if this class has no package or is in the default (unnamed) package.
- Primitive types (e.g.,
-
getPackageAnnotation
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
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:
- This class
- Parent classes in child-to-parent order
- 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 IGrandParentextends ISuperGrandParent {}interface ISuperParent {}interface IParentextends ISuperParent {}interface IChild {}// Class hierarchy: class GrandParentimplements IGrandParent {}class Parentextends GrandParentimplements IParent {}class Childextends Parentimplements IChild {}// For Child, returns (in this order): ClassInfoci = 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.) returns it's primitive class (e.g.Integer .class int.).class - Returns:
- The primitive class, or
null if class is not a primitive wrapper.
-
getPrimitiveWrapper
If this class is a primitive (e.g.) returns it's wrapper class (e.g.int .class Integer.).class - Returns:
- The wrapper class, or
null if class is not a primitive.
-
getProtectionDomain
Returns theProtectionDomainof this class.If a security manager is installed, this method requires
RuntimePermission("getProtectionDomain") .- Returns:
- The
ProtectionDomainof this class, ornull 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 benull .- 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 benull .- 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 benull .- 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
Objectclass 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@Repeatableannotation of this class.- Returns:
- The repeated annotation method on this class, or
null if it doesn't exist.
-
getResource
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
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 ofTypeVariableobjects 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
TypeVariableobjects 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 aClassInfo. Otherwise, returns thisClassInfoinstance.Example:
ClassInfo
intClass = ClassInfo.of (int .class ); ClassInfowrapper =intClass .getWrapperIfPrimitive();// Returns ClassInfo for Integer.class ClassInfostringClass = ClassInfo.of (String.class ); ClassInfosame =stringClass .getWrapperIfPrimitive();// Returns same ClassInfo -
hasAnnotation
Returnstrue 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
-
hasPackage
Returnstrue if this class is not in the root package.- Returns:
true if this class is not in the root package.
-
hasPrimitiveWrapper
Returnstrue if thegetPrimitiveWrapper()method returns a value.- Returns:
true if thegetPrimitiveWrapper()method returns a value.
-
inner
Returns the wrapped class as aClass.- Type Parameters:
T- The inner class type.- Returns:
- The wrapped class as a
Class, ornull if it's not a class (e.g. it's aParameterizedType).
-
innerType
Returns the wrapped class as aType.- Returns:
- The wrapped class as a
Type.
-
is
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
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
Returnstrue if all specified flags are applicable to this class.- Overrides:
isin classElementInfo- Parameters:
flag- The flag to test for.- Returns:
true if all specified flags are applicable to this class.
-
isAnnotation
Returnstrue if this class is an annotation.- Returns:
true if this class is an annotation.
-
isAnonymousClass
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue if this class is an array.- Returns:
true if this class is an array.
-
isChildOf
Returnstrue if this class is a child or the same asparent .- Parameters:
parent- The parent class.- Returns:
true if this class is a child or the same asparent .
-
isChildOf
Returnstrue if this class is a child or the same asparent .- Parameters:
parent- The parent class.- Returns:
true if this class is a parent or the same asparent .
-
isChildOf
Returnstrue if this class is a child or the same asparent .- Parameters:
parent- The parent class.- Returns:
true if this class is a parent or the same asparent .
-
isChildOfAny
Returnstrue if this class is a child or the same as any of theparents .- Parameters:
parents- The parents class.- Returns:
true if this class is a child or the same as any of theparents .
-
isClass
Returnstrue if this class is not an interface.- Returns:
true if this class is not an interface.
-
isCollectionOrArray
Returnstrue if this class is aCollectionor an array.- Returns:
true if this class is aCollectionor an array.
-
isDeprecated
Returnstrue if this class has the@Deprecatedannotation on it.- Returns:
true if this class has the@Deprecatedannotation on it.
-
isEnum
Returnstrue if this class is an enum.- Returns:
true if this class is an enum.
-
isInstance
Returnstrue 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
Returnstrue if this class is a local class.- Returns:
true if this class is a local class.
-
isMemberClass
Returnstrue if this class is a member class.- Returns:
true if this class is a member class.
-
isNestmateOf
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
Returnstrue if this class is a member class and not static.- Returns:
true if this class is a member class and not static.
-
isNotDeprecated
Returnstrue if this class doesn't have the@Deprecatedannotation on it.- Returns:
true if this class doesn't have the@Deprecatedannotation on it.
-
isNotLocalClass
Returnstrue if this class is a local class.- Returns:
true if this class is a local class.
-
isNotMemberClass
Returnstrue if this class is a member class.- Returns:
true if this class is a member class.
-
isNotNonStaticMemberClass
Returnsfalse if this class is a member class and not static.- Returns:
false if this class is a member class and not static.
-
isNotPrimitive
Returnstrue if this is not a primitive class.- Returns:
true if this is not a primitive class.
-
isVoid
- Returns:
true if this class is orvoid .class Voidor has the simple name"Void .
-
isNotVoid
- Returns:
true if this class is not and notvoid .class Voidand does not have the simple name"Void .
-
isParentOf
Returnstrue if this class is a parent or the same aschild .- Parameters:
child- The child class.- Returns:
true if this class is a parent or the same aschild .
-
isParentOf
Returnstrue if this class is a parent or the same aschild .Same as
isParentOf(Class)but takes in aClassInfo.Example:
ClassInfo
parent = ClassInfo.of (List.class ); ClassInfochild = 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 aschild .
-
isParentOf
Returnstrue if this class is a parent or the same aschild .- Parameters:
child- The child class.- Returns:
true if this class is a parent or the same aschild .
-
isParentOfLenient
Returnstrue if this class is a parent or the same aschild .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 aschild .
-
isParentOfLenient
Same asisParentOfLenient(Class)but takes in aClassInfo.- Parameters:
child- The child class.- Returns:
true if this class is a parent or the same aschild .
-
isParentOfLenient
Returnstrue if this class is a parent or the same aschild .- Parameters:
child- The child class.- Returns:
true if this class is a parent or the same aschild .
-
isPrimitive
Returnstrue if this is a primitive class.- Returns:
true if this is a primitive class.
-
isRecord
Returnstrue 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
Returnstrue 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@Repeatableannotation of this class.- Returns:
true if this is a repeated annotation class.
-
isRuntimeException
Returnstrue if this class is aRuntimeException.- Returns:
true if this class is aRuntimeException.
-
isSealed
Returnstrue 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
Returnstrue if this class is a child ofparent .- Parameters:
parent- The parent class.- Returns:
true if this class is a parent ofchild .
-
isSynthetic
Returnstrue 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
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 callingClass.getDeclaredConstructor().newInstance() on the underlying class.- Returns:
- A new instance of the underlying class
- Throws:
ExecutableException- Exception occurred on invoked constructor/method/field.
-
toString
-
unwrap
- 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.
-