My lots of library is in java. And some of them, I need to know type of the class. Is there any way out in java?
Is a class Java or Kotlin?
My lots of library is in java. And some of them, I need to know type of the class. Is there any way out in java?
Is a class Java or Kotlin?
Classes generated by the Kotlin compiler have a kotlin.Metadata annotation that you can read by reflection: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-metadata/
I guess you might employ multiple heuristics and approaches to get the correct source file name. One might be reading the .class file directly and getting the name of the source file from there:
ClassReader cr = new ClassReader(this.getClass().getName()); // Just so that we have some class to read here.
ClassNode cn = new ClassNode();
cr.accept(cn, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
LOGGER.info("class {} source file: {}", cn.name, cn.sourceFile);
The above uses ASM.
Another might be a fall back to something like the @kotlin.Metadata annotation.