| Modifier and Type | Method and Description |
|---|---|
static <T> Optional<Class<? extends T>> |
asSubclassIfPossible(Class<?> classToInspect,
Class<T> parent)
Returns an Optional with the same class cast as subtype of the given
parent if possible,
otherwise returns an empty optional. |
static boolean |
classExists(String name)
Returns whether the class exists and can be loaded by the current class loader.
|
static String |
getClassName(@Nullable Object object)
Returns the
class name of the object null-safely, returning "null" if the object is null. |
static String |
getSemanticName(@Nullable Class<?> clazz)
Returns a human-friendly string representation of the given class, or of the most meaningful associated type.
|
static String |
getSemanticName(@Nullable Object object)
Returns a human-friendly string representation of the given object, based on the
semantic type of the given object. |
static @Nullable Class<?> |
getSemanticType(@Nullable Object object)
Returns the "semantic type" of the given object.
|
static ClassType |
getType(Class<?> clazz)
Returns the type of the given class (see
ClassType). |
static boolean |
isRegularJavaClass(@Nullable Class<?> clazz)
Returns whether the given class is a "regular" Java class.
|
static Class<?> |
loadClassOrThrow(String name)
Loads a class or throws an
IllegalArgumentException. |
static <R> R |
processClassByType(@Nullable Class<?> clazz,
ClassTypeCallback<? extends R> typeCallback)
Calls the method on the given
ClassTypeCallback that corresponds to the given class's type and
returns the result. |
static Optional<Class<?>> |
tryLoadClass(String name)
Wraps
Class.forName(String) and swallows the ClassNotFoundException it potentially throws:
allows to load a class by name if possible, otherwise returns an empty optional. |
public static boolean classExists(String name)
name - the name of the class to try to loadpublic static Optional<Class<?>> tryLoadClass(String name)
Class.forName(String) and swallows the ClassNotFoundException it potentially throws:
allows to load a class by name if possible, otherwise returns an empty optional.
Note that errors thrown by Class.forName(String) (e.g. LinkageError) are not caught as they
typically indicate more severe issues.
name - the class name to try to loadpublic static Class<?> loadClassOrThrow(String name)
IllegalArgumentException. This method wraps Class.forName(String)
to throw a runtime exception for convenience.name - the class name to loadpublic static <T> Optional<Class<? extends T>> asSubclassIfPossible(Class<?> classToInspect, Class<T> parent)
parent if possible,
otherwise returns an empty optional.
Note that the behavior for primitive classes may be unintuitive:
Optional<? extends Integer> result1 = asSubClassIfPossible(int.class, int.class); // Optional.of(int.class)
Optional<? extends Integer> result2 = asSubClassIfPossible(int.class, Integer.class); // Optional.empty()
See PrimitiveType#toReferenceType to
unwrap primitive types.T - the parent typeclassToInspect - the class to processparent - the parent type to check if the class is an extension ofClass.asSubclass(java.lang.Class<U>)public static String getClassName(@Nullable @Nullable Object object)
class name of the object null-safely, returning "null" if the object is null.
Use getSemanticName(Object) to generate a null-safe, more user-appropriate name of the object's type.object - the object whose type should be returned as string@Nullable public static @Nullable Class<?> getSemanticType(@Nullable @Nullable Object object)
Examples:
getSemanticType(null) = null
NumericShaper.Range.ETHIOPIC.getClass() = NumericShaper$Range$1.class // or similar
getSemanticType(NumericShaper.Range.ETHIOPIC) = NumericShaper$Range.class
FunctionalInterface fiAnnotation = Runnable.class.getAnnotation(FunctionalInterface.class);
fiAnnotation.getClass() = $Proxy12.class // or similar
getSemanticType(fiAnnotation) = FunctionalInterface.class
object - the object to inspect, or nullpublic static String getSemanticName(@Nullable @Nullable Object object)
semantic type of the given object. Returns the string "null" if the object is null.
The returned class names do not include the package. Annotations are prefixed by "@". Strings of array
classes are constructed by adding [] at the end of the component type (e.g. "String[]").
Inner classes and anonymous classes contain the name as given by Class.getName() with the package removed.
For example, a Map.Entry object is returned as "Map$Entry".
object - the object to provide a user-friendly string for of its typepublic static String getSemanticName(@Nullable @Nullable Class<?> clazz)
EnumUtils.getAssociatedEnumType(java.lang.Class<?>).
Prefer using getSemanticName(Object) whenever possible, as more semantic types can be inferred based
on an object.
clazz - the class to provide a user-friendly string for of its typepublic static boolean isRegularJavaClass(@Nullable
@Nullable Class<?> clazz)
class or record keyword. This is done by exclusion: the class is
considered as a "regular class" if it is not: an interface, an enum, an array, an annotation, a proxy class,
a primitive type or void.class.
To handle more types, use getType(Class).
clazz - the class to inspect, or nullpublic static ClassType getType(Class<?> clazz)
ClassType). A class is considered as a
regular class if no other ClassType entry applies to it.clazz - the class to inspect, or null@Nullable
public static <R> R processClassByType(@Nullable
@Nullable Class<?> clazz,
ClassTypeCallback<? extends R> typeCallback)
ClassTypeCallback that corresponds to the given class's type and
returns the result.R - the callback's result typeclazz - the class to inspect and processtypeCallback - the type callback to generate a result withCopyright © 2023. All rights reserved.