Code example for Field

Methods: get, setAccessible

0
   */ 
  public<T> T getFieldValue(String fieldName, Class<T> fieldClassType)
      throws NoSuchFieldException, IllegalAccessException 
  { 
    Field field = getField(fieldName);
    Object value = field.get(_classInstance);
    return fieldClassType.cast(value);
  } 
 
  /** 
   * Set the value of the field which is not accessible[private] 
   * @param fieldName The name of the field to be set 
   * @param fieldValue Value to set for the field 
   * @throws NoSuchFieldException 
   * @throws IllegalAccessException 
   */ 
  public void setFieldValue(String fieldName, Object fieldValue)
      throws NoSuchFieldException, IllegalAccessException 
  { 
    getField(fieldName).set(_classInstance, fieldValue);
  } 
 
  /** 
   * Invoke any method (including private) of the class using this method. 
   * @param methodName The method name that needs to be invoked 
   * @param args The parameters that are needed for the invokation of the method 
   */ 
  public Object invokeMethod(String methodName, Object... args)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException 
  { 
    Method method = getMethod(methodName);
    return method.invoke(_classInstance,args);
  } 
 
  //=================== Getter and setter method for the class type (not the instance) ================================ 
 
  /** 
   * Get access to the method for future invocations. The method changes the visibility of private members as well. 
   * Get the 'Method' (Java type Method, not the actual field from the instance). 
   * @param methodName The name of the method 
   * @return 
   * @throws NoSuchMethodException 
   */ 
  public Method getMethod(String methodName)
      throws NoSuchMethodException 
  { 
    return methodUnwinder(methodName,_classType);
  } 
 
  /** 
   * Get the 'Field' (Java type Field, not the actual field from the instance). 
   * @param fieldName The name of the field whose visibility is to be changed 
   * @return 
   * @throws NoSuchFieldException 
   */ 
  public Field getField(String fieldName)
      throws NoSuchFieldException 
  { 
    return fieldUnwinder(fieldName, _classType);
  } 
 
  //==================================== Helper methods for the introspection class =================================== 
 
  /** 
   * Recursively goes through the class's hierarchy to search for the particular field 
   */ 
  private static<T> Field fieldUnwinder(String fieldName, Class<T> classType)
      throws NoSuchFieldException 
  { 
    Field field = null;
 
    if(LOG.isDebugEnabled())
      LOG.debug("Peeking into classInstance: " + classType);
 
    try 
    { 
      field = classType.getDeclaredField(fieldName);
    } 
    catch (NoSuchFieldException e)
    { 
      //If the field does not exist, let's check if it exists in the parent class. 
      Class<?> superClass = classType.getSuperclass();
      if(superClass == null)
        throw e;
 
      return fieldUnwinder(fieldName, superClass);
    } 
 
    field.setAccessible(true);
    return field;
  } 
 
  /** 
   * Recursively goes through the class's hierarchy to search for the particular method