Here you can find the source of findField(Class> clazz, final String field)
Parameter | Description |
---|---|
clazz | the class to search in |
field | the field to search for |
public static Field findField(Class<?> clazz, final String field)
//package com.java2s; import java.lang.reflect.Field; public class Main { /**/*from w w w .ja v a 2 s . c o m*/ * Finds a field by name. The search is performed up the class hierarchy and includes all public, protected, * default (package) and private fields which include both static and instance members. * * @param clazz the class to search in * @param field the field to search for * @return the field or null if not found */ public static Field findField(Class<?> clazz, final String field) { do { for (Field f : clazz.getDeclaredFields()) { if (f.getName().equals(field)) { return f; } } } while ((clazz = clazz.getSuperclass()) != null); return null; } /** * Finds a field by name and type. The search is performed up the class hierarchy and includes all public, * protected, default (package) and private fields which include both static and instance members. * * @param clazz the class to search in * @param field te field to search for * @param type the type of the field * @return the field or null if not found */ public static Field findField(Class<?> clazz, final String field, final Class<?> type) { do { for (Field f : clazz.getDeclaredFields()) { if (f.getName().equals(field) && f.getType().equals(type)) { return f; } } } while ((clazz = clazz.getSuperclass()) != null); return null; } }