public class Reflections
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Reflections.ReflectionAccessException |
static class |
Reflections.ReflectionNotFoundException |
Constructor and Description |
---|
Reflections(java.lang.ClassLoader classLoader) |
Modifier and Type | Method and Description |
---|---|
static <T> T |
cast(java.lang.Object candidate,
java.lang.Class<? extends T> type)
convenience for casting the given candidate to the given type (without any coercion, and allowing candidate to be null)
|
static <T> void |
copyFields(T source,
T target)
copies all fields from the source to target; very little compile-time safety checking, so use with care
|
static java.lang.reflect.Field |
findField(java.lang.Class<?> clazz,
java.lang.String name) |
static java.lang.reflect.Method |
findMethod(java.lang.Class<?> clazz,
java.lang.String name,
java.lang.Class<?>... parameterTypes) |
static java.util.List<java.lang.reflect.Field> |
findPublicFieldsOrderedBySuper(java.lang.Class<?> clazz) |
static java.util.List<java.lang.reflect.Method> |
findPublicMethodsOrderedBySuper(java.lang.Class<?> clazz) |
static <T> java.lang.Class<? super T> |
findSuperType(T impl,
java.lang.String typeName) |
static java.util.List<java.lang.Class<?>> |
getAllInterfaces(java.lang.Class<?> type)
Gets all the interfaces implemented by the given type, including its parent classes.
|
java.lang.ClassLoader |
getClassLoader() |
static java.util.Set<java.lang.Class<?>> |
getInterfacesIncludingClassAncestors(java.lang.Class<?> clazz)
whereas Class.getInterfaces() only returns interfaces directly implemented by a class,
this walks the inheritance hierarchy to include interfaces implemented by superclass/ancestors;
(note it does not include superinterfaces)
|
java.net.URL |
getResource(java.lang.String r)
finds the resource in the classloader, if it exists; inserts or replaces leading slash as necessary
(i believe it should _not_ have one, but there is some inconsistency)
Will return null if no resource is found.
|
static boolean |
hasNoArgConstructor(java.lang.Class<?> clazz) |
static boolean |
hasNoNonObjectFields(java.lang.Class<? extends java.lang.Object> clazz) |
static java.lang.Class<?> |
inferSubbest(java.lang.Class<?> c1,
java.lang.Class<?> c2)
Gets the class that is in the sub-class; or null if neither is a sub-class of the other.
|
static java.lang.reflect.Field |
inferSubbestField(java.lang.reflect.Field f1,
java.lang.reflect.Field f2)
Gets the field that is in the sub-class; or null if one field does not come from a sub-class of the other field's class
|
static java.lang.reflect.Method |
inferSubbestMethod(java.lang.reflect.Method m1,
java.lang.reflect.Method m2)
Gets the method that is in the sub-class; or null if one method does not come from a sub-class of the other method's class
|
static <T> com.google.common.base.Optional<T> |
invokeConstructorWithArgs(java.lang.Class<T> clazz,
java.lang.Object... argsArray)
Invokes a suitable constructor, supporting varargs and primitives
|
static <T> com.google.common.base.Optional<T> |
invokeConstructorWithArgs(java.lang.Class<T> clazz,
java.lang.Object[] argsArray,
boolean setAccessible)
Invokes a suitable constructor, supporting varargs and primitives
|
static <T> com.google.common.base.Optional<T> |
invokeConstructorWithArgs(java.lang.ClassLoader classLoader,
java.lang.Class<T> clazz,
java.lang.Object[] argsArray,
boolean setAccessible)
Invokes a suitable constructor, supporting varargs and primitives
|
static <T> com.google.common.base.Optional<T> |
invokeConstructorWithArgs(java.lang.ClassLoader classLoader,
java.lang.String className,
java.lang.Object... argsArray)
Invokes a suitable constructor, supporting varargs and primitives
|
static <T> com.google.common.base.Optional<T> |
invokeConstructorWithArgs(Reflections reflections,
java.lang.Class<T> clazz,
java.lang.Object[] argsArray,
boolean setAccessible)
Invokes a suitable constructor, supporting varargs and primitives, additionally supporting setAccessible
|
java.lang.Object |
invokeMethod(java.lang.reflect.Method method,
java.lang.Object obj,
java.lang.Object... argValues) |
static com.google.common.base.Optional<java.lang.Object> |
invokeMethodWithArgs(java.lang.Object clazzOrInstance,
java.lang.String method,
java.util.List<java.lang.Object> args)
invokes the given method on the given clazz or instance, doing reasonably good matching on args etc
|
static com.google.common.base.Optional<java.lang.Object> |
invokeMethodWithArgs(java.lang.Object clazzOrInstance,
java.lang.String method,
java.util.List<java.lang.Object> args,
boolean setAccessible) |
java.lang.Object |
invokeStaticMethod(java.lang.reflect.Method method,
java.lang.Object... argValues) |
java.lang.Class<?> |
loadClass(java.lang.String classname)
instantiates the given class from its binary name
|
<T> java.lang.Class<? extends T> |
loadClass(java.lang.String classname,
java.lang.Class<T> superType) |
java.lang.Class<?> |
loadClassFromCanonicalName(java.lang.String canonicalName)
Loads class given its canonical name format (e.g.
|
java.lang.reflect.Constructor<?> |
loadConstructor(java.lang.Class<?> clazz,
java.lang.Class<?>[] argTypes) |
java.lang.Class<?> |
loadInnerClassNotInheritted(java.lang.Class<?> outerClazz,
java.lang.String innerClassname)
does not look through ancestors of outer class
|
java.lang.Class<?> |
loadInnerClassNotInheritted(java.lang.String outerClassname,
java.lang.String innerClassname)
does not look through ancestors of outer class
|
static java.lang.Class<?> |
loadInnerClassPossiblyInheritted(java.lang.Class<?> clazz,
java.lang.String nestedPart)
given a nested part, e.g.
|
<T> T |
loadInstance(java.lang.reflect.Constructor<T> constructor,
java.lang.Object... argValues) |
java.lang.Object |
loadInstance(java.lang.String classname) |
java.lang.Object |
loadInstance(java.lang.String classname,
java.lang.Class<?>[] argTypes,
java.lang.Object[] argValues) |
java.lang.Object |
loadInstance(java.lang.String classname,
java.lang.Object... argValues) |
java.lang.reflect.Method |
loadMethod(java.lang.Class<?> clazz,
java.lang.String methodName)
returns the first method matching the given name
|
java.lang.reflect.Method |
loadMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>[] argTypes) |
java.lang.reflect.Constructor<?> |
loadSingleConstructor(java.lang.Class<?> clazz)
returns a single constructor in a given class, or throws an exception
|
java.lang.Object |
loadStaticField(java.lang.Class<?> clazz,
java.lang.String fieldname) |
java.lang.Object[] |
loadStaticFields(java.lang.Class<?> clazz,
java.lang.String[] fieldnamesArray,
java.lang.Object[] defaults) |
static boolean |
typesMatch(java.lang.Object[] argsArray,
java.lang.Class<?>[] parameterTypes)
true iff all args match the corresponding types
|
static boolean |
typesMatchUpTo(java.lang.Object[] argsArray,
java.lang.Class<?>[] parameterTypes,
int lengthRequired)
true iff the initial N args match the corresponding types
|
java.lang.Object |
updateFromNewClassLoader(java.lang.Object data)
Serialize the given object, then reload using the current class loader;
this removes linkages to instances with classes loaded by an older class loader.
|
public java.lang.Object loadInstance(java.lang.String classname, java.lang.Object... argValues) throws Reflections.ReflectionNotFoundException, Reflections.ReflectionAccessException
public java.lang.Object loadInstance(java.lang.String classname, java.lang.Class<?>[] argTypes, java.lang.Object[] argValues) throws Reflections.ReflectionNotFoundException, Reflections.ReflectionAccessException
public java.lang.Object loadInstance(java.lang.String classname) throws Reflections.ReflectionNotFoundException, Reflections.ReflectionAccessException
public java.lang.Class<?> loadClass(java.lang.String classname) throws Reflections.ReflectionNotFoundException
public <T> java.lang.Class<? extends T> loadClass(java.lang.String classname, java.lang.Class<T> superType) throws Reflections.ReflectionNotFoundException
public static java.lang.Class<?> loadInnerClassPossiblyInheritted(java.lang.Class<?> clazz, java.lang.String nestedPart) throws Reflections.ReflectionNotFoundException
so it will find Clazz.Inner.VeryInner wherever in the hierarchy it is defined
(as opposed to ClassLoader which requires Inner.VeryInner to be _declared_ in clazz, not in any supertype
returns null if not found
public java.lang.Class<?> loadInnerClassNotInheritted(java.lang.String outerClassname, java.lang.String innerClassname) throws Reflections.ReflectionNotFoundException
public java.lang.Class<?> loadInnerClassNotInheritted(java.lang.Class<?> outerClazz, java.lang.String innerClassname) throws Reflections.ReflectionNotFoundException
uses the classloader set in this class, not in the clazz supplied
public java.lang.reflect.Constructor<?> loadConstructor(java.lang.Class<?> clazz, java.lang.Class<?>[] argTypes) throws Reflections.ReflectionAccessException
public static <T> com.google.common.base.Optional<T> invokeConstructorWithArgs(java.lang.ClassLoader classLoader, java.lang.String className, java.lang.Object... argsArray)
public static <T> com.google.common.base.Optional<T> invokeConstructorWithArgs(java.lang.ClassLoader classLoader, java.lang.Class<T> clazz, java.lang.Object[] argsArray, boolean setAccessible)
public static <T> com.google.common.base.Optional<T> invokeConstructorWithArgs(java.lang.Class<T> clazz, java.lang.Object... argsArray)
public static <T> com.google.common.base.Optional<T> invokeConstructorWithArgs(java.lang.Class<T> clazz, java.lang.Object[] argsArray, boolean setAccessible)
public static <T> com.google.common.base.Optional<T> invokeConstructorWithArgs(Reflections reflections, java.lang.Class<T> clazz, java.lang.Object[] argsArray, boolean setAccessible)
public java.lang.reflect.Constructor<?> loadSingleConstructor(java.lang.Class<?> clazz)
public <T> T loadInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... argValues) throws java.lang.IllegalArgumentException, Reflections.ReflectionAccessException
java.lang.IllegalArgumentException
Reflections.ReflectionAccessException
public java.lang.reflect.Method loadMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>[] argTypes) throws Reflections.ReflectionNotFoundException, Reflections.ReflectionAccessException
public java.lang.reflect.Method loadMethod(java.lang.Class<?> clazz, java.lang.String methodName) throws Reflections.ReflectionNotFoundException, Reflections.ReflectionAccessException
public java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... argValues) throws Reflections.ReflectionAccessException
Reflections.ReflectionAccessException
- If invocation failed due to illegal access or the invoked method failedjava.lang.IllegalArgumentException
- If the arguments were invalidpublic java.lang.Object invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... argValues) throws java.lang.IllegalArgumentException, Reflections.ReflectionAccessException
java.lang.IllegalArgumentException
Reflections.ReflectionAccessException
public java.lang.Object loadStaticField(java.lang.Class<?> clazz, java.lang.String fieldname) throws Reflections.ReflectionAccessException
public java.lang.Object[] loadStaticFields(java.lang.Class<?> clazz, java.lang.String[] fieldnamesArray, java.lang.Object[] defaults) throws Reflections.ReflectionAccessException
public static <T> void copyFields(T source, T target) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
public java.lang.Class<?> loadClassFromCanonicalName(java.lang.String canonicalName) throws java.lang.ClassNotFoundException, Reflections.ReflectionNotFoundException
Reflections.ReflectionNotFoundException
java.lang.ClassNotFoundException
@Nullable public java.net.URL getResource(java.lang.String r)
public final java.lang.Object updateFromNewClassLoader(java.lang.Object data) throws java.io.IOException, java.lang.ClassNotFoundException
(like a poor man's clone)
aka "reconstitute(Object)"
java.io.IOException
java.lang.ClassNotFoundException
public java.lang.ClassLoader getClassLoader()
public static <T> java.lang.Class<? super T> findSuperType(T impl, java.lang.String typeName)
public static java.util.Set<java.lang.Class<?>> getInterfacesIncludingClassAncestors(java.lang.Class<?> clazz)
public static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
public static java.lang.reflect.Field findField(java.lang.Class<?> clazz, java.lang.String name) throws java.lang.NoSuchFieldException
java.lang.NoSuchFieldException
public static java.util.List<java.lang.reflect.Field> findPublicFieldsOrderedBySuper(java.lang.Class<?> clazz)
public static java.util.List<java.lang.reflect.Method> findPublicMethodsOrderedBySuper(java.lang.Class<?> clazz)
public static java.lang.reflect.Field inferSubbestField(java.lang.reflect.Field f1, java.lang.reflect.Field f2)
public static java.lang.reflect.Method inferSubbestMethod(java.lang.reflect.Method m1, java.lang.reflect.Method m2)
public static java.lang.Class<?> inferSubbest(java.lang.Class<?> c1, java.lang.Class<?> c2)
public static <T> T cast(java.lang.Object candidate, java.lang.Class<? extends T> type)
public static com.google.common.base.Optional<java.lang.Object> invokeMethodWithArgs(java.lang.Object clazzOrInstance, java.lang.String method, java.util.List<java.lang.Object> args) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
public static com.google.common.base.Optional<java.lang.Object> invokeMethodWithArgs(java.lang.Object clazzOrInstance, java.lang.String method, java.util.List<java.lang.Object> args, boolean setAccessible) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
public static boolean typesMatch(java.lang.Object[] argsArray, java.lang.Class<?>[] parameterTypes)
public static boolean typesMatchUpTo(java.lang.Object[] argsArray, java.lang.Class<?>[] parameterTypes, int lengthRequired)
public static java.util.List<java.lang.Class<?>> getAllInterfaces(@Nullable java.lang.Class<?> type)
type
- the class to look uppublic static boolean hasNoArgConstructor(java.lang.Class<?> clazz)
public static boolean hasNoNonObjectFields(java.lang.Class<? extends java.lang.Object> clazz)