I'm reading J. Bloch's effective Java and I got that unchecked casts is never good unless we made sure that the cast is safe. Now, since Java Collection frameworks doesn't provide us with Tree
data structure I have to write on my own.
public interface TreeVisitor<E, R> {
public R visit(E leaf);
public R visit(E val, Tree<E>... subtrees);
}
public abstract class Tree<E> {
public abstract <R> R accept(TreeVisitor<E, R> visitor);
public Tree<E> leaf(E leaf) {
return new Tree<E>() {
@Override
public <R> R accept(TreeVisitor<E, R> visitor) {
return visitor.visit(leaf);
}
};
}
public Tree<E> branch(E value, Tree<E>... subtrees){ //1
return new Tree<E>(){
@Override
public <R> R accept(TreeVisitor<E, R> visitor) {
return visitor.visit(value, subtrees);
}
};
}
}
At //1
, I got the warning:
Type safety: Potential heap pollution via varargs parameter subtrees
How can I check that my code actually safe?