Integrated: 8178701: Compile error with switch statement on protected enum defined in parent inner class

Jan Lahoda jlahoda at openjdk.java.net
Mon May 16 11:56:55 UTC 2022


On Fri, 25 Feb 2022 12:32:53 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider the following code in two packages:
> 
> package foo;
> 
> import bar.D.E;
> 
> public class A {
> 
>     public static class B {
> 
>         protected enum C {
>             X, Y, Z
>         }
>     }
> 
>     public static void main(String... args) {
>         new E().run(B.C.X);
>     }
> }
> 
> 
> 
> package bar;
> 
> import foo.A.B;
> 
> public class D {
> 
>     public static class E extends B {
> 
>         public void run(C arg) {
>             switch (arg) {
>                 default:
>                     System.out.println("OK");
>             }
>         }
>     }
> }
> 
> 
> Accessing `foo.A.B.C` in `bar.D.E` is OK, as it is accessible through the `E`'s superclass. But, for the `switch`, javac creates a map (array) from runtime ordinals of constants of `E` to compile-time positions. But, this mapping is in a different class that does not extend `B`, so internal method lookups fail for it, as the methods are not accessible per Java rules.
> 
> But, at runtime, the enum class is effectively public, so I think it is possible to ignore the protected access rules and accept the method invocations while generatting the mapping code, which is what this patch is trying to do.

This pull request has now been integrated.

Changeset: 77dfbb45
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.java.net/jdk/commit/77dfbb457083fd30da344d0cbea5b0510aa3a0fc
Stats:     182 lines in 4 files changed: 137 ins; 0 del; 45 mod

8178701: Compile error with switch statement on protected enum defined in parent inner class

Reviewed-by: vromero

-------------

PR: https://git.openjdk.java.net/jdk/pull/7621


More information about the compiler-dev mailing list