Java Reflection Field Set setField(Field field, Object obj, String value)

Here you can find the source of setField(Field field, Object obj, String value)

Description

Set field value using reflection.

License

Apache License

Parameter

Parameter Description
field the field
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 setField(Field field, Object obj, String value) throws Exception 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .  j a va2s  .  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.Field;

public class Main {
    /**
     * Set field value using reflection.
     *
     * @param field the field
     * @param obj the target object
     * @param value the value which is String, primitive types or wrapper types
     * @throws Exception
     */
    public static void setField(Field field, Object obj, String value) throws Exception {
        Class<?> type = field.getType();

        Object valueObject = convertValue(type, value);
        if (valueObject != null) {
            field.set(obj, 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. setField(Field f, Object instance, Object value)
  2. setField(Field f, Object this_, Object value)
  3. setField(Field field, @Nullable Object instance, Object thing)
  4. setField(Field field, Object classWithField, Object value)
  5. setField(Field field, Object instance, Object value)
  6. setField(Field field, Object objToSet, Object value)
  7. setField(Field field, Object target, Object value)
  8. setField(Field field, Object target, Object value)
  9. setField(Field field, Object target, Object value)