1

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?

St.Antario
  • 26,175
  • 41
  • 130
  • 318

2 Answers2

2

How can I check that my code actually safe?

It's safe if the visitors only rely on the fact that the elements of subtrees are Tree<E>, and do not rely on the fact that subtrees is Tree<E>[]. If that is the case, then you should annotate the visit method with @SafeVarargs.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • 1
    the visit method is just iterating over the list of varargs. Is that safe> – St.Antario Jul 12 '15 at 11:48
  • My intuitive guess would be that just iterating is safe, but I would appreciate it if a knowledgeable person could confirm...? – pallgeuer Nov 29 '20 at 14:16
  • 1
    @pallgeuer: Yes, just iterating over the varargs array is safe, because it only relies on the type of the elements (type of the variable arguments), not the type of the array itself. – newacct Nov 29 '20 at 18:09
  • After reading your answer to this other question, it finally all made sense what the true problem is: https://stackoverflow.com/a/38910556/12235376 – pallgeuer Nov 30 '20 at 10:44
1

This Java Tutorial about Generics and Non-Reifiable Types covers your question

Heap pollution occurs when a variable of a parameterized type refers to an object that is not of that parameterized type


When the compiler encounters a varargs method, it translates the varargs formal parameter into an array. However, the Java programming language does not permit the creation of arrays of parameterized types. In the method ArrayBuilder.addToList, the compiler translates the varargs formal parameter T... elements to the formal parameter T[] elements, an array. However, because of type erasure, the compiler converts the varargs formal parameter to Object[] elements. Consequently, there is a possibility of heap pollution.

Clashsoft
  • 11,553
  • 5
  • 40
  • 79