From 01269f4aa38d7362d519fb1bd9c4a337cadab1b6 Mon Sep 17 00:00:00 2001 From: IgnatBeresnev Date: Thu, 4 Aug 2022 20:19:35 +0200 Subject: [PATCH 1/2] Display values of java constants Fixes #2524 --- .../psi/DefaultPsiToDocumentableTranslator.kt | 17 +++++++ .../DefaultPsiToDocumentableTranslatorTest.kt | 45 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index 03e0b9ef50..6699f222b2 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -658,12 +658,29 @@ class DefaultPsiToDocumentableTranslator( inheritedFrom?.let { inheritedFrom -> InheritedMember(inheritedFrom.toSourceSetDependent()) }, it.toSourceSetDependent().toAdditionalModifiers(), annotations.toSourceSetDependent().toAnnotations(), + psi.getConstantExpression()?.let { DefaultValue(it.toSourceSetDependent()) }, takeIf { isVar }?.let { IsVar } ) } ) } + private fun PsiField.getConstantExpression(): Expression? { + val constantValue = this.computeConstantValue() ?: return null + return when (constantValue) { + is Byte -> IntegerConstant(constantValue.toLong()) + is Short -> IntegerConstant(constantValue.toLong()) + is Int -> IntegerConstant(constantValue.toLong()) + is Long -> IntegerConstant(constantValue) + is Char -> StringConstant(constantValue.toString()) + is String -> StringConstant(constantValue) + is Double -> DoubleConstant(constantValue) + is Float -> FloatConstant(constantValue) + is Boolean -> BooleanConstant(constantValue) + else -> ComplexExpression(constantValue.toString()) + } + } + private fun PsiField.getVisibility(getter: DFunction?): Visibility { return getter?.visibility?.get(sourceSetData) ?: this.getVisibility() } diff --git a/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt b/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt index 3f34f02021..97d855488b 100644 --- a/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt +++ b/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt @@ -198,6 +198,51 @@ class DefaultPsiToDocumentableTranslatorTest : BaseAbstractTest() { } } + @Test + fun `should add default value to constant properties`() { + testInline( + """ + |/src/main/java/test/JavaConstants.java + |package test; + | + |public class JavaConstants { + | public static final byte BYTE = 1; + | public static final short SHORT = 2; + | public static final int INT = 3; + | public static final long LONG = 4L; + | public static final float FLOAT = 5.0f; + | public static final double DOUBLE = 6.0d; + | public static final String STRING = "Seven"; + | public static final char CHAR = 'E'; + | public static final boolean BOOLEAN = true; + |} + """.trimIndent(), + configuration + ) { + documentablesMergingStage = { module -> + val testedClass = module.packages.single().classlikes.single { it.name == "JavaConstants" } + + val constants = testedClass.properties + assertEquals(9, constants.size) + + val constantsByName = constants.associateBy { it.name } + fun getConstantExpression(name: String): Expression? { + return constantsByName.getValue(name).extra[DefaultValue]?.expression?.values?.first() + } + + assertEquals(IntegerConstant(1), getConstantExpression("BYTE")) + assertEquals(IntegerConstant(2), getConstantExpression("SHORT")) + assertEquals(IntegerConstant(3), getConstantExpression("INT")) + assertEquals(IntegerConstant(4), getConstantExpression("LONG")) + assertEquals(FloatConstant(5.0f), getConstantExpression("FLOAT")) + assertEquals(DoubleConstant(6.0), getConstantExpression("DOUBLE")) + assertEquals(StringConstant("Seven"), getConstantExpression("STRING")) + assertEquals(StringConstant("E"), getConstantExpression("CHAR")) + assertEquals(BooleanConstant(true), getConstantExpression("BOOLEAN")) + } + } + } + @Test fun `should resolve static imports used as annotation param values as literal values`() { testInline( From 85245cb4631bc98818bf60dd29c6e2040002bc65 Mon Sep 17 00:00:00 2001 From: IgnatBeresnev Date: Thu, 4 Aug 2022 20:23:18 +0200 Subject: [PATCH 2/2] Move function declaration lower to preserve call order --- .../psi/DefaultPsiToDocumentableTranslator.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index 6699f222b2..a58e1b830f 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -665,6 +665,13 @@ class DefaultPsiToDocumentableTranslator( ) } + private fun PsiField.getVisibility(getter: DFunction?): Visibility { + return getter?.visibility?.get(sourceSetData) ?: this.getVisibility() + } + + private fun Collection.toListOfAnnotations() = + filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() } + private fun PsiField.getConstantExpression(): Expression? { val constantValue = this.computeConstantValue() ?: return null return when (constantValue) { @@ -681,13 +688,6 @@ class DefaultPsiToDocumentableTranslator( } } - private fun PsiField.getVisibility(getter: DFunction?): Visibility { - return getter?.visibility?.get(sourceSetData) ?: this.getVisibility() - } - - private fun Collection.toListOfAnnotations() = - filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() } - private fun JvmAnnotationAttribute.toValue(): AnnotationParameterValue = when (this) { is PsiNameValuePair -> value?.toValue() ?: attributeValue?.toValue() ?: StringValue("") else -> StringValue(this.attributeName)