I tried to create a way to compare any two any Objects and its fields, with the option to exclude fields when needed.
It works but when I tested it its 30 times slower than a equals Method (in which i can't exclude fields).
Is there a way to make the reflection faster ?
Or is it generally bad to use it at all ? And if so, is there a way to make a dynamical but genereic compare option so that I can exclude fields? I genereally Use miltiple comperators for issues like that, but I want to make it more "easy" if thats a good idea.
private static boolean compareFieldsWithBlacklist(Class clazz, Object o1, Object o2, List<String> blackList)
throws IllegalAccessException {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (!blackList.contains(field.getName())) {
field.setAccessible(true);
Object value1 = field.get(o1);
Object value2 = field.get(o2);
if (!value1.equals(value2))
return false;
}
}
return true;
}
I also tried to instanciate the Fields Array only once when i try to compare a collection. But it only made it a little bit faster.