Here you can find the source of setField(Object object, String fieldName, Object value)
public static void setField(Object object, 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 object, String fieldName, Object value) { try {/*from w w w. j a v a 2s . c om*/ Field field = object.getClass().getField(fieldName); field.set(object, value); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } public static Object getField(Object object, String fieldName) { try { Field field = object.getClass().getField(fieldName); return field.get(object); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); return null; } } private static Class<?> getClass(String clazz) { try { return Class.forName(clazz); } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } } }