Java Reflection Field Set setFields(Field field, Object value, Object config)

Here you can find the source of setFields(Field field, Object value, Object config)

Description

set Fields

License

Apache License

Declaration

public static boolean setFields(Field field, Object value, Object config) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    public static boolean setFields(Field field, Object value, Object config) {
        boolean flag = true;
        Class<?> clazz = field.getType();
        try {//from  w ww  .j ava 2s  .com
            switch (clazz.getName()) {
            case "boolean":
                field.set(config, Boolean.valueOf((String) value));
                break;
            case "int":
                field.set(config, Integer.valueOf((String) value));
                break;
            case "long":
                field.set(config, Long.valueOf((String) value));
                break;
            case "java.lang.String":
                field.set(config, (String) value);
                break;
            default:
                flag = false;
                break;
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {
            flag = false;
        }
        return flag;

    }
}

Related

  1. setFieldIfExists(final Object instance, final String fieldName, final Object value)
  2. setFieldObject(Class clazz, String field, Object object, Object newObj)
  3. setFieldObject(Class target, Object targetObject, String fieldName, Object object)
  4. setFieldObjectValue(Class targetClass, Object target, String fieldName, Object value)
  5. setFieldPrimitive(Object target, Field field, Object value)
  6. setFields(final Map fields, final Object target)
  7. setFieldStatic(final Object pBean, final String fieldname, final Object pInject)
  8. setFieldVal(Object object, String name, Object val)
  9. setFieldValeByType(Field field, Object obj, String value)