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

Display values of java constants #2609

Merged
merged 2 commits into from Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -658,6 +658,7 @@ class DefaultPsiToDocumentableTranslator(
inheritedFrom?.let { inheritedFrom -> InheritedMember(inheritedFrom.toSourceSetDependent()) },
it.toSourceSetDependent().toAdditionalModifiers(),
annotations.toSourceSetDependent().toAnnotations(),
psi.getConstantExpression()?.let { DefaultValue(it.toSourceSetDependent()) },
takeIf { isVar }?.let { IsVar }
)
}
Expand All @@ -671,6 +672,22 @@ class DefaultPsiToDocumentableTranslator(
private fun Collection<PsiAnnotation>.toListOfAnnotations() =
filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() }

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 JvmAnnotationAttribute.toValue(): AnnotationParameterValue = when (this) {
is PsiNameValuePair -> value?.toValue() ?: attributeValue?.toValue() ?: StringValue("")
else -> StringValue(this.attributeName)
Expand Down
Expand Up @@ -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(
Expand Down