Here you can find the source of setFieldValeByType(Field field, Object obj, String value)
public static void setFieldValeByType(Field field, Object obj, String value) throws Exception
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setFieldValeByType(Field field, Object obj, String value) throws Exception { Class<?> type = field.getType(); String typeName = type.getName(); if (typeName.equals("int")) { if (value.equals("")) { value = "0"; }//from w ww .j av a 2 s . c om field.set(obj, Integer.valueOf(value)); } else if (typeName.equals("long")) { if (value.equals("")) { value = "0"; } field.set(obj, Long.valueOf(value)); } else if (typeName.equals("boolean")) { if (value.equals("")) { value = "false"; } field.set(obj, Boolean.valueOf(value)); } else if (typeName.equals("double")) { if (value.equals("")) { value = "0.0"; } field.set(obj, Double.valueOf(value)); } else { field.set(obj, value); } } }