Package io.github.toolfactory.narcissus
Class ReflectionCache
java.lang.Object
io.github.toolfactory.narcissus.ReflectionCache
public class ReflectionCache
extends java.lang.Object
ReflectionCache -- caches fields and methods of a class. Use this if you need to use reflection to access
multiple methods and/or fields, for speed.
- Author:
- Luke Hutchison
-
Constructor Summary
Constructors Constructor Description ReflectionCache(java.lang.Class<?> cls)Instantiate a new reflection cache for a class.ReflectionCache(java.lang.String className)Instantiate a new reflection cache for a named class. -
Method Summary
Modifier and Type Method Description java.lang.reflect.FieldgetField(java.lang.String fieldName)Get the field of the class that has a given field name.java.lang.reflect.MethodgetMethod(java.lang.String methodName, java.lang.Class<?>... paramTypes)Get a method by name and parameter types.java.util.List<java.lang.reflect.Method>getMethods(java.lang.String methodName)Get all methods in the class for a given method name.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
ReflectionCache
public ReflectionCache(java.lang.Class<?> cls)Instantiate a new reflection cache for a class.- Parameters:
cls- the class to instantiate.
-
ReflectionCache
public ReflectionCache(java.lang.String className)Instantiate a new reflection cache for a named class.Finds the class by name (e.g.
"com.xyz.MyClass") using the current classloader or the system classloader. Finds array classes if the class name is of the form"com.xyz.MyClass[][]".- Parameters:
className- the name of the class to instantiate.
-
-
Method Details
-
getField
public java.lang.reflect.Field getField(java.lang.String fieldName)Get the field of the class that has a given field name.- Parameters:
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.
-
getMethods
public java.util.List<java.lang.reflect.Method> getMethods(java.lang.String methodName)Get all methods in the class for a given method name. (There may be multiple methods with the same name but different parameter types).- Parameters:
methodName- The name of the method.- Returns:
- A list of
Methodobjects for methods of the requested method name, or null if no such method was found in the class.
-
getMethod
public java.lang.reflect.Method getMethod(java.lang.String methodName, java.lang.Class<?>... paramTypes)Get a method by name and parameter types.- Parameters:
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.
-