why java compiler doesn't report unchecked cast warnings for multi-cast expression in Intellij?

Viewed 1190

Why the code below didn't report unchecked warnings by Intellij IDEA with jdk 1.8.0_121 since Supplier<R> & Serializable is the supertype of T?

<T extends Supplier<Integer> & Serializable> T createdBy(AtomicInteger counter) {
    //      v--- if I removed the first cast expression, I can't compile it
    return (T) (Supplier<Integer> & Serializable) counter::incrementAndGet;
    //           ^--- it should be reports unchecked warnings, but it doesn't

}

And the following code has reported unchecked cast warnings:

<T, R extends T> R apply(T value) {
    return (R) value;
    //      ^--- unchecked cast 
}

Why this question occurs, the interested thing occurs during I write the code at below for chaining a type with multi-supertypes:

AtomicInteger counter  = new AtomicInteger(0);
Supplier<Integer>  serialized = serialized(createdBy(counter));

assert serialized.get() == 1; // ok
assert counter.get() == 0 ; // ok

<T extends Serializable> T serialized(T value) {
    return deserialize(serialize(value));
}

I have searched through the JLS, but I can't find out the exactly favorable evidence. Could someone tell me why?

1 Answers
Related