Simone Parca
Simone Parca

Reputation: 13

How to avoid warning in cast to Generic in this case?

Best way to avoid this warning? Is there any case/example where the (T) cast can fail? (Java language compiled with Eclipse)

Example updated for reference with Lee's answer: cast fail subtly using result.

interface Test {
}

class Test1 implements Test {
    void doSomething() {}
}
class Test2 implements Test {}

public class TestIterable<T extends Test> implements Iterable<T> {

    final ArrayList<T> result=new ArrayList<>();

    public TestIterable(Object obj) {
        if (obj instanceof Test) {
            Test t = (Test) obj;
            result.add((T) obj); // Warning: unchecked cast from Test to T
            // Iteresting decompilation result....
            // result.add((Test) obj);
        }
    }

    @Override
    public Iterator<T> iterator() {
        return result.iterator();
    }

    public static void main(String[] args) {
        TestIterable<Test1> x = new TestIterable<>(new Test2());
        for (Test1 y :x.result) {
            y.doSomething();
            // java.lang.ClassCastException
        };
    }
}

Upvotes: 0

Views: 65

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109567

Two alternatives. The natural solution to require a T object probably does not fit your needs. Then the type T has to be explicitly passed because of the type erasure making any other construct senseless.

class TestIterable<T extends Test> implements Iterable<T> {

    final List<T> result = new ArrayList<>();

    public TestIterable(T obj) {
        result.add(obj);
    }

    public TestIterable(Class<T> type, Object obj) {
        if (obj == null || type.isAssignableFrom(obj.getClass())) {
            result.add(type.cast(obj));
        }
    }

    @Override
    public Iterator<T> iterator() {
        return result.iterator();
    }
}

Upvotes: 3

Related Questions