Java Reflection Method Setter Invoke invokeSetter(final Method setter, final Object target, Object value)

Here you can find the source of invokeSetter(final Method setter, final Object target, Object value)

Description

Conveniance method used to invoke a JavaBeans-compatible setter method.

License

Apache License

Parameter

Parameter Description
setter Method to invoke.
target Target object on which 'setter' should be invoked.
value Value to pass to the setter.

Exception

Parameter Description
IllegalAccessException normal reflection exception
InvocationTargetException normal reflection exception

Declaration

public static void invokeSetter(final Method setter, final Object target, Object value)
        throws IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
/**//from   w  ww .j  a v  a2 s.  c  o  m
 * A collection of small utilities for component management and reflection handling.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

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

public class Main {
    /**
     * Conveniance method used to invoke a JavaBeans-compatible setter method. As far as possible, this method will take
     * care of parameter conversions necessary for reflection access.
     *
     * @param setter Method to invoke.
     * @param target Target object on which 'setter' should be invoked.
     * @param value  Value to pass to the setter.
     * @throws IllegalAccessException    normal reflection exception
     * @throws InvocationTargetException normal reflection exception
     */
    public static void invokeSetter(final Method setter, final Object target, Object value)
            throws IllegalAccessException, InvocationTargetException {

        if (value instanceof Number)
            value = convertNumber(setter.getParameterTypes()[0], (Number) value);
        setter.invoke(target, value);
    }

    /**
     * This method converts a given number into a target class. This method does not change the value (except when
     * explicitly casting to a more general type, e.g. from double to int), just the internal type representation. While
     * this is unnecessary while using normal java code, reflection based access to method parameters is a bit more
     * difficult. As far as possible, this method will prevent the ArgumentMismatch error when passing numbers as
     * parameters.
     * <p/>
     * If the value can not be converted to the given target class, it will be returned unchanged.
     *
     * @param targetClass Class to which the number should be converted, if possible.
     * @param value       Number value to convert.
     * @return 'value' converted to an instance of 'targetClass'.
     */
    public static Object convertNumber(final Class targetClass, final Number value) {
        if (targetClass.equals(Double.class) || targetClass.equals(Double.TYPE))
            return value.doubleValue();
        if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE))
            return value.intValue();
        if (targetClass.equals(Long.class) || targetClass.equals(Long.TYPE))
            return value.longValue();
        if (targetClass.equals(Short.class) || targetClass.equals(Short.TYPE))
            return value.shortValue();
        if (targetClass.equals(Byte.class) || targetClass.equals(Byte.TYPE))
            return value.byteValue();
        if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE))
            return value.intValue();
        if (targetClass.equals(Float.class) || targetClass.equals(Float.TYPE))
            return value.floatValue();
        return value;
    }
}

Related

  1. invokeSetter(Method setter, Object obj, String fieldName, Object value)
  2. invokeSetter(Method setter, Object obj, String value)
  3. invokeSetter(Method setter, Object object, Object propValue)
  4. invokeSetter(Object entity, String propertyName, Object propertyValue)