Recently we upgraded one of our enum class to sealed class with objects as sub-classes so we can make another tier of abstraction to simplify code. However we c
A wise choice is using ServiceLoader in kotlin. and then write some providers to get a common class, enum, object or data class instance. for example:
val provides = ServiceLoader.load(YourSealedClassProvider.class).iterator();
val subInstances = providers.flatMap{it.get()};
fun YourSealedClassProvider.get():List{/*todo*/};
the hierarchy as below:
Provider SealedClass
^ ^
| |
-------------- --------------
| | | |
EnumProvider ObjectProvider ObjectClass EnumClass
| |-------------------^ ^
| |
|-------------------------------------------|
Another option, is more complicated, but it can meet your needs since sealed classes in the same package. let me tell you how to archive in this way:
ClassLoader.getResource("com/xxx/app/YourSealedClass.class")
jar://**/com/xxx/app
or file://**/com/xxx/app
, and then find out all the "com/xxx/app/*.class"
files/entries.ClassLoader.loadClass(eachClassName)
Enum.values()
, object.INSTANCE
.