podam icon indicating copy to clipboard operation
podam copied to clipboard

Podam strategy for POJO with overloaded setters

Open janlukasik opened this issue 9 years ago • 8 comments

I'm using a class with overloaded setter method in my project. Could you please tell me how Podam selects a setter to be used? Is there a way - having two or more setters - choose the one that I would like to use?

janlukasik avatar Jan 07 '17 14:01 janlukasik

Hi, Podam uses a setter, which is defined in the most specific class (the latest in an inheritance chain). You can implement your own ClassInfoStrategy, if you want to select setters yourself. And what setter do you want to select in your case?

daivanov avatar Jan 07 '17 14:01 daivanov

Hi, thanks for the quick reply. I don't have a class used in project right now, but basically, having two setters as below:

public void setField(Integer field) {
    this.field = field;
}

and

public void setField(String field) {
    this.field = Integer.parseInt(field);
}

I would like to use the first one. Thanks for the hint - I will read more about the ClassInfoStrategy.

janlukasik avatar Jan 07 '17 15:01 janlukasik

Well, one should never write the code like in second setter. This will fuck up not only Podam, but also inattentive developers. This is actually a good thing that Podam fails in such use case as it helps developers to fix this issue with their code.

daivanov avatar Jan 07 '17 15:01 daivanov

Probably it is a silly question but what is wrong with second setter? Not sure if it makes a diffierence, probably not - the purpose of the class above is to solely test a code.

janlukasik avatar Jan 07 '17 16:01 janlukasik

Here field is declared as a string, while in reality it's expecting an integer. And every time anyone will pass there string, which is not a number, they will get NumberFormatException, which is not even declared in the method signature. This requires other developers to read sources to be sure what they are doing makes sense. And what is even a gain here?

However, if we are speaking about, for example, String/URL ambiguity - this is a valid problem.

daivanov avatar Jan 07 '17 17:01 daivanov

I see. Thank you very much for the hint and clarifications - seems I have to review the project code to answer your question - but as far as I remember the setters looked as I presented above.

janlukasik avatar Jan 07 '17 17:01 janlukasik

Hi After the code review I came to the conclusion that the second setter was a mistake. As you mentioned - there is no gain to have it. Thank you.

janlukasik avatar Jan 09 '17 10:01 janlukasik

Not at all.

daivanov avatar Jan 09 '17 10:01 daivanov