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.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.lang.reflect.Field>
fieldNameToField
private java.util.Map<java.lang.String,java.util.List<java.lang.reflect.Method>>
methodNameToMethods
-
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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.reflect.Field
getField(java.lang.String fieldName)
Get the field of the class that has a given field name.java.lang.reflect.Method
getMethod(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.
-
-
-
Constructor Detail
-
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 Detail
-
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
Field
object 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
Method
objects 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
Method
object for the matching method, or null if no such method was found in the class.
-
-