Java Reflection Field Set setFieldPrimitive(Object target, Field field, Object value)

Here you can find the source of setFieldPrimitive(Object target, Field field, Object value)

Description

set Field Primitive

License

Open Source License

Declaration

public static void setFieldPrimitive(Object target, Field field, Object value) 

Method Source Code

//package com.java2s;
/**/*from ww w .j  av a  2 s.  c  om*/
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2014, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import java.lang.reflect.Field;

public class Main {
    public static void setFieldPrimitive(Object target, Field field, Object value) {
        try {
            final Class<?> c = field.getType();
            if (c == boolean.class) {
                field.setBoolean(target, (Boolean) value);
            } else if (c == byte.class) {
                field.setByte(target, ((Number) value).byteValue());
            } else if (c == char.class) {
                field.setChar(target, (Character) value);
            } else if (c == short.class) {
                field.setShort(target, ((Number) value).shortValue());
            } else if (c == int.class) {
                field.setInt(target, ((Number) value).intValue());
            } else if (c == float.class) {
                field.setFloat(target, ((Number) value).floatValue());
            } else if (c == long.class) {
                field.setLong(target, ((Number) value).longValue());
            } else if (c == double.class) {
                field.setDouble(target, ((Number) value).doubleValue());
            } else {
                throw new RuntimeException("WTF ? --->  [" + c.getName() + "]");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

    }
}

Related

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