I have this abstract class
public abstract class SomeClass<S> {// S should be equal to the type of the class which extends this class
private final Validator validator;
...
protected void someMethod() {
Set<ConstraintViolation<S>> violations =
validator.validate((S) this); //here I get the warning
}
I am getting a warning about an unchecked cast. I know why there is a warning, but I couldn't figure how to do the required check.
I thought of if(this instanceof S)
but this is always true as it will be executed at runtime in the subclass's code and from that context this
will refer to the subclass
How can I solve this to get rid of the warning (without suppressing the warning) and enforce the subclass to put its type in the generic upon declaration (I don't want the scenario which happens in this answer)