Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6945418

Project Coin: Simplified Varargs Method Invocation

XMLWordPrintable

    • b100
    • unknown
    • generic
    • Verified

      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);
        }

            mcimadamore Maurizio Cimadamore
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: