Here you can find the source of setField(Object obj, Object value, String fieldName)
public static void setField(Object obj, Object value, String fieldName)
//package com.java2s; /**//www . j a va 2 s .co m * Zephyrus * * @author minnymin3 * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html) * */ import java.lang.reflect.Field; public class Main { public static void setField(Object obj, Object value, String fieldName) { try { getField(obj.getClass(), fieldName).set(obj, value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } /** * Retrieves a field * @param cl The class to retrieve the field from * @param fieldName The name of the field * @return */ public static Field getField(Class<?> cl, String fieldName) { try { Field field = cl.getDeclaredField(fieldName); return field; } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } return null; } }