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

Fix parsing of static imports in annotation params #2593

Merged
merged 2 commits into from Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -682,9 +682,22 @@ class DefaultPsiToDocumentableTranslator(
* This is a workaround for static imports from JDK like RetentionPolicy
* For some reason they are not represented in the same way than using normal import
*/
private fun JvmAnnotationAttributeValue.toValue(): AnnotationParameterValue? = when (this) {
is JvmAnnotationEnumFieldValue -> (field as? PsiElement)?.let { EnumValue(fieldName ?: "", DRI.from(it)) }
else -> null
private fun JvmAnnotationAttributeValue.toValue(): AnnotationParameterValue? {
return when {
this is JvmAnnotationEnumFieldValue -> (field as? PsiElement)?.let { EnumValue(fieldName ?: "", DRI.from(it)) }
// static import of a constant is resolved to constant value instead of a field/link
this.isPsiAnnotationConstantValue() -> this.getPsiAnnotationConstantValue()?.toAnnotationLiteralValue()
else -> null
}
}

private fun Any.toAnnotationLiteralValue() = when (this) {
is Int -> IntValue(this)
is Long -> LongValue(this)
is Boolean -> BooleanValue(this)
is Float -> FloatValue(this)
is Double -> DoubleValue(this)
else -> StringValue(this.toString())
}

private fun PsiAnnotationMemberValue.toValue(): AnnotationParameterValue? = when (this) {
Expand Down
15 changes: 15 additions & 0 deletions plugins/base/src/main/kotlin/translators/psi/PsiPackageHelper.kt
@@ -0,0 +1,15 @@
/**
* Pretend to be from `com.intellij.psi` in order to access package-private declarations
*/
@file:Suppress("PackageDirectoryMismatch")
package com.intellij.psi

import com.intellij.lang.jvm.annotation.JvmAnnotationAttributeValue

/*
* For some reason `PsiAnnotationConstantValue` is package private,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a public interface JvmAnnotationConstantValue instead of PsiAnnotationConstantValue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, yeah, totally.

I was looking for something like this, but when you open up PsiAnnotationConstantValue and go to declaration of JvmAnnotationConstantValue from the signature - it goes to the kotlin-compiler artifact, where the interface is empty, so I thought there was no other way 🤦

Importing it seems to be fine though

2022-08-03_15-04-12

* even though it's returned in public API calls.
*/
internal fun JvmAnnotationAttributeValue.isPsiAnnotationConstantValue() = this is PsiAnnotationConstantValue
internal fun JvmAnnotationAttributeValue.getPsiAnnotationConstantValue(): Any? =
(this as PsiAnnotationConstantValue).constantValue
Expand Up @@ -198,6 +198,70 @@ class DefaultPsiToDocumentableTranslatorTest : BaseAbstractTest() {
}
}

@Test
fun `should resolve static imports used as annotation param values as literal values`() {
testInline(
"""
|/src/main/java/test/JavaClassUsingAnnotation.java
|package test;
|
|import static test.JavaConstants.STRING;
|import static test.JavaConstants.INTEGER;
|import static test.JavaConstants.LONG;
|import static test.JavaConstants.BOOLEAN;
|import static test.JavaConstants.DOUBLE;
|import static test.JavaConstants.FLOAT;
|
|@JavaAnnotation(
| stringValue = STRING, intValue = INTEGER, longValue = LONG,
| booleanValue = BOOLEAN, doubleValue = DOUBLE, floatValue = FLOAT
|)
|public class JavaClassUsingAnnotation {
|}
|
|/src/main/java/test/JavaAnnotation.java
|package test;
|@Documented
|public @interface JavaAnnotation {
| String stringValue();
| int intValue();
| long longValue();
| boolean booleanValue();
| double doubleValue();
| float floatValue();
|}
|
|/src/main/java/test/JavaConstants.java
|package test;
|public class JavaConstants {
| public static final String STRING = "STRING_CONSTANT_VALUE";
| public static final int INTEGER = 5;
| public static final long LONG = 6L;
| public static final boolean BOOLEAN = true;
| public static final double DOUBLE = 7.0d;
| public static final float FLOAT = 8.0f;
|}
""".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)
assertEquals(StringValue("STRING_CONSTANT_VALUE"), annotation.params["stringValue"])

assertEquals(IntValue(5), annotation.params["intValue"])
assertEquals(LongValue(6), annotation.params["longValue"])
assertEquals(BooleanValue(true), annotation.params["booleanValue"])
assertEquals(DoubleValue(7.0), annotation.params["doubleValue"])
assertEquals(FloatValue(8.0f), annotation.params["floatValue"])
}
}
}

class OnlyPsiPlugin : DokkaPlugin() {
private val dokkaBase by lazy { plugin<DokkaBase>() }

Expand Down