While getting my code reviewed by my senior I got a comment to not use Reflection because it is a security vulnerability in companies and also it makes code very slow. Now when I execute it on my IDE then I can't see the difference because it runs immediately. I searched a lot but I didn't get any clear explanation anywhere why this is so.
I had a list of objects containing a lot of fields from which I have to use just a few and the rest needed to stay blank. This object is coming from an external API. We have a spring-boot project.
public void setNullExceptGivenFields(List<String> fieldsToExcludeWhileSettingNull) {
try {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (!field.getClass().isPrimitive() && !fieldsToExcludeWhileSettingNull.contains(field.getName())
&& field.get(this) != null) {
field.set(this, null);
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
LOGGER.info("Exception occured while setting fields null " + e.getMessage());
} catch (Exception e) {
LOGGER.info("Unhandled exception occured");
}
}
Any help will be appreciated.