Java Method Call invoke(Object obj, String methodName, Class clazz, Object newValue)

Here you can find the source of invoke(Object obj, String methodName, Class clazz, Object newValue)

Description

Invokes the specified setter 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.

Declaration

public static Object invoke(Object obj, String methodName, Class clazz,
        Object newValue) throws NoSuchMethodException 

Method Source Code

//package com.java2s;
/*// w  w w . j  a  v  a  2s  .  c o m
 * @(#)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 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. invoke(Object obj, String method, Class[] params, Object[] args)
  2. invoke(Object obj, String method, Object param, Object paramType)
  3. invoke(Object obj, String methodName)
  4. invoke(Object obj, String methodName, boolean newValue)
  5. invoke(Object obj, String methodName, Class argType, Object arg)
  6. invoke(Object obj, String methodName, Class[] parameterTypes, Object[] args)
  7. invoke(Object obj, String methodName, Object... args)
  8. invoke(Object obj, String methodName, Object... params)
  9. invoke(Object obj, String methodName, Object[] params)