Let's say I have a class with a string field named "myfield", and use reflection to get the field, I've found that Object.getClass().getDeclaredField("myfield");
is case sensitive, it will throw an NoSuchFieldException
if I for example use Object.getClass().getDeclaredField("MyField");
Is there any way around it? forcing it to ignore case?
Thanks
Just use Class.getDeclaredFields()
and look through the results performing a case-insensitive match yourself.
No, there's no such way. You can get all fields and search through them:
Field[] fields = src.getClass().getDeclaredFields();
for(Field f:fields){
if(f.getName().equalsIgnoreCase("myfield")){
//stuff.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With