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
import com.intellij.lang.jvm.JvmAnnotatedElement
import com.intellij.lang.jvm.JvmModifier
import com.intellij.psi.*
import com.intellij.psi.JavaTokenType.SUPER_KEYWORD
import com.intellij.psi.JavaTokenType.THIS_KEYWORD
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.tree.IElementType
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.elements.KtLightField
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.j2k.ReferenceSearcher
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.JKLiteralExpression.LiteralType.*
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.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -416,7 +421,7 @@ class JavaToJKTreeBuilder(
classKind,
typeParameterList?.toJK() ?: JKTypeParameterListImpl(),
createClassBody(),
annotationList(),
annotationList(this),
modifiers(),
visibility(),
modality()
@@ -538,7 +543,7 @@ class JavaToJKTreeBuilder(
JKTypeElementImpl(type.toJK(symbolProvider)),
JKNameIdentifierImpl(name),
with(expressionTreeMapper) { initializer.toJK() },
annotationList(),
annotationList(this),
modifiers(),
visibility(),
modality(),
@@ -549,9 +554,9 @@ class JavaToJKTreeBuilder(
}
}
fun <T> T.annotationList(): JKAnnotationList where T : JvmAnnotatedElement, T : PsiDocCommentOwner {
val plainAnnotations = annotations.map { it.toJK() }
val deprecatedAnnotation = docComment?.deprecatedAnnotation() ?: return JKAnnotationListImpl(plainAnnotations)
fun <T : JvmAnnotatedElement> T.annotationList(docCommentOwner: PsiDocCommentOwner?): JKAnnotationList {
val plainAnnotations = annotations.map { it.cast<PsiAnnotation>().toJK() }
val deprecatedAnnotation = docCommentOwner?.docComment?.deprecatedAnnotation() ?: return JKAnnotationListImpl(plainAnnotations)
return JKAnnotationListImpl(
plainAnnotations.mapNotNull { annotation ->
if (annotation.classSymbol.fqName == "java.lang.Deprecated") null else annotation
@@ -618,7 +623,7 @@ class JavaToJKTreeBuilder(
parameterList.parameters.map { it.toJK() },
body?.toJK() ?: JKBodyStub,
typeParameterList?.toJK() ?: JKTypeParameterListImpl(),
annotationList(),
annotationList(this),
throwsList.referencedTypes.map { JKTypeElementImpl(it.toJK(symbolProvider)) },
modifiers(),
visibility(),
@@ -653,7 +658,8 @@ class JavaToJKTreeBuilder(
JKTypeElementImpl(type.toJK(symbolProvider)),
JKNameIdentifierImpl(this.name ?: TODO()),
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 ->
symbolProvider.provideUniverseSymbol(this, i)
i.psi = this
@@ -16,17 +16,16 @@
package org.jetbrains.kotlin.nj2k
import org.jetbrains.kotlin.nj2k.NewCodeBuilder.ParenthesisKind.*
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.tree.*
import org.jetbrains.kotlin.nj2k.tree.impl.*
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.addToStdlib.firstIsInstanceOrNull
class NewCodeBuilder {
@@ -301,6 +300,8 @@ class NewCodeBuilder {
override fun visitParameter(parameter: JKParameter) {
renderModifiersList(parameter)
printer.printWithNoIndent(" ")
parameter.annotationList.accept(this)
printer.printWithNoIndent(" ")
if (parameter.isVarArgs) {
printer.printWithNoIndent("vararg ")
}
@@ -576,6 +577,9 @@ class NewCodeBuilder {
}
override fun visitLocalVariable(localVariable: JKLocalVariable) {
printer.printWithNoIndent(" ")
localVariable.annotationList.accept(this)
printer.printWithNoIndent(" ")
renderModifiersList(localVariable)
printer.printWithNoIndent(" ")
localVariable.name.accept(this)
@@ -5,8 +5,8 @@
package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.kotlin.nj2k.ConversionContext
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.impl.JKTypeElementImpl
@@ -23,28 +23,28 @@ class JetbrainsNullableAnnotationsConverter(private val context: ConversionConte
val notNullAnnotation =
element.annotationList.annotations.firstOrNull { it.classSymbol == notNullAnnotationSymbol }
when (element) {
is JKField -> {
is JKVariable -> {
if (nullableAnnotation != null) {
element.annotationList.annotations -= nullableAnnotation
element.type =
JKTypeElementImpl(element.type.type.updateNullability(Nullability.Nullable))
JKTypeElementImpl(element.type.type.updateNullability(Nullability.Nullable))
}
if (notNullAnnotation != null) {
element.annotationList.annotations -= notNullAnnotation
element.type =
JKTypeElementImpl(element.type.type.updateNullability(Nullability.NotNull))
JKTypeElementImpl(element.type.type.updateNullability(Nullability.NotNull))
}
}
is JKMethod -> {
if (nullableAnnotation != null) {
element.annotationList.annotations -= nullableAnnotation
element.returnType =
JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.Nullable))
JKTypeElementImpl(element.returnType.type.updateNullability(Nullability.Nullable))
}
if (notNullAnnotation != null) {
element.annotationList.annotations -= notNullAnnotation
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(
type: JKTypeElement,
name: JKNameIdentifier,
initializer: JKExpression
initializer: JKExpression,
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKForLoopVariable, JKBranchElementBase() {
override var initializer by child(initializer)
override var name by child(name)
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)
}
@@ -84,7 +86,8 @@ class JKParameterImpl(
type: JKTypeElement,
name: JKNameIdentifier,
override var isVarArgs: Boolean = false,
initializer: JKExpression = JKStubExpressionImpl()
initializer: JKExpression = JKStubExpressionImpl(),
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKParameter, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitParameter(this, data)
@@ -248,11 +251,13 @@ class JKLocalVariableImpl(
type: JKTypeElement,
name: JKNameIdentifier,
initializer: JKExpression,
override var mutability: Mutability
override var mutability: Mutability,
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKLocalVariable, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
override var initializer by child(initializer)
override var name by child(name)
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)
}
@@ -453,13 +458,15 @@ class JKEnumConstantImpl(
name: JKNameIdentifier,
arguments: JKArgumentList,
body: JKClassBody,
type: JKTypeElement
type: JKTypeElement,
annotationList: JKAnnotationList = JKAnnotationListImpl()
) : JKEnumConstant, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
override var name: JKNameIdentifier by child(name)
override val arguments: JKArgumentList by child(arguments)
override val body: JKClassBody by child(body)
override var type: JKTypeElement by child(type)
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)
}
@@ -108,7 +108,7 @@ interface JKMethod : JKDeclaration, JKVisibilityOwner, JKModalityOwner, JKExtraM
var block: JKBlock
}
interface JKVariable : JKDeclaration {
interface JKVariable : JKDeclaration, JKAnnotationListOwner {
var type: JKTypeElement
var name: JKNameIdentifier
var initializer: JKExpression