Open Clement-Jean opened 3 years ago
Another question is how would you get the default value of the parameter in the Annotation class ?
All my default values of the annotation class are set to null
for some reason. In the quickstart guide, it says that KSP's analog of Java's Element.getAnnotation
is not yet implemented. Has this already been implemented or am I missing something?
@ting-yuan @neetopia Any thoughts on this?
If the annotation class is declared in source code, or from a binary library that is compiled from Java source code, then default value should work. The remaining scenario where it is from a kotlin library is not working as a known issue we are tracking.
If the annotation class is declared in source code, or from a binary library that is compiled from Java source code, then default value should work. The remaining scenario where it is from a kotlin library is not working as a known issue we are tracking.
Does that also apply to fetching arguments from the annotation?
For example:
annotation class BindType(
val to: KClass<*> = Nothing::class,
val installIn: Component = Component.NONE
) {
enum class Component {
NONE,
SINGLETON,
ACTIVITY_RETAINED,
SERVICE,
ACTIVITY,
VIEW_MODEL,
FRAGMENT,
VIEW,
VIEW_WITH_FRAGMENT,
CUSTOM
}
}
// then
@BindType(
to = Testable::class,
installIn = BindType.Component.VIEW
)
class Test : Testable
// then
fun getComponent(bindAnnotation: KSAnnotation): BindType.Component {
return bindAnnotation.arguments
.associate { it.name!!.asString() to it.value }
.getValue("installIn") // returns KSTypeImpl, instead of BindType.Component
}
In javac, it is as simple as this:
fun getComponent(element: Element): BindType.Component {
return element.getAnnotation(BindType::class.java).installIn
}
What is the equivalent of the above code in KSP? Thanks in advance.
In Java's analogy, what's provided in KSP is closer to getAnnotationMirrors
but not getAnnotation
, because the annotation type may not be instantiable at compile time. We plan to implement getAnnotation
in utils using reflection (e.g., java.lang.reflect.Proxy) but haven't have time to do so yet.
Ref: https://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/Element.html
Just checking did we manage to fix this. I want to get the enum value passed to one of the annotations. Similar to @mars885 it's returning KSTypeImpl
Is there any solution avail now?. I have same type of problem,
@Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE) annotation class GenerateDI( val moduleType: ModuleType, val superType: KClass<*> = Unit::class, )
@GenerateDI(ModuleType.ACTIVITY, BaseClass::class) class SampleClass : BaseClass
generateDIAnnotation?.arguments?.get(0)?.value.toString()
I used this line of code to get the param value as string. But is there any other way to type cast value?
Having the following enum:
and the following annotation:
I'm trying to retrieve the value of that priority property with the following code:
However, I get a
java.lang.ClassCastException
saying:Am I doing something wrong or is it a bug? No idea. If its the former please explain the problem to me (and potentially make the error more explicit).
Ps: works with Int type