From b355572aabda949dbe4b6db5f4a4398df0f0dfdf Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Wed, 13 Feb 2019 12:49:36 +0300 Subject: [PATCH] New J2K: Add annotation list for JKVariable --- .../kotlin/nj2k/JavaToJKTreeBuilder.kt | 26 ++++++++++++------- .../jetbrains/kotlin/nj2k/NewCodeBuilder.kt | 14 ++++++---- .../JetbrainsNullableAnnotationsConverter.kt | 12 ++++----- .../org/jetbrains/kotlin/nj2k/tree/impl/jk.kt | 15 ++++++++--- nj2k/src/org/jetbrains/kotlin/nj2k/tree/jk.kt | 2 +- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/nj2k/src/org/jetbrains/kotlin/nj2k/JavaToJKTreeBuilder.kt b/nj2k/src/org/jetbrains/kotlin/nj2k/JavaToJKTreeBuilder.kt index 1ab4aeaa4af..6c727b89ba0 100644 --- a/nj2k/src/org/jetbrains/kotlin/nj2k/JavaToJKTreeBuilder.kt +++ b/nj2k/src/org/jetbrains/kotlin/nj2k/JavaToJKTreeBuilder.kt @@ -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.annotationList(): JKAnnotationList where T : JvmAnnotatedElement, T : PsiDocCommentOwner { - val plainAnnotations = annotations.map { it.toJK() } - val deprecatedAnnotation = docComment?.deprecatedAnnotation() ?: return JKAnnotationListImpl(plainAnnotations) + fun T.annotationList(docCommentOwner: PsiDocCommentOwner?): JKAnnotationList { + val plainAnnotations = annotations.map { it.cast().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 diff --git a/nj2k/src/org/jetbrains/kotlin/nj2k/NewCodeBuilder.kt b/nj2k/src/org/jetbrains/kotlin/nj2k/NewCodeBuilder.kt index 3cc4c41d6e7..5fee13007c8 100644 --- a/nj2k/src/org/jetbrains/kotlin/nj2k/NewCodeBuilder.kt +++ b/nj2k/src/org/jetbrains/kotlin/nj2k/NewCodeBuilder.kt @@ -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) diff --git a/nj2k/src/org/jetbrains/kotlin/nj2k/conversions/JetbrainsNullableAnnotationsConverter.kt b/nj2k/src/org/jetbrains/kotlin/nj2k/conversions/JetbrainsNullableAnnotationsConverter.kt index 32707747f35..f83f3d1364c 100644 --- a/nj2k/src/org/jetbrains/kotlin/nj2k/conversions/JetbrainsNullableAnnotationsConverter.kt +++ b/nj2k/src/org/jetbrains/kotlin/nj2k/conversions/JetbrainsNullableAnnotationsConverter.kt @@ -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)) } } } diff --git a/nj2k/src/org/jetbrains/kotlin/nj2k/tree/impl/jk.kt b/nj2k/src/org/jetbrains/kotlin/nj2k/tree/impl/jk.kt index d15fc0f62ca..95330d46ea8 100644 --- a/nj2k/src/org/jetbrains/kotlin/nj2k/tree/impl/jk.kt +++ b/nj2k/src/org/jetbrains/kotlin/nj2k/tree/impl/jk.kt @@ -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 accept(visitor: JKVisitor, 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 accept(visitor: JKVisitor, 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 accept(visitor: JKVisitor, 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 accept(visitor: JKVisitor, data: D): R = visitor.visitEnumConstant(this, data) } diff --git a/nj2k/src/org/jetbrains/kotlin/nj2k/tree/jk.kt b/nj2k/src/org/jetbrains/kotlin/nj2k/tree/jk.kt index 9f0c4df4b16..5800077aaea 100644 --- a/nj2k/src/org/jetbrains/kotlin/nj2k/tree/jk.kt +++ b/nj2k/src/org/jetbrains/kotlin/nj2k/tree/jk.kt @@ -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