Android examples for java.lang.reflect:Field Name
get Field from Class by name
//package com.java2s; import android.util.NoSuchPropertyException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Main { public static Field getField(Class clazz, String name) throws NoSuchPropertyException { if (clazz == null || clazz == Object.class) { throw new NoSuchPropertyException("Field not found: " + name); }// ww w . j a va2s. c om Field field; try { field = clazz.getDeclaredField(name); } catch (NoSuchFieldException e) { field = getField(clazz, clazz.getSuperclass(), name); } if (Modifier.isPrivate(field.getModifiers())) { field.setAccessible(true); } return field; } private static Field getField(Class root, Class clazz, String name) { if (clazz == null || clazz == Object.class) { throw new NoSuchPropertyException("Field [" + name + "] not found in " + root.getName()); } Field field; try { field = clazz.getDeclaredField(name); } catch (NoSuchFieldException e) { field = getField(clazz, clazz.getSuperclass(), name); } if (Modifier.isPrivate(field.getModifiers())) { field.setAccessible(true); } return field; } }