hichem fazai
01/28/2022, 1:52 PMclass Bar {
init {
// Here I want to get the name of the file (Foo)
}
}
Joffrey
01/28/2022, 1:54 PM.class
file which is then packaged into a jar, what file name do you expect to get when you run this program?Bar::class.java.protectionDomain.codeSource.location.path
But it might not be 100% guaranteed to be accessiblehichem fazai
01/28/2022, 1:59 PMfun main() {
Bar()
}
class Bar {
init {
throw Exception()
}
}
output:
Exception in thread "main" java.lang.Exception
at Bar.<init>(Foo.kt:7)
at FooKt.main(Foo.kt:2)
at FooKt.main(Foo.kt)
class Bar {
init {
val name = Exception().stackTrace.first().fileName.removeSuffix(".kt")
}
}
Joffrey
01/28/2022, 2:13 PMhichem fazai
01/28/2022, 2:26 PMfun localize(form: Form) {
val name = form.fileName()
// if language is french, look for localization in the file name-fr_FR.xml
// if language is english, look for localization in the file name-en_GB.xml
}
Joffrey
01/28/2022, 2:33 PMForm
instances with XML files based on the name of the file containing the form? Why not base this on a convention on class name? Or just specifying the file prefix explicitly in each form (which is more refactoring-proof)?hichem fazai
01/28/2022, 2:49 PM