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

Annotation values that are default are not visible in KSP #53

Closed
yigit opened this issue Jun 30, 2020 · 11 comments
Closed

Annotation values that are default are not visible in KSP #53

yigit opened this issue Jun 30, 2020 · 11 comments
Assignees
Labels
bug Something isn't working P1 major features or blocking bugs
Milestone

Comments

@yigit
Copy link
Collaborator

yigit commented Jun 30, 2020

When reading a Java annotation with default value, its value is not visible in the arguments list.

public @interface JavaAnnotation {
    String debug();
    String withDefaultValue()  default "";
}

If you annotate an element with JavaAnnotation and don't specify a withDefaultValue property, KSP processor threats it as not existing such that arguments map in KSAnnotation will only include debug field.

@neetopia neetopia self-assigned this Jul 1, 2020
@neetopia
Copy link
Contributor

android/kotlin#60 contains a partial fix. With that commit you will be able to see the name of the missing argument, but not able to find out the default value of that argument. Still working to find a solution to find the default value.

@ting-yuan ting-yuan transferred this issue from android/kotlin Sep 25, 2020
@ting-yuan ting-yuan added bug Something isn't working P2 affects usability but not blocks users labels Oct 3, 2020
@ting-yuan ting-yuan added this to the 2020Q4 milestone Oct 4, 2020
@yigit
Copy link
Collaborator Author

yigit commented Nov 30, 2020

i think this bug might already be fixed?
https://android-review.googlesource.com/c/platform/frameworks/support/+/1513790 is passing.

@yigit
Copy link
Collaborator Author

yigit commented Nov 30, 2020

actually nevermind, looks like it is still broken for defaults values w/ other annotations.

@neetopia
Copy link
Contributor

Yes, it is broken for annotations declared in kotlin and then compiled.

copybara-service bot pushed a commit to androidx/androidx that referenced this issue Dec 3, 2020
KSP cannot always read default values in annotations. This CL introduces
a new XAnnotationBox implementation that uses reflection instead to
workaround it.
Unfortunately, KSP's behavior does not let us distinguish between null
vs default or empty array vs default. In both cases, we prefer going to
the default value in the annotation if value is required.

Also added support for enum lists in the java annotation box
implementation.

google/ksp#53

Bug: 160322705
Test: XAnnotationBoxTest, KspReflectiveAnnotationBoxTest
Change-Id: Ic38ef8cae000a00f96b99425b9e266cddcae48cb
@ting-yuan ting-yuan modified the milestones: 2020Q4, 2021Q1 Jan 9, 2021
@ting-yuan ting-yuan modified the milestones: 1.0.0-beta, 1.0.0 Mar 22, 2021
@ting-yuan ting-yuan added P1 major features or blocking bugs and removed P2 affects usability but not blocks users labels Jul 28, 2021
@neetopia
Copy link
Contributor

fixed in #528

@yigit
Copy link
Collaborator Author

yigit commented Aug 20, 2021

Unfortunateely this still looks broken in beta07.
To reproduce, you can enable this line in the test in AndroidX

@yigit yigit reopened this Aug 20, 2021
@neetopia
Copy link
Contributor

fixed in #591

@elihart
Copy link

elihart commented Sep 13, 2021

I think I ran into an edge case in 1.0 where, even with this fix, I get a null default value in the case where a class is defined within the annotation class.

Happens with both kotlin and java - here is a kotlin example.

annotation class Example(
    val classParam: KClass<*> = DEFAULT::class
) {
    object DEFAULT
}

Moving the object outside the annotation class fixes the problem

@neetopia
Copy link
Contributor

is the above class from a compiled library?

@elihart
Copy link

elihart commented Sep 14, 2021

Yes, the annotation class is defined in a compiled library.

@neetopia
Copy link
Contributor

thanks, I'll create a separate issue to track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P1 major features or blocking bugs
Projects
None yet
Development

No branches or pull requests

4 participants