jTransfo, 0.10

org.jtransfo.internal
Class ReflectionHelper

java.lang.Object
  extended by org.jtransfo.internal.ReflectionHelper

public class ReflectionHelper
extends Object

Helper class for all things class manipulation and reflection.


Constructor Summary
ReflectionHelper()
           
 
Method Summary
 List<Field> getFields(Class<?> clazz)
          Find all declared fields of a class.
 Method getMethod(Class<?> type, Class<?> returnType, String name, Class<?>... parameters)
          Get method with given name and parameters and given return type.
 List<SyntheticField> getSyntheticFields(Class<?> clazz)
          Find all declared (synthetic) fields of a class.
<T> Class<T>
loadClass(String name)
          Load class with given name from the correct class loader.
 void makeAccessible(Field field)
          Make the given field accessible, explicitly setting it accessible if necessary.
 List<SyntheticField> makeSynthetic(Class<?> clazz, List<Field> fields)
          Convert list of (real) fields to synthetic fields (which use accessor methods).
<T> T
newInstance(Class<T> clazz)
          Create a new instance of a class.
<T> T
newInstance(String className)
          Create a new instance of a class given the class name.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionHelper

public ReflectionHelper()
Method Detail

newInstance

public <T> T newInstance(Class<T> clazz)
              throws IllegalAccessException,
                     InstantiationException
Create a new instance of a class.

Provided to allow mocking, making it possible to test the exception handling.

Type Parameters:
T - type of object to create
Parameters:
clazz - class to create new instance of
Returns:
new class instance
Throws:
IllegalAccessException - see Class.newInstance()
InstantiationException - see Class.newInstance()

newInstance

public <T> T newInstance(String className)
              throws IllegalAccessException,
                     InstantiationException,
                     ClassNotFoundException
Create a new instance of a class given the class name.

Provided to allow mocking, making it possible to test the exception handling.

Type Parameters:
T - type of object to create
Parameters:
className - name of class to create new instance of
Returns:
new class instance
Throws:
IllegalAccessException - see Class.newInstance()
InstantiationException - see Class.newInstance()
ClassNotFoundException - see ClassLoader.loadClass(String)

loadClass

public <T> Class<T> loadClass(String name)
                   throws ClassNotFoundException
Load class with given name from the correct class loader.

Type Parameters:
T - type of object to create
Parameters:
name - name of class to load
Returns:
class instance
Throws:
ClassNotFoundException - see ClassLoader.loadClass(String)

getFields

public List<Field> getFields(Class<?> clazz)
Find all declared fields of a class. Fields which are hidden by a child class are not included.

Parameters:
clazz - class to find fields for
Returns:
list of fields

makeSynthetic

public List<SyntheticField> makeSynthetic(Class<?> clazz,
                                          List<Field> fields)
Convert list of (real) fields to synthetic fields (which use accessor methods).

Parameters:
clazz - class which contains the fields
fields - fields to convert to synthetic fields
Returns:
list of synthetic fields

getSyntheticFields

public List<SyntheticField> getSyntheticFields(Class<?> clazz)
Find all declared (synthetic) fields of a class.

Parameters:
clazz - class to find fields for
Returns:
list of fields

makeAccessible

public void makeAccessible(Field field)
Make the given field accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).

This method is borrowed from Spring's ReflectionUtil class.

Parameters:
field - the field to make accessible
See Also:
AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)

getMethod

public Method getMethod(Class<?> type,
                        Class<?> returnType,
                        String name,
                        Class<?>... parameters)
Get method with given name and parameters and given return type.

Parameters:
type - class on which method should be found
returnType - required return type (or null for void or no check)
name - method name
parameters - method parameter types
Returns:
method or null when method not found

jTransfo, 0.10

Copyright © 2013. All rights reserved.