Why is it safe to suppress this unchecked warning?

2019-06-17 04:31发布

Consider the UnaryFunction interface defined in Effective Java generics chapter .

public interface UnaryFunction<T> {
T apply(T arg);
}

and the following code for returning the UnaryFunction

// Generic singleton factory pattern
private static UnaryFunction<Object> IDENTITY_FUNCTION = new UnaryFunction<Object>() {
  public Object apply(Object arg) { return arg; }
};

// IDENTITY_FUNCTION is stateless and its type parameter is
// unbounded so it's safe to share one instance across all types.
@SuppressWarnings("unchecked")
public static <T> UnaryFunction<T> identityFunction() {
  return (UnaryFunction<T>) IDENTITY_FUNCTION;
}

Why is the cast of IDENTITY_FUNCTION to (UnaryFunction<T>) safe ?

The book says this about the question I am asking but I can't follow the logic here . Where are we invoking the apply function which does the identity operation ? i am confused because it is that function which returns the same object passed into it without modifying anything .

The cast of IDENTITY_FUNCTION to (UnaryFunction<T>) generates an unchecked cast warning, as UnaryFunction<Object> is not a UnaryFunction<T> for every T. But the identity function is special: it returns its argument unmodified, so we know that it is typesafe to use it as a UnaryFunction<T> whatever the value of T. Therefore, we can confidently suppress the unchecked cast warning that is generated by this cast. Once we’ve done this, the code compiles without error or warning.

3条回答
萌系小妹纸
2楼-- · 2019-06-17 04:51

With type erasure

T apply(T arg);

actually is

Object apply(Object arg);

Now for identity

Abc x = ...;
Abc y = IDENTITY.apply(x);

one may assume that it always is correct (is equivalent to y = x;).

(Very pragmatic.)

查看更多
劫难
3楼-- · 2019-06-17 04:53

identityFunction() simply returns the function itself, to be used on different objects.

Following is a usage example:

String result = identityFunction().apply("Hello");

The type safety warning is important. It's there because IDENTITY_FUNCTION is implemented such that the compiler can't guarantee that the function returns the same type as the input. Consider the following alternative implementation:

private static UnaryFunction<Object> CONST_FUNCTION = new UnaryFunction<Object>() {
    public Object apply(Object arg) { return "Default"; } 
};

This implementation always returns a string, so it's obviously not safe to return it as a unary function on a generic data type.

In our case (IDENTITY_FUNCTION), the proof that the returned type is the same as the input type is inside the implementation. We return the same instance, so it's guaranteed to have the same type. When you suppress type safety warnings, it's recommended to justify it with a proof.

查看更多
贪生不怕死
4楼-- · 2019-06-17 05:11

The cast is safe insomuch only as the identity function returns the exact object that was passed to it in the first place. As such, at runtime, there is no specialization of the generic parameter T that can violate the cast.

Aka, you are casting an object as it's own type.

查看更多
登录 后发表回答