How to resolve the class object of method parameters and return type? #845
-
Hi, I'm using Classgraph to scan some Java classes for certain pattern and generate Kotlin code in response to those patterns using KotlinPoet. The problem: KotlinPoet requires concrete types, e.g. String.class for creating parameter and method definitions. Is it possible to resolve those without actually loading the concrete classes? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can resolve the class names using ClassGraph, but it intentionally does not load the classes unless you ask it to by calling It is impossible to have a class reference without first loading the class. If your API requires class references, then you have to load the class. ClassGraph does at least disable class initialization when it loads classes, if I remember right. Then classes are only initialized the first time a method is called or a field is accessed within the class. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much, this was the info I was looking for! Great library BTW! |
Beta Was this translation helpful? Give feedback.
You can resolve the class names using ClassGraph, but it intentionally does not load the classes unless you ask it to by calling
ClassInfo.loadClass
. (That's the whole point of ClassGraph existing. Classloading is time consuming, and can have side effects, when classes are initialized.)It is impossible to have a class reference without first loading the class. If your API requires class references, then you have to load the class.
ClassGraph does at least disable class initialization when it loads classes, if I remember right. Then classes are only initialized the first time a method is called or a field is accessed within the class.