Here you can find the source of setFieldValue(String name, Object instance, int value, Class cl)
public static void setFieldValue(String name, Object instance, int value, Class cl)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static boolean isThrowable = true; public static void setFieldValue(String name, Object instance, int value, Class cl) { try {//from w w w. j a v a 2s . c om getReflectField(name, instance, cl).setInt(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue int error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, boolean value, Class cl) { try { getReflectField(name, instance, cl).setBoolean(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue boolean error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, byte value, Class cl) { try { getReflectField(name, instance, cl).setByte(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue byte error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, char value, Class cl) { try { getReflectField(name, instance, cl).setChar(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue char error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, double value, Class cl) { try { getReflectField(name, instance, cl).setDouble(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue double error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, float value, Class cl) { try { getReflectField(name, instance, cl).setFloat(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue float error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, long value, Class cl) { try { getReflectField(name, instance, cl).setLong(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue long error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, Object value, Class cl) { try { getReflectField(name, instance, cl).set(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue Object error " + name + " target " + instance + " value " + value); } } } public static void setFieldValue(String name, Object instance, short value, Class cl) { try { getReflectField(name, instance, cl).setShort(instance, value); } catch (Exception e) { e.printStackTrace(); if (isThrowable) { throw new RuntimeException("setFieldValue short error " + name + " target " + instance + " value " + value); } } } private static Field getReflectField(String name, Object instance, Class cl) throws NoSuchFieldException { if (cl == null) { if (isThrowable) throw new RuntimeException("Field " + name + " declaring class is null "); else return null; } Field field; if (!cl.isInterface()) { try { field = cl.getDeclaredField(name); if (!field.isAccessible()) { field.setAccessible(true); } return field; } catch (NoSuchFieldException e) { // ignore and search next } } else { return cl.getDeclaredField(name); } throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass()); } }