For Java 15/16 under preview and targeted for 17+, sealed classes and interfaces can be created. It would be good for Groovy to support a similar capability.
In addition, Groovy should behave properly in the presence of such Java-created classes, e.g. currently the compiler allows Groovy classes to extend sealed Java classes or implement sealed interfaces, but such classes when loaded will give a runtime error:
- Java allows the permitted list to be inferred where the classes are from the same source file.
- Java allows sealed/non-sealed parts to the hierarchy.
- We could use an annotation to carry sealed information for JDK8-16 versions.
Advanced options should also be explored:
- When creating sealed classes, Java currently requires all mentioned permitted children to be compiled in the source set. However, it does allow subsequent compilation steps to include just one of the children (and have the parent pre-compiled) though this can lead to IncompatibleClassChangeError problems. If these problems can be overcome, it might be possible to have mixed Java/Groovy hierarchies by using stubs initially. This should be explored further to understand if it is indeed possible.
We should create a GEP which outlines what we want to do with respect to Groovy support for sealed hierarchies and discuss further there how we might implement the feature.