vendredi 3 mars 2023

In java, can I compare child-of-child elements that share a class if their parent elements don't share a class or superclass?

I'm trying to map a response object from a rest call in our sprint java application. As this is on my work computer I'll simplify and rename the issue:

I get back a rest object containing plenty of already-mapped fields and a ShapeSelect object. The ShapeSelect object contains many kinds of specific shape objects that do not inherit from a common class, IE:

TriangleObject t;
SquareObject s;
PentagonObject p;

and so forth - in total about 25 such objects. Importantly, for each response object only one of these shape objects will be instantiated - the rest will be null.

Each of these specific shapes have a child element of the same class: BaseShape, which contains the fields and methods I need to map. Ex:

BaseShape base = response.getShapeSelect.getTriangleObject().getBaseShape();

So: I need to identify the ShapeSelect element that isn't null and grab the getBaseShape from that element 1) without using a long series of if statements and 2) in a way that our sonarQube scanner won't complain about.

My current plan is to use reflect to iterate across the fields of ShapeSelect until I find the non-null shape, then iterate over the methods looking for a method name that matches getBaseShape... I will report back on this, but I've been informed that it will probably run afoul of sonarQube. and I'm struggling to think of another way to accomplish this.





Aucun commentaire:

Enregistrer un commentaire