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

Missing annotation values for array of static imports #2694

Open
juliamcclellan opened this issue Oct 7, 2022 · 0 comments
Open

Missing annotation values for array of static imports #2694

juliamcclellan opened this issue Oct 7, 2022 · 0 comments
Labels
bug feedback: Google An issue/PR submitted by colleagues at Google, most likely related to the Android API reference docs language: Java Issue/PR related to the Java language feature/analysis/docs

Comments

@juliamcclellan
Copy link
Contributor

Describe the bug
Followup to #2580, the fix for that bug works when a static import is directly used as an annotation param. However, when the param is instead an array of static imports, it ends up as an empty array in the docs.

Expected behaviour
I would expect an array of constant values, like what #2593 did for a single static import annotation param value. Alternatively, the name of the value linked to its definition would also work.

To Reproduce
Here's a (failing) test for the expected behavior:

@Test
    fun `should resolve array of static imports used as annotation param value as literal values`() {
        testInline(
            """
        |/src/main/java/test/JavaClassUsingAnnotation.java
        |package test;
        |
        |import static test.JavaConstants.STRING1;
        |import static test.JavaConstants.STRING2;
        |
        |@JavaAnnotation(stringValues = {STRING1, STRING2})
        |public class JavaClassUsingAnnotation {
        |}
        |
        |/src/main/java/test/JavaAnnotation.java
        |package test;
        |@Documented
        |public @interface JavaAnnotation {
        |    String[] stringValues();
        |}
        |
        |/src/main/java/test/JavaConstants.java
        |package test;
        |public class JavaConstants {
        |    public static final String STRING1 = "STRING_CONSTANT_VALUE_1";
        |    public static final String STRING2 = "STRING_CONSTANT_VALUE_2";
        |}
    """.trimIndent(),
            configuration
        ) {
            documentablesMergingStage = { module ->
                val testedClass = module.packages.single().classlikes.single { it.name == "JavaClassUsingAnnotation" }

                val annotation = (testedClass as DClass).extra[Annotations]?.directAnnotations?.values?.single()?.single()
                checkNotNull(annotation)

                assertEquals("JavaAnnotation", annotation.dri.classNames)

                val expectedArray = ArrayValue(listOf(StringValue("STRING_CONSTANT_VALUE_1"), StringValue("STRING_CONSTANT_VALUE_2")))
                assertEquals(expectedArray, annotation.params["stringValues"])
            }
        }
    }

(Fails with expected: <ArrayValue(value=[STRING_CONSTANT_VALUE_1, STRING_CONSTANT_VALUE_2])> but was: <ArrayValue(value=[])>)

When converting the annotations in DefaultPsiToDocumentableTranslator, each element of the array falls into the PsiReferenceExpression case of PsiAnnotationMemberValue.toValue(), but the psiReference ends up null, so the array ends up empty:
Screen Shot 2022-10-07 at 3 43 41 PM

Are you willing to provide a PR?
I haven't found an obvious way to use a similar solution to #2593 to get the constant values, hoping I'm missing something. All I have is a hacky workaround to link to the definitions by comparing the referenceName of the PsiReferenceExpression to each static import line from the file.

@IgnatBeresnev IgnatBeresnev added the feedback: Google An issue/PR submitted by colleagues at Google, most likely related to the Android API reference docs label Oct 12, 2022
@IgnatBeresnev IgnatBeresnev added the language: Java Issue/PR related to the Java language feature/analysis/docs label Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug feedback: Google An issue/PR submitted by colleagues at Google, most likely related to the Android API reference docs language: Java Issue/PR related to the Java language feature/analysis/docs
Projects
None yet
Development

No branches or pull requests

2 participants