Java Reflection Method Getter Invoke invokeGetter(Object target, Method getter)

Here you can find the source of invokeGetter(Object target, Method getter)

Description

call getter and return object.

License

Open Source License

Parameter

Parameter Description
target object on which to call getter
getter method for some field

Return

value stored in field

Declaration

public static Object invokeGetter(Object target, Method getter) 

Method Source Code


//package com.java2s;
/*//from  w w  w .java 2 s  .  c  o  m
 *   $Id$
 *
 *   Copyright 2006 University of Dundee. All rights reserved.
 *   Use is subject to license terms supplied in LICENSE.txt
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**
     * call getter and return object.
     * 
     * @DEV.TODO there maybe be cases where an exception is ok
     * @param target
     *            object on which to call getter
     * @param getter
     *            method for some field
     * @return value stored in field
     */
    public static Object invokeGetter(Object target, Method getter) {
        RuntimeException re = new RuntimeException("Error trying to get value: " + getter.toString());
        Object result = null;
        try {
            result = getter.invoke(target, new Object[] {});
        } catch (IllegalArgumentException e) {
            re.initCause(e);
            throw re;
        } catch (IllegalAccessException e) {
            re.initCause(e);
            throw re;
        } catch (InvocationTargetException e) {
            re.initCause(e);
            throw re;
        }
        return result;
    }
}

Related

  1. invokeGetter(Object getterOwner, Method method)
  2. invokeGetter(Object o, String name, boolean forceAccess)
  3. invokeGetter(Object obj, String methodName)
  4. invokeGetter(Object obj, String methodName, int defaultValue)
  5. invokeGetter(Object object, String field)
  6. invokeGetterMethod(final Method method, final Object object)
  7. invokeGetterSafe(Object o, String name, boolean forceAccess)
  8. invokeSetter(Object target, Class clazz, String methodName, String getterMethod, Object param)
  9. invokeStringGetterSafe(Object o, String name)