Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java annotation array type value in java will break getAnnotationsByType #1329

Closed
larryxiao625 opened this issue Feb 21, 2023 · 0 comments · Fixed by #1330
Closed

Java annotation array type value in java will break getAnnotationsByType #1329

larryxiao625 opened this issue Feb 21, 2023 · 0 comments · Fixed by #1330

Comments

@larryxiao625
Copy link
Contributor

If java annotation value is array type, it will make getAnnotationsByType throw class cast issue, it seems that in java we can declare a single value for annotation value whose type is array, like this one

@Retention(CLASS)
public @interface JavaAnnotationWithList {
    Class[] testList();
}

@JavaAnnotationWithList(testList = Main.class)
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

If I use getAnnotationByType with Main class

        val ksAnnotated = resolver.getSymbolsWithAnnotation(JavaAnnotationWithList::class.qualifiedName.toString())
        ksAnnotated.forEach {
            if (it is KSClassDeclaration) {
                it.getAnnotationsByType(JavaAnnotationWithList::class).apply {
                    this.forEach { it.testList }
                }
            }
        }

It will throw java.lang.ClassCastException
I have just made a simple reproduce for it: https://github.com/larryxiao625/KspGetJavaAnnotationTypeDemo, you can reproduce it with ./gradlew clean ksp-test:assemble -i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant