jeudi 4 mars 2021

Alternative to BeanUtils Java (error SonarQube)

I'm currently looking for a way to set dynamically a property into a Object, I have this:

public static void assign(Object instance, String attribute, Object value) {
    try {
        if (value != null) {
            BeanUtils.setProperty(instance, attribute, value);
        }
    } catch (IllegalAccessException | InvocationTargetException ex) {
        throw new TechnicalException(ex.getMessage(), ex);
    }
}

But Sonarqube throw me an error:

"Make sure that setting JavaBean properties is safe here."

So I need an alternative to fill my object dynamically or a way to avoid the exception in "BeanUtils.setProperty" (Avoid the exception because due to the nature of the project there are no risks of vulnerabilities).

Or some way to receive an object, and assign a field with a specific value by converting the data type of the value dynamically.

Any idea?

Beforehand thank you very much.





Aucun commentaire:

Enregistrer un commentaire