Here you can find the source of setField(Object o, String fieldName, Object value)
public static void setField(Object o, String fieldName, Object value)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Field; public class Main { public static void setField(Object o, String fieldName, Object value) { try {//from www . j a v a 2 s .c om Field field = null; try { field = o.getClass().getField(fieldName); } catch (NoSuchFieldException e) { try { o.getClass().getDeclaredField(fieldName).setAccessible(true); field = o.getClass().getField(fieldName); } catch (NoSuchFieldException ex) { ex.printStackTrace(); } } field.set(o, value); } catch (Exception e) { e.printStackTrace(); } } public static Object getField(Object o, String fieldName) { try { return o.getClass().getField(fieldName).get(o); } catch (Exception e) { e.printStackTrace(); } return null; } }