[code-reflection] RFR: Enhance JavaType::type to deal with inner classes

Paul Sandoz psandoz at openjdk.org
Tue Jun 17 17:31:50 UTC 2025


On Tue, 17 Jun 2025 14:54:05 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> In some cases, the `JavaType::type(Type)` factory loses information about enclosing/enclosed type.
> This is due to the fact that, when the type in question contains some generic parameterization, the enclosing/enclosed relationship is reified in the `ParameterizedType` class.
> However, if the type is non-generic, it is modelled as a simple `Class` -- and the enclosing/enclosed relationship is lost.
> 
> This PR enhances the factory to recover the enclosing type using the reflective information available:
> * the class is a non-static member class. The enclosing type is the class in which the class is declared
> * the class is a local class, defined in a method `m`. If `m` is non-static, the enclosing type is the class declaring `m`
> * the class is a local class, defined in a constructor of class `C`. The enclosing type is `C`
> * otherwise, there's no enclosing type.
> 
> Note that there are some [known pathological cases](https://bugs.openjdk.org/browse/JDK-8162500) where the enclosing class information cannot be reconstructed from the information we have available in the bytecode, but this should not be a big issue for the time being.

Marked as reviewed by psandoz (Lead).

I believe that should bring more consistency when round tripping too, as the compiler will produce the expected structure. IIUC it should work correctly for deeply nested classes, since we peel off the simple name and recursively process the outer part.

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

PR Review: https://git.openjdk.org/babylon/pull/443#pullrequestreview-2936516690
PR Comment: https://git.openjdk.org/babylon/pull/443#issuecomment-2981205923


More information about the babylon-dev mailing list