com-pas / compas-sct

System Configuration Tool (SCT) components
Apache License 2.0
9 stars 9 forks source link

[RSR-530] - Fix Utils#getField to not log error when field is on a super class #113

Closed massifben closed 1 year ago

massifben commented 2 years ago

Subject of the issue

This method org.lfenergy.compas.sct.commons.Utils#getField(Class<?> clazz, String name) logs an error if the field is declared on the super class, yet returning the correct answer. It should log an error only if the field is not found. Plus, the method uses Exception as control flow which is generally an anti-pattern.

Your environment

Steps to reproduce

Execute : Utils.getField(TBDA.class, "count");.

Expected behaviour

There should be no error log in the console.

Actual behaviour

The return Field is correct, but in the log you get : [main] ERROR org.lfenergy.compas.sct.commons.Utils: Cannot find field name count java.lang.NoSuchFieldException: count at java.base/java.lang.Class.getDeclaredField(Class.java:2411) at org.lfenergy.compas.sct.commons.Utils.getField(Utils.java:60) at org.lfenergy.compas.sct.commons.UtilsTest.getField(UtilsTest.java:14) ...

massifben commented 1 year ago

Commit 6dcdd01f6c844e1d51d3d2cd25b3fc71592e4aff introduced isSet and unset methods on JAXB generated classes. We no longer need reflection.