Java tutorial
//package com.java2s; import android.support.annotation.NonNull; import java.lang.reflect.Field; public class Main { public static <T> T getVar(@NonNull Object obj, @NonNull String name) throws Exception { for (Class cls = obj.getClass(); // cls != null && cls != Object.class; // cls = cls.getSuperclass()) { for (Field f : cls.getDeclaredFields()) { if (f.getName().equals(name)) { f.setAccessible(true); //noinspection unchecked return (T) f.get(obj); } } } throw new RuntimeException("no var matching " + name); } }