Can someone point me to a documentation where it i...
# compiler
t
Can someone point me to a documentation where it is defined how the class name is derived from the kotlin file name. So e.g. you have the file code.kt and than there will be internally a class generated that is called: CodeKt
Hmm i just realize that it really depends on many things. What i want to do is find all toplevel functions via reflection in one file. Maybe there is an other way without knowing the internal class name to do this.
d
The file -> class conversion happens here Here and here an actual implementation of the name conversion if
@JvmName
annotation is not defined
t
Ok i see. It really gets compilcated 😄 But thank you very much for this fast response. I will look into it. At the end i want to collect functions with a specific Annotation. I am only interested in toplevel functions. But i need to do this for files which are compiled during runtime of the application. So i need to load the class dynamically.
d
Well, it depends on inputs you have If you already have some
Class<*>
and want to understand if it is a "class for kotlin file", you can rely on the following invariant: • class is
final
it doesn't contain
@kotlin.Metadata
annotation
it contains only
static
functions/fields
functions are annotated with
@kotlin.Metadata
But if you want to scan the classpath to find such classes, it's more complicated They easiest way is to load all and check each one for the invariant above Also you potentially can use
kotlinx-metadata-jvm
library, but I'm not sure about exact API which will help you
u
> If you already have some
Class<*>
and want to understand if it is a "class for kotlin file", you can rely on the following invariant The correct way in this case would be to read
@kotlin.Metadata
annotation on the class, and check its
Metadata.kind
value (see the kdoc).
thank you color 1
t
But the problem for me is that the file is not loaded by the classloader. Because i compile it during runtime. But yea so i can use this function to get the classname. (Of course i need also search inside of the file for the @JvmName annotation and use this if it exists) right?
Copy code
PackagePartClassUtils.getPackagePartFqName(FqName("package name when exists"), kotlinFile.name)
For my usecase it is enough when i know the class name to be able to load it and check for member functions with a special annotation.
d
Yep, it should work
👍 1
t
Thank you very much. This helps a lot