Java Reflection Method Setter Invoke invokeSetter(Method setter, Object obj, String value)

Here you can find the source of invokeSetter(Method setter, Object obj, String value)

Description

Invokes the setter method using reflection.

License

Apache License

Parameter

Parameter Description
setter the setter method
obj the target object
value the value which is String, primitive types or wrapper types

Exception

Parameter Description
Exception an exception

Declaration

public static void invokeSetter(Method setter, Object obj, String value) throws Exception 

Method Source Code

//package com.java2s;
/*//from   ww  w .j  a  v a2 s  . c o  m
 * Copyright 2016 OPEN TONE Inc.
 *
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.Method;

public class Main {
    /**
     * Invokes the setter method using reflection.
     *
     * @param setter the setter method
     * @param obj the target object
     * @param value the value which is String, primitive types or wrapper types
     * @throws Exception
     */
    public static void invokeSetter(Method setter, Object obj, String value) throws Exception {
        Class<?>[] types = setter.getParameterTypes();

        if (types.length != 1) {
            return;
        }

        Object valueObject = convertValue(types[0], value);
        if (valueObject != null) {
            setter.invoke(obj, new Object[] { valueObject });
        }
    }

    private static Object convertValue(Class<?> type, String value) {
        if (type.equals(String.class)) {
            return value;
        } else if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
            if (value.length() == 0) {
                value = "0";
            }
            return new Integer(value);
        } else if (type.equals(Double.TYPE) || type.equals(Double.class)) {
            if (value.length() == 0) {
                value = "0";
            }
            return new Double(value);
        } else if (type.equals(Short.TYPE) || type.equals(Short.class)) {
            if (value.length() == 0) {
                value = "0";
            }
            return new Short(value);
        } else if (type.equals(Long.TYPE) || type.equals(Long.class)) {
            if (value.length() == 0) {
                value = "0";
            }
            return new Long(value);
        } else if (type.equals(Float.TYPE) || type.equals(Float.class)) {
            if (value.length() == 0) {
                value = "0";
            }
            return new Float(value);
        } else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
            if (value.length() == 0) {
                value = "false";
            }
            return new Boolean(value);
        } else if (type.equals(Character.TYPE) || type.equals(Character.class)) {
            if (value.length() == 0) {
                value = " ";
            }
            return new Character(value.charAt(0));
        }
        return null;
    }
}

Related

  1. invokeSetter(final Method setter, final Object target, Object value)
  2. invokeSetter(Method setter, Object obj, String fieldName, Object value)
  3. invokeSetter(Method setter, Object object, Object propValue)
  4. invokeSetter(Object entity, String propertyName, Object propertyValue)
  5. invokeSetter(Object o, String name, Class value1clazz, Object value)
  6. invokeSetter(Object o, String name, Object value, Class clazz)