import java.lang.reflect.Field;
class TesteReflection {
for (Class<?> c = onde; c != null; c = c.getSuperclass()) {
try {
return c.getDeclaredField(nome);
if (excecao == null) excecao = e;
}
}
throw excecao;
}
public static class A1 {
}
public static class A2 extends A1 {}
public static class A3 extends A2 {}
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.");
System.
out.
println("Não achou o Field naoTem."); }
System.
out.
println("Teste concluído com sucesso."); }
}
aW1wb3J0IGphdmEubGFuZy5yZWZsZWN0LkZpZWxkOwoKY2xhc3MgVGVzdGVSZWZsZWN0aW9uIHsKICAgIHB1YmxpYyBzdGF0aWMgRmllbGQgYWNoYXJGaWVsZChDbGFzczw/PiBvbmRlLCBTdHJpbmcgbm9tZSkgdGhyb3dzIE5vU3VjaEZpZWxkRXhjZXB0aW9uIHsKICAgICAgICBpZiAob25kZSA9PSBudWxsKSB0aHJvdyBuZXcgTnVsbFBvaW50ZXJFeGNlcHRpb24oKTsKICAgICAgICBpZiAobm9tZSA9PSBudWxsKSB0aHJvdyBuZXcgTnVsbFBvaW50ZXJFeGNlcHRpb24oKTsKICAgICAgICBOb1N1Y2hGaWVsZEV4Y2VwdGlvbiBleGNlY2FvID0gbnVsbDsKICAgICAgICBmb3IgKENsYXNzPD8+IGMgPSBvbmRlOyBjICE9IG51bGw7IGMgPSBjLmdldFN1cGVyY2xhc3MoKSkgewogICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgcmV0dXJuIGMuZ2V0RGVjbGFyZWRGaWVsZChub21lKTsKICAgICAgICAgICAgfSBjYXRjaCAoTm9TdWNoRmllbGRFeGNlcHRpb24gZSkgewogICAgICAgICAgICAgICAgaWYgKGV4Y2VjYW8gPT0gbnVsbCkgZXhjZWNhbyA9IGU7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdGhyb3cgZXhjZWNhbzsKICAgIH0KCiAgICBwdWJsa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