patterntesting.runtime.util
Class ReflectionHelper

Package class diagram package ReflectionHelper
java.lang.Object
  extended by patterntesting.runtime.util.ReflectionHelper

public class ReflectionHelper
extends Object

This class is a helper class to access some fields via reflection. Normally you should avoid reflection. Handle with care if you use it.

Since:
09.03.2009
Version:
$Revision: 1.12 $
Author:
oliver

Method Summary
static Field getField(Class<?> cl, String name)
          Tries to get the wanted field.
static Object getFieldValue(Object obj, String name)
          If it can it returns the value of the given field.
static Method getMethod(Class<?> cl, String name, Class<?>... parameterTypes)
          Gets the method.
static Method getMethod(Class<?> cl, String name, Object... args)
          Gets the method.
static Collection<Field> getUninitializedNonStaticFields(Object obj)
          To get all uninitialized field you can call this method.
static Object invokeMethod(Object target, String name, Object... args)
          Invoke method.
static boolean isStatic(Field field)
          Checks if the given field is static.
static String toShortString(Collection<Field> fields)
          To short string.
static String toShortString(Field field)
          To short string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getField

public static Field getField(Class<?> cl,
                             String name)
                      throws NoSuchFieldException
Tries to get the wanted field. Before it is returned the acc

Parameters:
cl - the cl
name - the name
Returns:
the wanted field
Throws:
NoSuchFieldException - the no such field exception

getFieldValue

public static Object getFieldValue(Object obj,
                                   String name)
                            throws NoSuchFieldException,
                                   IllegalArgumentException,
                                   IllegalAccessException
If it can it returns the value of the given field. If not it throws an exception.

Parameters:
obj - the obj
name - the name
Returns:
the field value
Throws:
NoSuchFieldException - the no such field exception
IllegalArgumentException - the illegal argument exception
IllegalAccessException - the illegal access exception

getUninitializedNonStaticFields

public static Collection<Field> getUninitializedNonStaticFields(Object obj)
To get all uninitialized field you can call this method.

Parameters:
obj - the object where to get the fields from
Returns:
a collecion of unitialized fields (can be empty)

isStatic

public static boolean isStatic(Field field)
Checks if the given field is static.

Parameters:
field - the field
Returns:
true, if is static

toShortString

public static String toShortString(Field field)
To short string.

Parameters:
field - the field
Returns:
the string

toShortString

public static String toShortString(Collection<Field> fields)
To short string.

Parameters:
fields - the fields
Returns:
the string

getMethod

public static Method getMethod(Class<?> cl,
                               String name,
                               Object... args)
                        throws NoSuchMethodException
Gets the method.

Parameters:
cl - the cl
name - the name
args - the args
Returns:
the method
Throws:
NoSuchMethodException - the no such method exception

getMethod

public static Method getMethod(Class<?> cl,
                               String name,
                               Class<?>... parameterTypes)
                        throws NoSuchMethodException
Gets the method.

Parameters:
cl - e.g. class java.lang.ClassLoader
name - e.g. "getPackages"
parameterTypes - the parameter types
Returns:
a Method object
Throws:
NoSuchMethodException - the no such method exception

invokeMethod

public static Object invokeMethod(Object target,
                                  String name,
                                  Object... args)
Invoke method. This could be also a protected or private method.

Parameters:
target - the target
name - the method name
args - the args for the method
Returns:
the result of the method.


Copyright © 2002–2014 PatternTesting Team. All rights reserved.