Java examples for Reflection:Field Value
get Value By Field Name
//package com.java2s; import java.lang.reflect.Field; public class Main { public static Object getValueByFieldName(Object obj, String fieldName) { Object value = null;//from w w w . j a v a 2s . c om try { Field field = getFieldByFieldName(obj, fieldName); if (field != null) { if (field.isAccessible()) { value = field.get(obj); } else { field.setAccessible(true); value = field.get(obj); field.setAccessible(false); } } } catch (Exception e) { } return value; } public static Field getFieldByFieldName(Object obj, String fieldName) { if (obj == null || fieldName == null) { return null; } for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { return superClass.getDeclaredField(fieldName); } catch (Exception e) { } } return null; } }