Render decompiled annotations on enum entries

This commit is contained in:
Alexander Udalov
2015-12-10 23:30:36 +03:00
parent 6f347f351a
commit 65c5c99c68
13 changed files with 133 additions and 29 deletions
@@ -52,10 +52,8 @@ public class AnnotationLoaderForStubBuilderImpl(
}
}
override fun loadEnumEntryAnnotations(container: ProtoContainer, proto: ProtoBuf.EnumEntry): List<ClassId> {
// TODO
return listOf()
}
override fun loadEnumEntryAnnotations(container: ProtoContainer, proto: ProtoBuf.EnumEntry): List<ClassId> =
proto.getExtension(protocol.enumEntryAnnotation).orEmpty().map { container.nameResolver.getClassId(it.id) }
override fun loadValueParameterAnnotations(
container: ProtoContainer,
@@ -64,9 +62,7 @@ public class AnnotationLoaderForStubBuilderImpl(
parameterIndex: Int,
proto: ProtoBuf.ValueParameter
): List<ClassId> =
proto.getExtension(protocol.parameterAnnotation).orEmpty().map {
container.nameResolver.getClassId(it.id)
}
proto.getExtension(protocol.parameterAnnotation).orEmpty().map { container.nameResolver.getClassId(it.id) }
override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer,
@@ -172,9 +172,22 @@ private class ClassClsStubBuilder(
}
private fun createEnumEntryStubs(classBody: KotlinPlaceHolderStubImpl<KtClassBody>) {
classProto.getEnumEntryNameList().forEach { id ->
val name = c.nameResolver.getName(id)
KotlinClassStubImpl(
if (classKind != ProtoBuf.Class.Kind.ENUM_CLASS) return
val container = ProtoContainer(classProto, null, c.nameResolver, c.typeTable)
val enumEntries: List<Pair<Int, List<ClassId>>> =
if (classProto.enumEntryList.isNotEmpty())
classProto.enumEntryList.map { enumEntryProto ->
enumEntryProto.name to c.components.annotationLoader.loadEnumEntryAnnotations(container, enumEntryProto)
}
else classProto.enumEntryNameList.map { enumEntryName ->
enumEntryName to listOf<ClassId>()
}
enumEntries.forEach { entry ->
val name = c.nameResolver.getName(entry.first)
val annotations = entry.second
val enumEntryStub = KotlinClassStubImpl(
KtStubElementTypes.ENUM_ENTRY,
classBody,
qualifiedName = c.containerFqName.child(name).ref(),
@@ -185,6 +198,9 @@ private class ClassClsStubBuilder(
isLocal = false,
isTopLevel = false
)
if (annotations.isNotEmpty()) {
createAnnotationStubs(annotations, createEmptyModifierListStub(enumEntryStub))
}
}
}
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.idea.decompiler.stubBuilder
import com.google.protobuf.MessageLite
import com.intellij.psi.PsiElement
import com.intellij.psi.stubs.StubElement
import com.intellij.util.SmartList
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
@@ -175,21 +174,13 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
container, callableProto, callableProto.annotatedCallableKind, index, valueParameterProto
)
if (parameterAnnotations.isNotEmpty()) {
createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierList(parameterStub))
createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierListStub(parameterStub))
}
createTypeReferenceStub(parameterStub, typeProto)
}
}
private fun createEmptyModifierList(parameterStub: KotlinParameterStubImpl): KotlinModifierListStubImpl {
return KotlinModifierListStubImpl(
parameterStub,
ModifierMaskUtils.computeMask { false },
KtStubElementTypes.MODIFIER_LIST
)
}
fun createTypeParameterListStub(
parent: StubElement<out PsiElement>,
typeParameterProtoList: List<ProtoBuf.TypeParameter>
@@ -262,6 +262,14 @@ fun createModifierListStub(
)
}
fun createEmptyModifierListStub(parent: KotlinStubBaseImpl<*>): KotlinModifierListStubImpl {
return KotlinModifierListStubImpl(
parent,
ModifierMaskUtils.computeMask { false },
KtStubElementTypes.MODIFIER_LIST
)
}
fun createAnnotationStubs(annotationIds: List<ClassId>, parent: KotlinStubBaseImpl<*>) {
return createTargetedAnnotationStubs(annotationIds.map { ClassIdWithTarget(it, null) }, parent)
}
@@ -77,13 +77,19 @@ public fun buildDecompiledText(
if (descriptor is MissingDependencyErrorClass) {
throw IllegalStateException("${descriptor.javaClass.getSimpleName()} cannot be rendered. FqName: ${descriptor.fullFqName}")
}
val startOffset = builder.length()
val header = if (isEnumEntry(descriptor))
descriptor.name.asString() + if (lastEnumEntry!!) ";" else ","
else
descriptorRenderer.render(descriptor).replace("= ...", DECOMPILED_COMMENT_FOR_PARAMETER)
builder.append(header)
var endOffset = builder.length()
val startOffset = builder.length
if (isEnumEntry(descriptor)) {
for (annotation in descriptor.annotations) {
builder.append(descriptorRenderer.renderAnnotation(annotation))
builder.append(" ")
}
builder.append(descriptor.name.asString())
builder.append(if (lastEnumEntry!!) ";" else ",")
}
else {
builder.append(descriptorRenderer.render(descriptor).replace("= ...", DECOMPILED_COMMENT_FOR_PARAMETER))
}
var endOffset = builder.length
if (descriptor is CallableDescriptor) {
//NOTE: assuming that only return types can be flexible