Is it possible to check if a class is explicitly marked as non-sealed like with MyClass.class.isSealed() and without checking its parent hierarchy? (And if so, how?)
Reason - I want to know if the info is somewhere in the class itself and not guess by checking if its final and looking up the parents. Maybe the language changes one day and this indirect condition may too.
There is, unfortunately, no dedicated method to find out whether a given class is explicitly defined as
non-sealedand negating the result ofisSealedmethod does not give you an accurate answer.If you know the rules of defining a class as
non-sealed, you can construct the method that would do it for you.class/interfacemust not befinal.class/interfacemust not besealedclass, its direct superclass must besealed.interface, one of the direct superclasses must besealed.You don't need to check the whole parent hierarchy but due to the definition of
non-sealed, you have to inspect its direct supertype (or supertypes in case ofinterface).In a small test, only
ReptileandCatare and should be foundnon-sealed:Classes:
Output, as you see, only
ReptileandDogarenon-sealed:Interfaces:
Output: