Here you can find the source of findField(Object obj, String fieldName)
Parameter | Description |
---|---|
obj | a parameter |
fieldName | a parameter |
public static Field findField(Object obj, String fieldName)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { /**/*from w ww. ja va2s .c o m*/ * Find a field in given object * * @param obj * @param fieldName * @return */ public static Field findField(Object obj, String fieldName) { Class<?> clazz = obj.getClass(); Field field = null; while (clazz != null && field == null) { try { field = clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException | SecurityException e) { // Do nothing } clazz = clazz.getSuperclass(); } return field; } }