Search code examples
javareflection

Java Reflection, Ignore case when using GetDeclaredField


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


Solution

  • Just use Class.getDeclaredFields() and look through the results performing a case-insensitive match yourself.