public class ReflectUtil
extends java.lang.Object
(Use @Jailbreak to avoid writing reflection code. See Type-safe Reflection.)
| Modifier and Type | Class and Description |
|---|---|
static class |
ReflectUtil.ConstructorRef |
static class |
ReflectUtil.FieldRef |
static class |
ReflectUtil.LiveFieldRef |
static class |
ReflectUtil.LiveMethodRef |
static class |
ReflectUtil.MethodRef |
static class |
ReflectUtil.WithNull
Use to access live methods and fields with possible null return value if not found
|
| Constructor and Description |
|---|
ReflectUtil() |
| Modifier and Type | Method and Description |
|---|---|
static ReflectUtil.ConstructorRef |
constructor(java.lang.Class<?> cls,
java.lang.Class<?>... params)
Get a
ReflectUtil.ConstructorRef to the specified constructor. |
static ReflectUtil.ConstructorRef |
constructor(java.lang.String fqn,
java.lang.Class<?>... params)
Get a
ReflectUtil.ConstructorRef to the specified constructor. |
static ReflectUtil.FieldRef |
field(java.lang.Class<?> cls,
java.lang.String name)
Get a
ReflectUtil.FieldRef to the specified field. |
static ReflectUtil.LiveFieldRef |
field(java.lang.Object receiver,
java.lang.String name)
Get a
ReflectUtil.LiveFieldRef to the specified field. |
static ReflectUtil.FieldRef |
field(java.lang.String fqn,
java.lang.String name)
Get a
ReflectUtil.FieldRef to the specified field. |
static ReflectUtil.MethodRef |
lambdaMethod(java.lang.Class<?> lambdaClass)
Get a
ReflectUtil.MethodRef corresponding with the functional interface implemented by lambdaClass. |
static ReflectUtil.MethodRef |
method(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class... params)
Get a
ReflectUtil.MethodRef to the specified method. |
static ReflectUtil.LiveMethodRef |
method(java.lang.Object receiver,
java.lang.String name,
java.lang.Class... params)
Get a
ReflectUtil.LiveMethodRef to the specified method. |
static ReflectUtil.MethodRef |
method(java.lang.String fqn,
java.lang.String name,
java.lang.Class... params)
Get a
ReflectUtil.MethodRef to the specified method. |
static ReflectUtil.MethodRef |
methodFromName(java.lang.Class<?> cls,
java.lang.String name)
Get a
ReflectUtil.MethodRef to from the specified name without regard to parameter types. |
static void |
preloadClassIntoParentLoader(java.lang.String fqn,
java.net.URI content,
java.lang.ClassLoader wouldBeLoader,
java.lang.ClassLoader parentLoader)
Force class with name
fqn to be loaded by parentLoader. |
static void |
setAccessible(java.lang.reflect.Constructor c) |
static void |
setAccessible(java.lang.reflect.Field f) |
static void |
setAccessible(java.lang.reflect.Member m) |
static void |
setAccessible(java.lang.reflect.Method m) |
static java.lang.Object |
structuralCall(java.lang.reflect.Method structMethod,
java.lang.Object receiver,
java.lang.Object... args) |
static ReflectUtil.MethodRef |
structuralMethod(java.lang.Class target,
java.lang.Class structIface,
java.lang.String name,
java.lang.Class... params) |
static ReflectUtil.LiveMethodRef |
structuralMethod(java.lang.Object receiver,
java.lang.Class structIface,
java.lang.String name,
java.lang.Class... params) |
static java.lang.Class<?> |
type(java.lang.String fqn)
Searches the class loader of this class for the specified name, if not found searches
the current thread's context class loader.
|
static java.lang.Class<?> |
type(java.lang.String fqn,
boolean useCallChain) |
static java.lang.Class<?> |
type(java.lang.String fqn,
java.lang.ClassLoader cl)
Searches
cl for the specified class fqn. |
static java.lang.Class<?> |
type(java.lang.String fqn,
java.lang.ClassLoader cl,
boolean useCallChain) |
public static java.lang.Class<?> type(java.lang.String fqn)
fqn - The qualified name of the type e.g., "java.lang.String" or "java.lang.String[]"Class corresponding with fqn or null if not foundpublic static java.lang.Class<?> type(java.lang.String fqn,
boolean useCallChain)
public static java.lang.Class<?> type(java.lang.String fqn,
java.lang.ClassLoader cl)
cl for the specified class fqn.fqn - The qualified name of the type e.g., "java.lang.String"cl - The class loader to searchClass corresponding with fqn or null if not foundpublic static java.lang.Class<?> type(java.lang.String fqn,
java.lang.ClassLoader cl,
boolean useCallChain)
public static ReflectUtil.LiveMethodRef method(java.lang.Object receiver, java.lang.String name, java.lang.Class... params)
ReflectUtil.LiveMethodRef to the specified method. Typical use:
method(str, "substring", int.class).invoke(2)
receiver - The object to make the call onname - The name of the method to call or a '|' separated list of names, where the first found is usedparams - The types of the method's parametersRuntimeException if the method is not found.
Use ReflectUtil.WithNull to avoid the RuntimeException.public static ReflectUtil.MethodRef method(java.lang.String fqn, java.lang.String name, java.lang.Class... params)
ReflectUtil.MethodRef to the specified method. Typical use:
method("java.time.LocalTime", "of", int.class, int.class).invokeStatic(5, 30) fqn - The qualified name of the class containing the methodname - The name of the method or a '|' separated list of names, where the first found is usedparams - The types of the method's parameterspublic static ReflectUtil.MethodRef method(java.lang.Class<?> cls, java.lang.String name, java.lang.Class... params)
ReflectUtil.MethodRef to the specified method. Typical use:
method(LocalTime.class, "of", int.class, int.class).invokeStatic(5, 30)
cls - The class containing the methodname - The name of the method or a '|' separated list of names, where the first found is usedparams - The types of the method's parameterspublic static ReflectUtil.MethodRef methodFromName(java.lang.Class<?> cls, java.lang.String name)
ReflectUtil.MethodRef to from the specified name without regard to parameter types. If more than one
method has the name, the first one encountered is used, in no particular order. This method should be used
only when the named method is not overloaded. Typical use:
methodByName(LocalTime.class, "isAfter").invoke(source, time)
cls - The class containing the methodname - The name of the method or a '|' separated list of names, where the first found is usedpublic static ReflectUtil.MethodRef lambdaMethod(java.lang.Class<?> lambdaClass)
ReflectUtil.MethodRef corresponding with the functional interface implemented by lambdaClass.
lambdaMethod(function.getClass()).invoke(function, args)
public static ReflectUtil.LiveFieldRef field(java.lang.Object receiver, java.lang.String name)
ReflectUtil.LiveFieldRef to the specified field. Typical use:
String name = field(foo, "name").get();
receiver - The object having the fieldname - The name of the field or a '|' separated list of names, where the first found is usedRuntimeException if the field is not found.
Use ReflectUtil.WithNull to avoid the RuntimeException.public static ReflectUtil.FieldRef field(java.lang.String fqn, java.lang.String name)
ReflectUtil.FieldRef to the specified field. Typical use:
field("java.time.LocalTime", "hour").get(); fqn - The qualified name of the class having the fieldname - The name of the field or a '|' separated list of names, where the first found is usedpublic static ReflectUtil.FieldRef field(java.lang.Class<?> cls, java.lang.String name)
ReflectUtil.FieldRef to the specified field. Typical use:
field(LocalTime.class, "hour").get();
cls - The class having the fieldname - The name of the field or a '|' separated list of names, where the first found is usedpublic static ReflectUtil.ConstructorRef constructor(java.lang.String fqn, java.lang.Class<?>... params)
ReflectUtil.ConstructorRef to the specified constructor. Typical use:
constructor("java.util.ArrayList", int.class).newInstance(32) fqn - The qualified name of the class to constructparams - A list of parameter types for the constructorpublic static ReflectUtil.ConstructorRef constructor(java.lang.Class<?> cls, java.lang.Class<?>... params)
ReflectUtil.ConstructorRef to the specified constructor. Typical use:
constructor(ArrayList.class, int.class).newInstance(32)
cls - The class to constructparams - A list of parameter types for the constructorpublic static void setAccessible(java.lang.reflect.Field f)
public static void setAccessible(java.lang.reflect.Method m)
public static void setAccessible(java.lang.reflect.Constructor c)
public static void setAccessible(java.lang.reflect.Member m)
public static void preloadClassIntoParentLoader(java.lang.String fqn,
java.net.URI content,
java.lang.ClassLoader wouldBeLoader,
java.lang.ClassLoader parentLoader)
fqn to be loaded by parentLoader. Facilitates the case where a class
must be declared in a package defined in a parent class loader in order to subclass and use package-local
features defined there.
Note fqn's natural class loader must have the parentLoader in its chain of parent loaders.
Also be certain fqn is not already loaded by its natural loader, otherwise LinkageErrors
will result.
fqn - The qualified name of the class to loadcontent - The location of the class resource. With Java 8 this can be wouldBeLoader.getResource(className).
But with Java 9 and later the JPMS strictly prohibits a package from existing in two loaders,
therefore the class file must be placed in a different package, perhaps prefixed with a
suitably named root package, otherwise the VM will throw a LayerInstantiationException
when your application loads, before any of your code executes.wouldBeLoader - The class loader that would naturally load fqn, must have parentLoader in its
parent loader chainparentLoader - The class loader to load the class in, must be in the parent chain of wouldBeLoaderpublic static ReflectUtil.MethodRef structuralMethod(java.lang.Class target, java.lang.Class structIface, java.lang.String name, java.lang.Class... params)
public static ReflectUtil.LiveMethodRef structuralMethod(java.lang.Object receiver, java.lang.Class structIface, java.lang.String name, java.lang.Class... params)
public static java.lang.Object structuralCall(java.lang.reflect.Method structMethod,
java.lang.Object receiver,
java.lang.Object... args)
Copyright © 2021. All rights reserved.