Here you can find the source of setField(Object obj, String fieldName, Object value)
public static void setField(Object obj, String fieldName, Object value)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.*; public class Main { public static void setField(Object obj, String fieldName, Object value) { try {//w w w .ja v a 2 s . c om Class c = obj.getClass(); Field f = c.getField(fieldName); f.set(obj, value); } catch (Exception e) { } ; } public static void setField(Object obj, String fieldName, double value) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); f.setDouble(obj, value); } catch (Exception e) { } ; } public static void setField(Object obj, String fieldName, int value) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); f.setInt(obj, value); } catch (Exception e) { } ; } public static void setField(Object obj, String fieldName, long value) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); f.setLong(obj, value); } catch (Exception e) { } ; } public static void setField(Object obj, String fieldName, short value) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); f.setShort(obj, value); } catch (Exception e) { } ; } public static void setField(Object obj, String fieldName, byte value) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); f.setByte(obj, value); } catch (Exception e) { } ; } public static void setField(Object obj, String fieldName, char value) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); f.setChar(obj, value); } catch (Exception e) { } ; } public static void setField(Object obj, String fieldName, boolean value) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); f.setBoolean(obj, value); } catch (Exception e) { } ; } public static Object getField(Object obj, String fieldName) { try { Class c = obj.getClass(); Field f = c.getField(fieldName); return f.get(obj); } catch (Exception e) { } ; return null; } public static final Object GetField(Object obj, String field) { Field fld = null; try { Class c = obj.getClass(); fld = c.getField(field); } catch (Exception e) { System.err.println(e); e.printStackTrace(); } if (fld != null) { try { return fld.get(obj); } catch (Exception e) { System.err.println(e); e.printStackTrace(); } } else { System.err.println("ERROR: NO FIELD " + field + " FOUND IN CLASS " + obj.getClass().getName()); } return null; } }