Class ReflectionDriver
- java.lang.Object
-
- nonapi.io.github.classgraph.reflection.ReflectionDriver
-
- Direct Known Subclasses:
JVMDriverReflectionDriver,NarcissusReflectionDriver,StandardReflectionDriver
abstract class ReflectionDriver extends java.lang.ObjectReflection driver
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classReflectionDriver.ClassMemberCacheCaches class members.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.reflect.MethodcanAccessMethodprivate SingletonMap<java.lang.Class<?>,ReflectionDriver.ClassMemberCache,java.lang.Exception>classToClassMemberCacheprivate static java.lang.reflect.MethodisAccessibleMethod
-
Constructor Summary
Constructors Constructor Description ReflectionDriver()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) abstract java.lang.Class<?>findClass(java.lang.String className)Find a class by name.protected java.lang.reflect.FieldfindField(java.lang.Class<?> cls, java.lang.Object obj, java.lang.String fieldName)Get the field of the class that has a given field name.protected java.lang.reflect.FieldfindInstanceField(java.lang.Object obj, java.lang.String fieldName)Get the non-static field of the class that has a given field name.protected java.lang.reflect.MethodfindInstanceMethod(java.lang.Object obj, java.lang.String methodName, java.lang.Class<?>... paramTypes)Get a non-static method by name and parameter types.protected java.lang.reflect.MethodfindMethod(java.lang.Class<?> cls, java.lang.Object obj, java.lang.String methodName, java.lang.Class<?>... paramTypes)Get a method by name and parameter types.protected java.lang.reflect.FieldfindStaticField(java.lang.Class<?> cls, java.lang.String fieldName)Get the static field of the class that has a given field name.protected java.lang.reflect.MethodfindStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... paramTypes)Get a static method by name and parameter types.(package private) abstract <T> java.lang.reflect.Constructor<T>[]getDeclaredConstructors(java.lang.Class<T> cls)Get declared constructors for class.(package private) abstract java.lang.reflect.Field[]getDeclaredFields(java.lang.Class<?> cls)Get declared fields for class.(package private) abstract java.lang.reflect.Method[]getDeclaredMethods(java.lang.Class<?> cls)Get declared methods for class.(package private) abstract java.lang.ObjectgetField(java.lang.Object object, java.lang.reflect.Field field)Get the value of a non-static field, boxing the value if necessary.(package private) abstract java.lang.ObjectgetStaticField(java.lang.reflect.Field field)Get the value of a static field, boxing the value if necessary.(package private) abstract java.lang.ObjectinvokeMethod(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object... args)Invoke a non-static method, boxing the result if necessary.(package private) abstract java.lang.ObjectinvokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... args)Invoke a static method, boxing the result if necessary.(package private) booleanisAccessible(java.lang.Object instance, java.lang.reflect.AccessibleObject fieldOrMethod)Check whether a field or method is accessible.(package private) abstract booleanmakeAccessible(java.lang.Object instance, java.lang.reflect.AccessibleObject fieldOrMethod)Make a field or method accessible.(package private) abstract voidsetField(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value)Set the value of a non-static field, unboxing the value if necessary.(package private) abstract voidsetStaticField(java.lang.reflect.Field field, java.lang.Object value)Set the value of a static field, unboxing the value if necessary.
-
-
-
Field Detail
-
classToClassMemberCache
private final SingletonMap<java.lang.Class<?>,ReflectionDriver.ClassMemberCache,java.lang.Exception> classToClassMemberCache
-
isAccessibleMethod
private static java.lang.reflect.Method isAccessibleMethod
-
canAccessMethod
private static java.lang.reflect.Method canAccessMethod
-
-
Method Detail
-
findClass
abstract java.lang.Class<?> findClass(java.lang.String className) throws java.lang.ExceptionFind a class by name.- Parameters:
className- the class name- Returns:
- the class reference
- Throws:
java.lang.Exception
-
getDeclaredMethods
abstract java.lang.reflect.Method[] getDeclaredMethods(java.lang.Class<?> cls) throws java.lang.ExceptionGet declared methods for class.- Parameters:
cls- the class- Returns:
- the declared methods
- Throws:
java.lang.Exception
-
getDeclaredConstructors
abstract <T> java.lang.reflect.Constructor<T>[] getDeclaredConstructors(java.lang.Class<T> cls) throws java.lang.ExceptionGet declared constructors for class.- Type Parameters:
T- the generic type- Parameters:
cls- the class- Returns:
- the declared constructors
- Throws:
java.lang.Exception
-
getDeclaredFields
abstract java.lang.reflect.Field[] getDeclaredFields(java.lang.Class<?> cls) throws java.lang.ExceptionGet declared fields for class.- Parameters:
cls- the class- Returns:
- the declared fields
- Throws:
java.lang.Exception
-
getField
abstract java.lang.Object getField(java.lang.Object object, java.lang.reflect.Field field) throws java.lang.ExceptionGet the value of a non-static field, boxing the value if necessary.- Parameters:
object- the object instance to get the field value fromfield- the non-static field- Returns:
- the value of the field
- Throws:
java.lang.Exception
-
setField
abstract void setField(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value) throws java.lang.ExceptionSet the value of a non-static field, unboxing the value if necessary.- Parameters:
object- the object instance to get the field value fromfield- the non-static fieldvalue- the value to set- Throws:
java.lang.Exception
-
getStaticField
abstract java.lang.Object getStaticField(java.lang.reflect.Field field) throws java.lang.ExceptionGet the value of a static field, boxing the value if necessary.- Parameters:
field- the static field- Returns:
- the static field
- Throws:
java.lang.Exception
-
setStaticField
abstract void setStaticField(java.lang.reflect.Field field, java.lang.Object value) throws java.lang.ExceptionSet the value of a static field, unboxing the value if necessary.- Parameters:
field- the static fieldvalue- the value to set- Throws:
java.lang.Exception
-
invokeMethod
abstract java.lang.Object invokeMethod(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object... args) throws java.lang.ExceptionInvoke a non-static method, boxing the result if necessary.- Parameters:
object- the object instance to invoke the method onmethod- the non-static methodargs- the method arguments (ornew Object[0]if there are no args)- Returns:
- the return value (possibly a boxed value)
- Throws:
java.lang.Exception
-
invokeStaticMethod
abstract java.lang.Object invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... args) throws java.lang.ExceptionInvoke a static method, boxing the result if necessary.- Parameters:
method- the static methodargs- the method arguments (ornew Object[0]if there are no args)- Returns:
- the return value (possibly a boxed value)
- Throws:
java.lang.Exception
-
makeAccessible
abstract boolean makeAccessible(java.lang.Object instance, java.lang.reflect.AccessibleObject fieldOrMethod)Make a field or method accessible.- Parameters:
instance- the object instance, or null if static.fieldOrMethod- the field or method.- Returns:
- true if successful.
-
isAccessible
boolean isAccessible(java.lang.Object instance, java.lang.reflect.AccessibleObject fieldOrMethod)Check whether a field or method is accessible.N.B. this is overridden in Narcissus driver to just return true, since everything is accessible to JNI.
- Parameters:
instance- the object instance, or null if static.fieldOrMethod- the field or method.- Returns:
- true if accessible.
-
findField
protected java.lang.reflect.Field findField(java.lang.Class<?> cls, java.lang.Object obj, java.lang.String fieldName) throws java.lang.ExceptionGet the field of the class that has a given field name.- Parameters:
cls- the class.obj- the object instance, or null for a static field.fieldName- The name of the field.- Returns:
- The
Fieldobject for the requested field name, or null if no such field was found in the class. - Throws:
java.lang.Exception- if the field could not be found
-
findStaticField
protected java.lang.reflect.Field findStaticField(java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.ExceptionGet the static field of the class that has a given field name.- Parameters:
cls- the class.fieldName- The name of the field.- Returns:
- The
Fieldobject for the requested field name, or null if no such field was found in the class. - Throws:
java.lang.Exception- if the field could not be found
-
findInstanceField
protected java.lang.reflect.Field findInstanceField(java.lang.Object obj, java.lang.String fieldName) throws java.lang.ExceptionGet the non-static field of the class that has a given field name.- Parameters:
obj- the object instance, or null for a static field.fieldName- The name of the field.- Returns:
- The
Fieldobject for the requested field name, or null if no such field was found in the class. - Throws:
java.lang.Exception- if the field could not be found
-
findMethod
protected java.lang.reflect.Method findMethod(java.lang.Class<?> cls, java.lang.Object obj, java.lang.String methodName, java.lang.Class<?>... paramTypes) throws java.lang.ExceptionGet a method by name and parameter types.- Parameters:
cls- the class.obj- the object instance, or null for a static method.methodName- The name of the method.paramTypes- The types of the parameters of the method. For primitive-typed parameters, use e.g. Integer.TYPE.- Returns:
- The
Methodobject for the matching method, or null if no such method was found in the class. - Throws:
java.lang.Exception- if the method could not be found.
-
findStaticMethod
protected java.lang.reflect.Method findStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... paramTypes) throws java.lang.ExceptionGet a static method by name and parameter types.- Parameters:
cls- the class.methodName- The name of the method.paramTypes- The types of the parameters of the method. For primitive-typed parameters, use e.g. Integer.TYPE.- Returns:
- The
Methodobject for the matching method, or null if no such method was found in the class. - Throws:
java.lang.Exception- if the method could not be found.
-
findInstanceMethod
protected java.lang.reflect.Method findInstanceMethod(java.lang.Object obj, java.lang.String methodName, java.lang.Class<?>... paramTypes) throws java.lang.ExceptionGet a non-static method by name and parameter types.- Parameters:
obj- the object instance, or null for a static method.methodName- The name of the method.paramTypes- The types of the parameters of the method. For primitive-typed parameters, use e.g. Integer.TYPE.- Returns:
- The
Methodobject for the matching method, or null if no such method was found in the class. - Throws:
java.lang.Exception- if the method could not be found.
-
-