Modifier and Type | Method and Description |
---|---|
static <T> Class<T> |
forName(String fqcn)
Attempts to load the specified class name from the current thread's
context class loader , then the
current ClassLoader (Classes.class.getClassLoader() ), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader() , in that order. |
static <T> Constructor<T> |
getConstructor(Class<T> clazz,
Class<?>... argTypes)
|
static <T> T |
getFieldValue(Object instance,
String fieldName,
Class<T> fieldType)
Returns the
instance 's named (declared) field value. |
static InputStream |
getResourceAsStream(String name)
Returns the specified resource by checking the current thread's
context class loader , then the
current ClassLoader (Classes.class.getClassLoader() ), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader() , in that order, using
getResourceAsStream(name) . |
static <T> T |
instantiate(Constructor<T> ctor,
Object... args)
Creates a new object using the specified
Constructor , invoking it with the specified constructor
args arguments. |
static <T> T |
invokeStatic(Class<?> clazz,
String methodName,
Class<?>[] argTypes,
Object... args)
Invokes the
clazz 's matching static method (named methodName with exact argument types
of argTypes ) with the given args arguments, and returns the method return value. |
static <T> T |
invokeStatic(String fqcn,
String methodName,
Class<?>[] argTypes,
Object... args)
Invokes the fully qualified class name's method named
methodName with parameters of type argTypes
using the args as the method arguments. |
static boolean |
isAvailable(String fullyQualifiedClassName)
Returns
true if the specified fullyQualifiedClassName can be found in any of the thread
context, class, or system classloaders, or false otherwise. |
static <T> T |
newInstance(Class<T> clazz)
Creates a new instance of the specified
clazz via clazz.newInstance() . |
static <T> T |
newInstance(Class<T> clazz,
Object... args)
Returns a new instance of the specified
clazz , invoking the associated constructor with the specified
args arguments. |
static <T> T |
newInstance(String fqcn)
Creates and returns a new instance of the class with the specified fully qualified class name using the
classes default no-argument constructor.
|
static <T> T |
newInstance(String fqcn,
Class<?>[] ctorArgTypes,
Object... args)
Creates and returns a new instance of the specified fully qualified class name using the
specified
args arguments provided to the constructor with ctorArgTypes |
static <T> T |
newInstance(String fqcn,
Object... args)
Creates and returns a new instance of the specified fully qualified class name using a constructor that matches
the specified
args arguments. |
public static <T> Class<T> forName(String fqcn) throws UnknownClassException
context class loader
, then the
current ClassLoader (Classes.class.getClassLoader()
), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader()
, in that order. If any of them cannot locate
the specified class, an UnknownClassException
is thrown (our RuntimeException equivalent of
the JRE's ClassNotFoundException
.T
- The type of Class returnedfqcn
- the fully qualified class name to loadUnknownClassException
- if the class cannot be found.public static InputStream getResourceAsStream(String name)
context class loader
, then the
current ClassLoader (Classes.class.getClassLoader()
), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader()
, in that order, using
getResourceAsStream(name)
.name
- the name of the resource to acquire from the classloader(s).null
if the resource cannot be found from any
of the three mentioned ClassLoaders.public static boolean isAvailable(String fullyQualifiedClassName)
true
if the specified fullyQualifiedClassName
can be found in any of the thread
context, class, or system classloaders, or false
otherwise.fullyQualifiedClassName
- the fully qualified class name to checktrue
if the specified fullyQualifiedClassName
can be found in any of the thread
context, class, or system classloaders, or false
otherwise.public static <T> T newInstance(String fqcn)
T
- the type of object createdfqcn
- the fully qualified class namepublic static <T> T newInstance(String fqcn, Class<?>[] ctorArgTypes, Object... args)
args
arguments provided to the constructor with ctorArgTypes
T
- the type of object createdfqcn
- the fully qualified class namectorArgTypes
- the argument types of the constructor to invokeargs
- the arguments to supply when invoking the constructorpublic static <T> T newInstance(String fqcn, Object... args)
args
arguments.T
- the type of the object createdfqcn
- fully qualified class nameargs
- the arguments to supply to the constructorpublic static <T> T newInstance(Class<T> clazz)
clazz
via clazz.newInstance()
.T
- the type of the object createdclazz
- the class to invokepublic static <T> T newInstance(Class<T> clazz, Object... args)
clazz
, invoking the associated constructor with the specified
args
arguments.T
- the type of the created objectclazz
- the class to invokeargs
- the arguments matching an associated class constructorpublic static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... argTypes) throws IllegalStateException
T
- the type of object to createclazz
- the class to inspectargTypes
- the argument types for the desired constructorIllegalStateException
- if the constructor for the specified argTypes
does not exist.public static <T> T instantiate(Constructor<T> ctor, Object... args)
Constructor
, invoking it with the specified constructor
args
arguments.T
- the type of object to creatector
- the constructor to invokeargs
- the arguments to supply to the constructorInstantiationException
- if the constructor cannot be invoked successfullypublic static <T> T invokeStatic(String fqcn, String methodName, Class<?>[] argTypes, Object... args)
methodName
with parameters of type argTypes
using the args
as the method arguments.T
- the expected type of the object returned from the invoked method.fqcn
- fully qualified class name to locatemethodName
- name of the method to invoke on the classargTypes
- the method argument types supported by the methodName
methodargs
- the runtime arguments to use when invoking the located class methodpublic static <T> T invokeStatic(Class<?> clazz, String methodName, Class<?>[] argTypes, Object... args)
clazz
's matching static method (named methodName
with exact argument types
of argTypes
) with the given args
arguments, and returns the method return value.T
- the type of object expected to be returned from the methodclazz
- the class to invokemethodName
- the name of the static method on clazz
to invokeargTypes
- the types of the arguments accepted by the methodargs
- the actual runtime arguments to use when invoking the methodpublic static <T> T getFieldValue(Object instance, String fieldName, Class<T> fieldType)
instance
's named (declared) field value.T
- field instance value typeinstance
- the instance with the internal fieldfieldName
- the name of the field to inspectfieldType
- the type of field to inspectCopyright © 2014–2025 jsonwebtoken.io. All rights reserved.