I've got the following method:
protected <S> void setValue(final S oldValue, final S newValue) {
// Do something
}
I want to make sure, that both parameters are of the same type. It would be cool, if there'd be a compiler error when you try to pass parameters of two different types.
The above way is clearly not the correct one. I can put into a String and an Integer, since the both extend from Object.
Is my want even possible? Or is the only way to make sure both parameters are of the same type to check it inside the method and throw an IllegalArgumentException?
You can do that if you consider that S is the correct type :
protected <S, T extends S> void setValue(final S oldValue, final T newValue) {
// Do something
}
You can and can't input these :
// Works
setValue("something", "something");
setValue(new Object(), new String());
// Doesn't work
setValue(new String(), new Object());
or
You can do :
protected <S> void setValue(final S oldValue, final S newValue, final Class<S> clazz) {
// Do something
}
and use it like that
setValue("something", "something", String.class);
or
protected <S> void setValue(final S oldValue, final S newValue) {
if(!oldValue.getClass().equals(newValue.getClass())) {
//throw something
}
}
This isn't really possible, I'm afraid, except for explicit checks. It'll always get coerced up to Object; there's no way to stop inputs from getting coerced up to supertypes.
You can use explicit reflection-based checking in your argument validation, but that's just about your only option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With