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

Check varargs access against inferred signature

XMLWordPrintable

    • b61
    • Verified

        Accessibility of the varargs array element is supposed to be tested against "the last formal parameter type of the invocation type of the method" (JLS 15.12.3). That is, the inferred type, not necessarily the declared type. This is consistent with the generated bytecode, which allocates an array based on the inferred varargs type. (JLS 15.12.4.2 is ambiguous about the declared/inferred distinction for generated bytecode, but javac consistently uses the inferred type.)

        Since 8u40, per JDK-8036953, the varargs access check is performed on the declared element type only. This leads to runtime errors.

        Example:

        -----
        public class Test {
            static <T> void varargs(T... ts) { System.out.println(ts); }

            public static void main(String... args) {
                varargs(p2.Other.getPrivate());
            }

        }

        -----
        package p2;

        public class Other {
            public static Iterable<Private> getPrivate() { return null; }
            private class Private {}
        }

        -----
        javac 8u20: compiler error
        javac 8u40: compiles, IllegalAccessError at runtime

              dlsmith Dan Smith
              dlsmith Dan Smith
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: