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

15.9.2: Fix the assertion regarding instantiation of an anonymous class

XMLWordPrintable

    • b04

        This regards the PREVIEW JEP 492.

        [The fix to the spec in JDK-8346100 was itself broken.]

        The issue regards an anonymous class instance creation expression that creates an instance that is a *subclass of a local class that is in a static context*. For example:

        ```
        class O {
            static void foo(int x) { // static context, so no enclosing instance
                class X { /* capture x */ } // inner local class in a static context
                class U {
                    static void test() { new X(){...}; } // just as bad as saying `new X()`
                }
            }
        }
        ```

        The anonymous class instance creation expression should be forbidden (as it would be if it was just `new X()`).

        The condition in the draft spec should read:

        - If *C* is an anonymous class, then:

            - If the class instance creation expression occurs in a static context,
                then *i* has no immediately enclosing instance. **If the direct
                superclass of *C* is an inner local class *L* that occurs in a static
                context, then let *S* be the nearest `static` method declaration,
                `static` field declaration, or static initializer that encloses the
                declaration of *L*. If the nearest `static` method declaration, `static`
                field declaration, or static initializer that encloses the class
                instance creation expression is not *S*, then a compile-time error
                occurs.**

              gbierman Gavin Bierman
              gbierman Gavin Bierman
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: