JDK-6945418 : Project Coin: Simplified Varargs Method Invocation
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: unknown
  • Submitted: 2010-04-20
  • Updated: 2017-05-16
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b100Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
Quoting from project coin proposal at the following URL:

http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000316.html

"When a programmer tries to invoke a varargs (variable
arity) method with a non-reifiable varargs type, the compiler currently
generates an "unsafe operation" warning. This proposal moves the warning
from the call site to the method declaration."

BEFORE:

  static <T> List<T> asList(T... elements) { ... }

  static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
    // Warning: "uses unchecked or unsafe operations"
    return asList(a, b, c);
  }

AFTER (1):

  // Warning: "enables unsafe generic array creation"
  static <T> List<T> asList(T... elements) { ... }

  static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
    // Warning: "uses unchecked or unsafe operations"
    return asList(a, b, c);
  }

AFTER (2):

  @SuppressWarnings("generic-varargs")
  static <T> List<T> asList(T... elements) { ... } //no warning

  static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
    // no warning
    return asList(a, b, c);
  }

Comments
SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/46cf751559ae
11-06-2010

EVALUATION Sounds a sensible proposal. Vararg warnings have been historically very hard to cope with. However there are a few technical point of this proposal that deserve further discussions: the proposal in [1] seem to suggest the idea that it is possible to realize that a vararg method is unsafe simply by looking at its signature. More specifically, the proposed rule is something along the following lines: "When compiling a varargs method that could accept a non-reifiable varargs type, the compiler should generate a warning on the varargs method declaration. A varargs type is non-reifiable if it contains a type variable anywhere in its signature." The following example shows that this is not enough: void m(List<String>... o) { Object[] arr = o; o[0] = new ArrayList<Integer>(); List<String> ls = o[0]; //CCE!! } The spec should be made stricter, by mandating warnings for vararg method with a non-reifiable element type, where non-reifiable is defined the JLS way (see JLS 3rd 4.7). Note also that there are cases in which suppressing the call-site warning might be dangerous --- esp. if the called vararg method has been compiled in a separate compilation unit; in those cases, there will be no unchecked warning on the call-site (as per proposal), but there could still be a CCE at runtime (e.g. because the vararg method we compiled against was unsafe). For this reason we think it would be better to leave call-site warnings as they are now, but to give users a way to disable those warnings (e.g. via the @SuppressWarnings("varargs-unchecked") annotation). [1] - http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000316.html
20-04-2010