If the statement below is called thousands of times, it will be very slow if there are many classes that do not have the a field named 'id'. Is there a way to get a field by name using reflection in java that will not throw an exception if the field does not exist?
Field f2 = null;
try{
f2 = classThatMightNotHaveFieldId.getClass().getDeclaredField("id");
f2.setAccessible(true);
}catch(Exception e){
System.out.println(f1.get(classThatMightNotHaveFieldId));
}
Aucun commentaire:
Enregistrer un commentaire