Why does the compiler request an explicit cast here?
public class Predicate<T> {
private String operand1;
private String operator;
private T operand2;
private Class<T> classType;
public Predicate(String operand1, T operand2){
this(operand1, "=", operand2);
}
@SuppressWarnings("unchecked")
public Predicate(String operand1, String operator, T operand2){
this.operand1 = operand1;
this.operator = operator;
this.operand2 = operand2;
this.classType = (Class<T>) operand2.getClass();
}
}
If the class uses a generic T, and requests a generic T typed argument to be set as T typed field, then why does the compiler doubt about the correctness of the type?
If the cast is made, then why does the compiler still throw a warning about the cast being potentially unsafe?
Additionally, should I be worried about the warning?
In case you wonder why I need it, I use this class type to define stmt.set in preparedStatement (jdbc) later on.