import java.lang.reflect.Field;

class TesteReflection {
    public static Field acharField(Class<?> onde, String nome) throws NoSuchFieldException {
        if (onde == null) throw new NullPointerException();
        if (nome == null) throw new NullPointerException();
        NoSuchFieldException excecao = null;
        for (Class<?> c = onde; c != null; c = c.getSuperclass()) {
            try {
                return c.getDeclaredField(nome);
            } catch (NoSuchFieldException e) {
                if (excecao == null) excecao = e;
            }
        }
        throw excecao;
    }

    public static class A1 {
        private String teste;
    }

    public static class A2 extends A1 {}
    public static class A3 extends A2 {}

    public static void main(String[] args) throws NoSuchFieldException {
        Field f = acharField(A3.class, "teste");
        System.out.println("Achou o Field " + f.getName() + ".");
        try {
        	acharField(A3.class, "naoTem");
        	throw new AssertionError("Ops, não deveria chear aqui.");
        } catch (NoSuchFieldException e) {
        	System.out.println("Não achou o Field naoTem.");
        }
        System.out.println("Teste concluído com sucesso.");
    }
}