New J2K: Add throws list for java method & add JKClassLiteralExpression
This commit is contained in:
committed by
Ilya Kirillov
parent
69633e7a0f
commit
3ea4ee9110
@@ -64,6 +64,7 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
||||
)
|
||||
is PsiArrayInitializerExpression -> toJK()
|
||||
is PsiLambdaExpression -> toJK()
|
||||
is PsiClassObjectAccessExpressionImpl -> toJK()
|
||||
else -> {
|
||||
throw RuntimeException("Not supported: ${this::class}")
|
||||
}
|
||||
@@ -72,6 +73,15 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
||||
}
|
||||
}
|
||||
|
||||
fun PsiClassObjectAccessExpressionImpl.toJK(): JKClassLiteralExpression {
|
||||
val type = operand.toJK()
|
||||
return JKClassLiteralExpressionImpl(
|
||||
type,
|
||||
if (type.type is JKJavaPrimitiveType) JKClassLiteralExpression.LiteralType.JAVA_PRIMITIVE_CLASS
|
||||
else JKClassLiteralExpression.LiteralType.JAVA_CLASS
|
||||
)
|
||||
}
|
||||
|
||||
fun PsiInstanceOfExpression.toJK(): JKJavaInstanceOfExpression {
|
||||
return JKJavaInstanceOfExpressionImpl(operand.toJK(), checkType?.toJK() ?: TODO())
|
||||
}
|
||||
|
||||
@@ -743,7 +743,22 @@ class NewCodeBuilder {
|
||||
|
||||
override fun visitAnnotation(annotation: JKAnnotation) {
|
||||
printer.printWithNoIndent("@")
|
||||
annotation.name.accept(this)
|
||||
printer.printWithNoIndent(annotation.classSymbol.name)
|
||||
if (annotation.arguments.expressions.isNotEmpty()) {
|
||||
printer.par {
|
||||
annotation.arguments.accept(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitClassLiteralExpression(classLiteralExpression: JKClassLiteralExpression) {
|
||||
renderType(classLiteralExpression.classType.type.updateNullability(Nullability.NotNull))
|
||||
printer.printWithNoIndent("::")
|
||||
when (classLiteralExpression.literalType) {
|
||||
JKClassLiteralExpression.LiteralType.KOTLIN_CLASS -> printer.printWithNoIndent("class")
|
||||
JKClassLiteralExpression.LiteralType.JAVA_CLASS -> printer.printWithNoIndent("class.java")
|
||||
JKClassLiteralExpression.LiteralType.JAVA_PRIMITIVE_CLASS -> printer.printWithNoIndent("class.javaPrimitiveType")
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitKtWhenCase(ktWhenCase: JKKtWhenCase) {
|
||||
@@ -778,10 +793,21 @@ class NewCodeBuilder {
|
||||
javaNewExpression.acceptChildren(this)
|
||||
}
|
||||
|
||||
override fun visitTypeElement(typeElement: JKTypeElement) {
|
||||
val type = typeElement.type
|
||||
// override fun visitTypeElement(typeElement: JKTypeElement, data: Nothing?) {
|
||||
// val classType = typeElement.type as? JKClassType ?: return
|
||||
// collectedFqNames.add(FqName(classType.classReference.fqName!!))
|
||||
// }
|
||||
|
||||
override fun visitClassLiteralExpression(classLiteralExpression: JKClassLiteralExpression) {
|
||||
val type = classLiteralExpression.classType.type
|
||||
if (type is JKClassType) {
|
||||
collectedFqNames.add(FqName(type.classReference.fqName!!))
|
||||
val fqName = type.classReference.fqName!!
|
||||
val isDefaultImport = fqName.split('.').let {
|
||||
it.size == 2 && it.first() == "kotlin"
|
||||
}
|
||||
if (!isDefaultImport) {
|
||||
collectedFqNames.add(FqName(fqName))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,3 +188,13 @@ fun kotlinAssert(assertion: JKExpression, message: JKExpression?, symbolProvider
|
||||
),
|
||||
JKExpressionListImpl(listOfNotNull(assertion, message))
|
||||
)
|
||||
|
||||
fun throwAnnotation(throws: List<JKType>, symbolProvider: JKSymbolProvider) =
|
||||
JKAnnotationImpl(
|
||||
symbolProvider.provideByFqName("kotlin.jvm.Throws"),
|
||||
JKExpressionListImpl(
|
||||
throws.map {
|
||||
JKClassLiteralExpressionImpl(JKTypeElementImpl(it), JKClassLiteralExpression.LiteralType.KOTLIN_CLASS)
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
@@ -62,6 +62,7 @@ class JKJavaMethodImpl(
|
||||
override var block: JKBlock by child(block)
|
||||
override var typeParameterList: JKTypeParameterList by child(typeParameterList)
|
||||
override var annotationList: JKAnnotationList by child(annotationList)
|
||||
override var throwsList: List<JKTypeElement> by children(throwsList)
|
||||
}
|
||||
|
||||
class JKJavaLiteralExpressionImpl(
|
||||
|
||||
@@ -484,8 +484,11 @@ class JKAnnotationListImpl(annotations: List<JKAnnotation> = emptyList()) : JKAn
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitAnnotationList(this, data)
|
||||
}
|
||||
|
||||
class JKAnnotationImpl(name: JKNameIdentifier) : JKAnnotation, JKBranchElementBase() {
|
||||
override var name: JKNameIdentifier by child(name)
|
||||
class JKAnnotationImpl(
|
||||
override val classSymbol: JKClassSymbol,
|
||||
arguments: JKExpressionList
|
||||
) : JKAnnotation, JKBranchElementBase() {
|
||||
override var arguments: JKExpressionList by child(arguments)
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitAnnotation(this, data)
|
||||
}
|
||||
|
||||
@@ -494,4 +497,13 @@ class JKTypeArgumentListImpl(typeArguments: List<JKTypeElement> = emptyList()) :
|
||||
override val typeArguments: List<JKTypeElement> by children(typeArguments)
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitTypeArgumentList(this, data)
|
||||
|
||||
}
|
||||
|
||||
class JKClassLiteralExpressionImpl(
|
||||
classType: JKTypeElement,
|
||||
override var literalType: JKClassLiteralExpression.LiteralType
|
||||
) : JKClassLiteralExpression, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
|
||||
override val classType: JKTypeElement by child(classType)
|
||||
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitClassLiteralExpression(this, data)
|
||||
}
|
||||
@@ -24,6 +24,7 @@ interface JKField : JKVariable, JKVisibilityOwner, JKMutabilityOwner, JKModality
|
||||
interface JKJavaField : JKField, JKBranchElement
|
||||
|
||||
interface JKJavaMethod : JKMethod, JKBranchElement {
|
||||
var throwsList: List<JKTypeElement>
|
||||
}
|
||||
|
||||
interface JKJavaMethodCallExpression : JKMethodCallExpression
|
||||
|
||||
@@ -70,7 +70,8 @@ interface JKAnnotationList : JKTreeElement {
|
||||
}
|
||||
|
||||
interface JKAnnotation : JKTreeElement {
|
||||
var name: JKNameIdentifier//TODO
|
||||
val classSymbol: JKClassSymbol
|
||||
var arguments: JKExpressionList
|
||||
}
|
||||
|
||||
interface JKAnnotationListOwner : JKTreeElement {
|
||||
@@ -383,3 +384,14 @@ interface JKForInStatement : JKStatement {
|
||||
interface JKPackageDeclaration : JKDeclaration {
|
||||
var packageName: JKNameIdentifier
|
||||
}
|
||||
|
||||
interface JKClassLiteralExpression : JKExpression {
|
||||
val classType: JKTypeElement
|
||||
val literalType: LiteralType
|
||||
|
||||
enum class LiteralType {
|
||||
KOTLIN_CLASS,
|
||||
JAVA_CLASS,
|
||||
JAVA_PRIMITIVE_CLASS
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user