New J2K: Add annotation list for JKVariable

This commit is contained in:
Ilya Kirillov
2019-02-13 12:49:36 +03:00
committed by Ilya Kirillov
parent 40f5ebb77e
commit b355572aab
5 changed files with 43 additions and 26 deletions
@@ -17,11 +17,15 @@
package org.jetbrains.kotlin.nj2k package org.jetbrains.kotlin.nj2k
import com.intellij.lang.jvm.JvmAnnotatedElement import com.intellij.lang.jvm.JvmAnnotatedElement
import com.intellij.lang.jvm.JvmModifier
import com.intellij.psi.* import com.intellij.psi.*
import com.intellij.psi.JavaTokenType.SUPER_KEYWORD import com.intellij.psi.JavaTokenType.SUPER_KEYWORD
import com.intellij.psi.JavaTokenType.THIS_KEYWORD import com.intellij.psi.JavaTokenType.THIS_KEYWORD
import com.intellij.psi.impl.source.tree.ChildRole import com.intellij.psi.impl.source.tree.ChildRole
import com.intellij.psi.impl.source.tree.java.* import com.intellij.psi.impl.source.tree.java.PsiClassObjectAccessExpressionImpl
import com.intellij.psi.impl.source.tree.java.PsiLabeledStatementImpl
import com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl
import com.intellij.psi.impl.source.tree.java.PsiNewExpressionImpl
import com.intellij.psi.javadoc.PsiDocComment import com.intellij.psi.javadoc.PsiDocComment
import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.InheritanceUtil import com.intellij.psi.util.InheritanceUtil
@@ -29,18 +33,19 @@ import com.intellij.psi.util.PsiUtil
import org.jetbrains.kotlin.asJava.classes.KtLightClassForFacade import org.jetbrains.kotlin.asJava.classes.KtLightClassForFacade
import org.jetbrains.kotlin.asJava.elements.KtLightField import org.jetbrains.kotlin.asJava.elements.KtLightField
import org.jetbrains.kotlin.asJava.elements.KtLightMethod import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.idea.caches.lightClasses.KtLightClassForDecompiledDeclaration
import org.jetbrains.kotlin.idea.j2k.content import org.jetbrains.kotlin.idea.j2k.content
import org.jetbrains.kotlin.j2k.ReferenceSearcher import org.jetbrains.kotlin.j2k.ReferenceSearcher
import org.jetbrains.kotlin.j2k.ast.Nullability import org.jetbrains.kotlin.j2k.ast.Nullability
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.nj2k.tree.* import org.jetbrains.kotlin.nj2k.tree.*
import org.jetbrains.kotlin.nj2k.tree.JKLiteralExpression.LiteralType.* import org.jetbrains.kotlin.nj2k.tree.JKLiteralExpression.LiteralType.*
import org.jetbrains.kotlin.nj2k.tree.impl.* import org.jetbrains.kotlin.nj2k.tree.impl.*
import org.jetbrains.kotlin.lexer.KtToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -416,7 +421,7 @@ class JavaToJKTreeBuilder(
classKind, classKind,
typeParameterList?.toJK() ?: JKTypeParameterListImpl(), typeParameterList?.toJK() ?: JKTypeParameterListImpl(),
createClassBody(), createClassBody(),
annotationList(), annotationList(this),
modifiers(), modifiers(),
visibility(), visibility(),
modality() modality()
@@ -538,7 +543,7 @@ class JavaToJKTreeBuilder(
JKTypeElementImpl(type.toJK(symbolProvider)), JKTypeElementImpl(type.toJK(symbolProvider)),
JKNameIdentifierImpl(name), JKNameIdentifierImpl(name),
with(expressionTreeMapper) { initializer.toJK() }, with(expressionTreeMapper) { initializer.toJK() },
annotationList(), annotationList(this),
modifiers(), modifiers(),
visibility(), visibility(),
modality(), modality(),
@@ -549,9 +554,9 @@ class JavaToJKTreeBuilder(
} }
} }
fun <T> T.annotationList(): JKAnnotationList where T : JvmAnnotatedElement, T : PsiDocCommentOwner { fun <T : JvmAnnotatedElement> T.annotationList(docCommentOwner: PsiDocCommentOwner?): JKAnnotationList {
val plainAnnotations = annotations.map { it.toJK() } val plainAnnotations = annotations.map { it.cast<PsiAnnotation>().toJK() }
val deprecatedAnnotation = docComment?.deprecatedAnnotation() ?: return JKAnnotationListImpl(plainAnnotations) val deprecatedAnnotation = docCommentOwner?.docComment?.deprecatedAnnotation() ?: return JKAnnotationListImpl(plainAnnotations)
return JKAnnotationListImpl( return JKAnnotationListImpl(
plainAnnotations.mapNotNull { annotation -> plainAnnotations.mapNotNull { annotation ->
if (annotation.classSymbol.fqName == "java.lang.Deprecated") null else annotation if (annotation.classSymbol.fqName == "java.lang.Deprecated") null else annotation
@@ -618,7 +623,7 @@ class JavaToJKTreeBuilder(
parameterList.parameters.map { it.toJK() }, parameterList.parameters.map { it.toJK() },
body?.toJK() ?: JKBodyStub, body?.toJK() ?: JKBodyStub,
typeParameterList?.toJK() ?: JKTypeParameterListImpl(), typeParameterList?.toJK() ?: JKTypeParameterListImpl(),
annotationList(), annotationList(this),
throwsList.referencedTypes.map { JKTypeElementImpl(it.toJK(symbolProvider)) }, throwsList.referencedTypes.map { JKTypeElementImpl(it.toJK(symbolProvider)) },
modifiers(), modifiers(),
visibility(), visibility(),
@@ -653,7 +658,8 @@ class JavaToJKTreeBuilder(
JKTypeElementImpl(type.toJK(symbolProvider)), JKTypeElementImpl(type.toJK(symbolProvider)),
JKNameIdentifierImpl(this.name ?: TODO()), JKNameIdentifierImpl(this.name ?: TODO()),
with(expressionTreeMapper) { initializer.toJK() }, with(expressionTreeMapper) { initializer.toJK() },
if (hasModifierProperty(PsiModifier.FINAL)) Mutability.IMMUTABLE else Mutability.UNKNOWN if (hasModifierProperty(PsiModifier.FINAL)) Mutability.IMMUTABLE else Mutability.UNKNOWN,
annotationList(null)
).also { i -> ).also { i ->
symbolProvider.provideUniverseSymbol(this, i) symbolProvider.provideUniverseSymbol(this, i)
i.psi = this i.psi = this
@@ -16,17 +16,16 @@
package org.jetbrains.kotlin.nj2k package org.jetbrains.kotlin.nj2k
import org.jetbrains.kotlin.nj2k.NewCodeBuilder.ParenthesisKind.*
import org.jetbrains.kotlin.j2k.ast.Nullability import org.jetbrains.kotlin.j2k.ast.Nullability
import org.jetbrains.kotlin.lexer.KtKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.nj2k.NewCodeBuilder.ParenthesisKind.*
import org.jetbrains.kotlin.nj2k.conversions.parentOfType import org.jetbrains.kotlin.nj2k.conversions.parentOfType
import org.jetbrains.kotlin.nj2k.tree.* import org.jetbrains.kotlin.nj2k.tree.*
import org.jetbrains.kotlin.nj2k.tree.impl.* import org.jetbrains.kotlin.nj2k.tree.impl.*
import org.jetbrains.kotlin.nj2k.tree.visitors.JKVisitorVoid import org.jetbrains.kotlin.nj2k.tree.visitors.JKVisitorVoid
import org.jetbrains.kotlin.lexer.KtKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
class NewCodeBuilder { class NewCodeBuilder {
@@ -301,6 +300,8 @@ class NewCodeBuilder {
override fun visitParameter(parameter: JKParameter) { override fun visitParameter(parameter: JKParameter) {
renderModifiersList(parameter) renderModifiersList(parameter)
printer.printWithNoIndent(" ") printer.printWithNoIndent(" ")
parameter.annotationList.accept(this)
printer.printWithNoIndent(" ")
if (parameter.isVarArgs) { if (parameter.isVarArgs) {
printer.printWithNoIndent("vararg ") printer.printWithNoIndent("vararg ")
} }
@@ -576,6 +577,9 @@ class NewCodeBuilder {
} }
override fun visitLocalVariable(localVariable: JKLocalVariable) { override fun visitLocalVariable(localVariable: JKLocalVariable) {
printer.printWithNoIndent(" ")
localVariable.annotationList.accept(this)
printer.printWithNoIndent(" ")
renderModifiersList(localVariable) renderModifiersList(localVariable)
printer.printWithNoIndent(" ") printer.printWithNoIndent(" ")
localVariable.name.accept(this) localVariable.name.accept(this)
@@ -5,8 +5,8 @@
package org.jetbrains.kotlin.nj2k.conversions package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.kotlin.nj2k.ConversionContext
import org.jetbrains.kotlin.j2k.ast.Nullability import org.jetbrains.kotlin.j2k.ast.Nullability
import org.jetbrains.kotlin.nj2k.ConversionContext
import org.jetbrains.kotlin.nj2k.tree.* import org.jetbrains.kotlin.nj2k.tree.*
import org.jetbrains.kotlin.nj2k.tree.impl.JKTypeElementImpl import org.jetbrains.kotlin.nj2k.tree.impl.JKTypeElementImpl
@@ -23,28 +23,28 @@ class JetbrainsNullableAnnotationsConverter(private val context: ConversionConte
val notNullAnnotation = val notNullAnnotation =
element.annotationList.annotations.firstOrNull { it.classSymbol == notNullAnnotationSymbol } element.annotationList.annotations.firstOrNull { it.classSymbol == notNullAnnotationSymbol }
when (element) { when (element) {
is JKField -> { is JKVariable -> {
if (nullableAnnotation != null) { if (nullableAnnotation != null) {
element.annotationList.annotations -= nullableAnnotation element.annotationList.annotations -= nullableAnnotation
element.type = element.type =
JKTypeElementImpl(element.type.type.updateNullability(Nullability.Nullable)) JKTypeElementImpl(element.type.type.updateNullability(Nullability.Nullable))
} }
if (notNullAnnotation != null) { if (notNullAnnotation != null) {
element.annotationList.annotations -= notNullAnnotation element.annotationList.annotations -= notNullAnnotation
element.type = element.type =
JKTypeElementImpl(element.type.type.updateNullability(Nullability.NotNull)) JKTypeElementImpl(element.type.type.updateNullability(Nullability.NotNull))
} }
} }
is JKMethod -> { is JKMethod -> {
if (nullableAnnotation != null) { if (nullableAnnotation != null) {
element.annotationList.annotations -= nullableAnnotation element.annotationList.annotations -= nullableAnnotation
element.returnType = element.returnType =
JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.Nullable)) JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.Nullable))
} }
if (notNullAnnotation != null) { if (notNullAnnotation != null) {
element.annotationList.annotations -= notNullAnnotation element.annotationList.annotations -= notNullAnnotation
element.returnType = element.returnType =
JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.NotNull)) JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.NotNull))
} }
} }
} }
@@ -70,11 +70,13 @@ class JKNameIdentifierImpl(override val value: String) : JKNameIdentifier, JKEle
class JKForLoopVariableImpl( class JKForLoopVariableImpl(
type: JKTypeElement, type: JKTypeElement,
name: JKNameIdentifier, name: JKNameIdentifier,
initializer: JKExpression initializer: JKExpression,
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKForLoopVariable, JKBranchElementBase() { ) : JKForLoopVariable, JKBranchElementBase() {
override var initializer by child(initializer) override var initializer by child(initializer)
override var name by child(name) override var name by child(name)
override var type by child(type) override var type by child(type)
override var annotationList by child(annotationList)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitForLoopVariable(this, data) override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitForLoopVariable(this, data)
} }
@@ -84,7 +86,8 @@ class JKParameterImpl(
type: JKTypeElement, type: JKTypeElement,
name: JKNameIdentifier, name: JKNameIdentifier,
override var isVarArgs: Boolean = false, override var isVarArgs: Boolean = false,
initializer: JKExpression = JKStubExpressionImpl() initializer: JKExpression = JKStubExpressionImpl(),
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKParameter, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() { ) : JKParameter, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitParameter(this, data) override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitParameter(this, data)
@@ -248,11 +251,13 @@ class JKLocalVariableImpl(
type: JKTypeElement, type: JKTypeElement,
name: JKNameIdentifier, name: JKNameIdentifier,
initializer: JKExpression, initializer: JKExpression,
override var mutability: Mutability override var mutability: Mutability,
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKLocalVariable, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() { ) : JKLocalVariable, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
override var initializer by child(initializer) override var initializer by child(initializer)
override var name by child(name) override var name by child(name)
override var type by child(type) override var type by child(type)
override var annotationList by child(annotationList)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitLocalVariable(this, data) override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitLocalVariable(this, data)
} }
@@ -453,13 +458,15 @@ class JKEnumConstantImpl(
name: JKNameIdentifier, name: JKNameIdentifier,
arguments: JKArgumentList, arguments: JKArgumentList,
body: JKClassBody, body: JKClassBody,
type: JKTypeElement type: JKTypeElement,
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKEnumConstant, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() { ) : JKEnumConstant, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
override var name: JKNameIdentifier by child(name) override var name: JKNameIdentifier by child(name)
override val arguments: JKArgumentList by child(arguments) override val arguments: JKArgumentList by child(arguments)
override val body: JKClassBody by child(body) override val body: JKClassBody by child(body)
override var type: JKTypeElement by child(type) override var type: JKTypeElement by child(type)
override var initializer: JKExpression by child(JKStubExpressionImpl()) override var initializer: JKExpression by child(JKStubExpressionImpl())
override var annotationList by child(annotationList)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitEnumConstant(this, data) override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitEnumConstant(this, data)
} }
@@ -108,7 +108,7 @@ interface JKMethod : JKDeclaration, JKVisibilityOwner, JKModalityOwner, JKExtraM
var block: JKBlock var block: JKBlock
} }
interface JKVariable : JKDeclaration { interface JKVariable : JKDeclaration, JKAnnotationListOwner {
var type: JKTypeElement var type: JKTypeElement
var name: JKNameIdentifier var name: JKNameIdentifier
var initializer: JKExpression var initializer: JKExpression