Here you can find the source of setFieldPrimitive(Object target, Field field, Object value)
public static void setFieldPrimitive(Object target, Field field, Object value)
//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); } } }