Java Reflection Method Getter Invoke invokeGetter(Object obj, String methodName, int defaultValue)

Here you can find the source of invokeGetter(Object obj, String methodName, int defaultValue)

Description

Invokes the specified getter method if it exists.

License

Open Source License

Parameter

Parameter Description
obj The object on which to invoke the method.
methodName The name of the method.
defaultValue This value is returned, if the method does not exist.

Return

The value returned by the getter method or the default value.

Declaration

public static int invokeGetter(Object obj, String methodName,
        int defaultValue) 

Method Source Code

//package com.java2s;
/*/*  w  w w. j  a  v a 2 s  .  c om*/
 * @(#)Methods.java  1.0.1 2006-06-25
 *
 * Copyright (c) 1996-2006 by the original authors of JHotDraw
 * and all its contributors ("JHotDraw.org")
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JHotDraw.org ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with
 * JHotDraw.org.
 */

import java.lang.reflect.*;

public class Main {
    /**
     * Invokes the specified getter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     * @param defaultValue This value is returned, if the method does not exist.
     * @return  The value returned by the getter method or the default value.
     */
    public static int invokeGetter(Object obj, String methodName,
            int defaultValue) {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[0]);
            Object result = method.invoke(obj, new Object[0]);
            return ((Integer) result).intValue();
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }

    /**
     * Invokes the specified getter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     * @param defaultValue This value is returned, if the method does not exist.
     * @return  The value returned by the getter method or the default value.
     */
    public static long invokeGetter(Object obj, String methodName,
            long defaultValue) {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[0]);
            Object result = method.invoke(obj, new Object[0]);
            return ((Long) result).longValue();
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }

    /**
     * Invokes the specified getter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     * @param defaultValue This value is returned, if the method does not exist.
     * @return The value returned by the getter method or the default value.
     */
    public static boolean invokeGetter(Object obj, String methodName,
            boolean defaultValue) {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[0]);
            Object result = method.invoke(obj, new Object[0]);
            return ((Boolean) result).booleanValue();
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }

    /**
     * Invokes the specified getter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     * @param defaultValue This value is returned, if the method does not exist.
     * @return The value returned by the getter method or the default value.
     */
    public static Object invokeGetter(Object obj, String methodName,
            Object defaultValue) {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[0]);
            Object result = method.invoke(obj, new Object[0]);
            return result;
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }

    /**
     * Invokes the specified accessible parameterless method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     * @return The return value of the method.
     * @return NoSuchMethodException if the method does not exist or is not
     * accessible.
     */
    public static Object invoke(Object obj, String methodName)
            throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[0]);
            Object result = method.invoke(obj, new Object[0]);
            return result;
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName
                    + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    /**
     * Invokes the specified accessible method with a string parameter if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     * @param stringParameter The String parameter
     * @return The return value of the method or METHOD_NOT_FOUND.
     * @return NoSuchMethodException if the method does not exist or is not accessible.
     */
    public static Object invoke(Object obj, String methodName,
            String stringParameter) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[] { String.class });
            Object result = method.invoke(obj,
                    new Object[] { stringParameter });
            return result;
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName
                    + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    /**
     * Invokes the specified setter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     */
    public static Object invoke(Object obj, String methodName,
            boolean newValue) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[] { Boolean.TYPE });
            return method.invoke(obj,
                    new Object[] { new Boolean(newValue) });
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName
                    + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    /**
     * Invokes the specified method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     */
    public static Object invoke(Object obj, String methodName, int newValue)
            throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[] { Integer.TYPE });
            return method.invoke(obj,
                    new Object[] { new Integer(newValue) });
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName
                    + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    /**
     * Invokes the specified setter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     */
    public static Object invoke(Object obj, String methodName,
            float newValue) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[] { Float.TYPE });
            return method.invoke(obj, new Object[] { new Float(newValue) });
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName
                    + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    /**
     * Invokes the specified setter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     */
    public static Object invoke(Object obj, String methodName, Class clazz,
            Object newValue) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName,
                    new Class[] { clazz });
            return method.invoke(obj, new Object[] { newValue });
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName
                    + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    /**
     * Invokes the specified setter method if it exists.
     *
     * @param obj The object on which to invoke the method.
     * @param methodName The name of the method.
     */
    public static Object invoke(Object obj, String methodName,
            Class[] clazz, Object... newValue) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName, clazz);
            return method.invoke(obj, newValue);
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName
                    + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            InternalError error = new InternalError(e.getMessage());
            error.initCause((e.getCause() != null) ? e.getCause() : e);
            throw error;
        }
    }
}

Related

  1. invokeGetter(Object bean, Method getter)
  2. invokeGetter(Object entity, String propertyName)
  3. invokeGetter(Object getterOwner, Method method)
  4. invokeGetter(Object o, String name, boolean forceAccess)
  5. invokeGetter(Object obj, String methodName)
  6. invokeGetter(Object object, String field)
  7. invokeGetter(Object target, Method getter)
  8. invokeGetterMethod(final Method method, final Object object)
  9. invokeGetterSafe(Object o, String name, boolean forceAccess)