Converter from Java: added proper annotations formatting
This commit is contained in:
@@ -72,7 +72,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
is PsiComment -> Comment(element.getText()!!)
|
is PsiComment -> Comment(element.getText()!!)
|
||||||
is PsiImportList -> convertImportList(element)
|
is PsiImportList -> convertImportList(element)
|
||||||
is PsiImportStatementBase -> convertImport(element, false)
|
is PsiImportStatementBase -> convertImport(element, false)
|
||||||
is PsiAnnotation -> convertAnnotation(element)
|
is PsiAnnotation -> convertAnnotation(element, false)
|
||||||
is PsiPackageStatement -> PackageStatement(quoteKeywords(element.getPackageName() ?: ""))
|
is PsiPackageStatement -> PackageStatement(quoteKeywords(element.getPackageName() ?: ""))
|
||||||
is PsiWhiteSpace -> WhiteSpace(element.getText()!!)
|
is PsiWhiteSpace -> WhiteSpace(element.getText()!!)
|
||||||
else -> null
|
else -> null
|
||||||
@@ -290,7 +290,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
Parameter(field.identifier, field.`type`, varValModifier, field.annotations, field.modifiers.filter { ACCESS_MODIFIERS.contains(it) })
|
Parameter(field.identifier, field.`type`, varValModifier, field.annotations, field.modifiers.filter { ACCESS_MODIFIERS.contains(it) })
|
||||||
}
|
}
|
||||||
|
|
||||||
val primaryConstructor = PrimaryConstructor(this, MemberComments.Empty, listOf(), setOf(Modifier.PRIVATE), ParameterList(parameters), Block.Empty)
|
val primaryConstructor = PrimaryConstructor(this, MemberComments.Empty, Annotations.Empty, setOf(Modifier.PRIVATE), ParameterList(parameters), Block.Empty)
|
||||||
val updatedMembers = MemberList(classBody.normalMembers.elements.filter { !finalOrWithEmptyInitializerFields.contains(it) })
|
val updatedMembers = MemberList(classBody.normalMembers.elements.filter { !finalOrWithEmptyInitializerFields.contains(it) })
|
||||||
return ClassBody(primaryConstructor, classBody.secondaryConstructors, updatedMembers, classBody.classObjectMembers)
|
return ClassBody(primaryConstructor, classBody.secondaryConstructors, updatedMembers, classBody.classObjectMembers)
|
||||||
}
|
}
|
||||||
@@ -418,7 +418,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun convertPrimaryConstructor(constructor: PsiMethod,
|
private fun convertPrimaryConstructor(constructor: PsiMethod,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
membersToRemove: MutableSet<PsiMember>): PrimaryConstructor {
|
membersToRemove: MutableSet<PsiMember>): PrimaryConstructor {
|
||||||
@@ -610,14 +610,30 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
PsiModifier.PRIVATE to Modifier.PRIVATE
|
PsiModifier.PRIVATE to Modifier.PRIVATE
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun convertAnnotations(owner: PsiModifierListOwner): List<Annotation> {
|
public fun convertAnnotations(owner: PsiModifierListOwner): Annotations {
|
||||||
return owner.getModifierList()?.getAnnotations()
|
val modifierList = owner.getModifierList()
|
||||||
?.filter { it.getQualifiedName() !in ANNOTATIONS_TO_REMOVE }
|
val annotations = modifierList?.getAnnotations()?.filter { it.getQualifiedName() !in ANNOTATIONS_TO_REMOVE }
|
||||||
?.map { convertAnnotation(it) }
|
if (annotations == null || annotations.isEmpty()) return Annotations.Empty
|
||||||
?.filterNotNull() ?: listOf()
|
|
||||||
|
val newLines = run {
|
||||||
|
if (!modifierList!!.isInSingleLine()) {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var child: PsiElement? = modifierList
|
||||||
|
while(true) {
|
||||||
|
child = child!!.getNextSibling()
|
||||||
|
if (child == null || child!!.getTextLength() != 0) break
|
||||||
|
}
|
||||||
|
if (child is PsiWhiteSpace) !child!!.isInSingleLine() else false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val list = annotations.map { convertAnnotation(it, owner is PsiLocalVariable) }.filterNotNull()
|
||||||
|
return Annotations(list, newLines)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnnotation(annotation: PsiAnnotation): Annotation? {
|
public fun convertAnnotation(annotation: PsiAnnotation, brackets: Boolean): Annotation? {
|
||||||
val name = Identifier((annotation.getNameReferenceElement() ?: return null).getText()!!)
|
val name = Identifier((annotation.getNameReferenceElement() ?: return null).getText()!!)
|
||||||
val annotationClass = annotation.getNameReferenceElement()?.resolve() as? PsiClass
|
val annotationClass = annotation.getNameReferenceElement()?.resolve() as? PsiClass
|
||||||
val lastMethod = annotationClass?.getMethods()?.lastOrNull()
|
val lastMethod = annotationClass?.getMethods()?.lastOrNull()
|
||||||
@@ -653,7 +669,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
|
|
||||||
attrValues.map { attrName to it }
|
attrValues.map { attrName to it }
|
||||||
}
|
}
|
||||||
return Annotation(name, arguments)
|
return Annotation(name, arguments, brackets)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val TYPE_MAP: Map<String, String> = mapOf(
|
private val TYPE_MAP: Map<String, String> = mapOf(
|
||||||
|
|||||||
@@ -18,17 +18,32 @@ package org.jetbrains.jet.j2k.ast
|
|||||||
|
|
||||||
import java.util.HashSet
|
import java.util.HashSet
|
||||||
|
|
||||||
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, Expression>>) : Element {
|
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, Expression>>, val brackets: Boolean) : Element {
|
||||||
override fun toKotlin(): String {
|
private fun surroundWithBrackets(text: String) = if (brackets) "[$text]" else text
|
||||||
if (arguments.isEmpty()) return name.toKotlin()
|
|
||||||
|
|
||||||
return name.toKotlin() + "(" + arguments.map {
|
override fun toKotlin(): String {
|
||||||
|
if (arguments.isEmpty()) {
|
||||||
|
return surroundWithBrackets(name.toKotlin())
|
||||||
|
}
|
||||||
|
|
||||||
|
val argsText = arguments.map {
|
||||||
if (it.first != null)
|
if (it.first != null)
|
||||||
it.first!!.toKotlin() + " = " + it.second.toKotlin()
|
it.first!!.toKotlin() + " = " + it.second.toKotlin()
|
||||||
else
|
else
|
||||||
it.second.toKotlin()
|
it.second.toKotlin()
|
||||||
}.makeString(", ") + ")"
|
}.makeString(", ")
|
||||||
|
return surroundWithBrackets(name.toKotlin() + "(" + argsText + ")")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun List<Annotation>.toKotlin(): String = if (isNotEmpty()) map { it.toKotlin() }.makeString("\n") + "\n" else ""
|
class Annotations(val annotations: List<Annotation>, val newLines: Boolean) {
|
||||||
|
private val br = if (newLines) "\n" else " "
|
||||||
|
|
||||||
|
fun toKotlin(): String = if (annotations.isNotEmpty()) annotations.map { it.toKotlin() }.makeString(br) + br else ""
|
||||||
|
|
||||||
|
fun plus(other: Annotations) = Annotations(annotations + other.annotations, newLines || other.newLines)
|
||||||
|
|
||||||
|
class object {
|
||||||
|
val Empty = Annotations(listOf(), false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,6 @@ import org.jetbrains.jet.j2k.Converter
|
|||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
|
|
||||||
class AnonymousClassBody(body: ClassBody, val extendsTrait: Boolean)
|
class AnonymousClassBody(body: ClassBody, val extendsTrait: Boolean)
|
||||||
: Class(Identifier(""), MemberComments.Empty, listOf(), setOf(), TypeParameterList.Empty, listOf(), listOf(), listOf(), body) {
|
: Class(Identifier(""), MemberComments.Empty, Annotations.Empty, setOf(), TypeParameterList.Empty, listOf(), listOf(), listOf(), body) {
|
||||||
override fun toKotlin() = body.toKotlin(null)
|
override fun toKotlin() = body.toKotlin(null)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import java.util.ArrayList
|
|||||||
open class Class(
|
open class Class(
|
||||||
val name: Identifier,
|
val name: Identifier,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
val typeParameterList: TypeParameterList,
|
val typeParameterList: TypeParameterList,
|
||||||
val extendsTypes: List<Type>,
|
val extendsTypes: List<Type>,
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import java.util.ArrayList
|
|||||||
abstract class Constructor(
|
abstract class Constructor(
|
||||||
converter: Converter,
|
converter: Converter,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
parameterList: ParameterList,
|
parameterList: ParameterList,
|
||||||
block: Block
|
block: Block
|
||||||
@@ -31,7 +31,7 @@ abstract class Constructor(
|
|||||||
|
|
||||||
class PrimaryConstructor(converter: Converter,
|
class PrimaryConstructor(converter: Converter,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
parameterList: ParameterList,
|
parameterList: ParameterList,
|
||||||
block: Block)
|
block: Block)
|
||||||
@@ -48,7 +48,7 @@ class PrimaryConstructor(converter: Converter,
|
|||||||
|
|
||||||
class SecondaryConstructor(converter: Converter,
|
class SecondaryConstructor(converter: Converter,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
parameterList: ParameterList,
|
parameterList: ParameterList,
|
||||||
block: Block)
|
block: Block)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ package org.jetbrains.jet.j2k.ast
|
|||||||
class Enum(
|
class Enum(
|
||||||
name: Identifier,
|
name: Identifier,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
typeParameterList: TypeParameterList,
|
typeParameterList: TypeParameterList,
|
||||||
extendsTypes: List<Type>,
|
extendsTypes: List<Type>,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ package org.jetbrains.jet.j2k.ast
|
|||||||
class EnumConstant(
|
class EnumConstant(
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
members: MemberComments,
|
members: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
`type`: Type,
|
`type`: Type,
|
||||||
params: Element
|
params: Element
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import java.util.ArrayList
|
|||||||
open class Field(
|
open class Field(
|
||||||
val identifier: Identifier,
|
val identifier: Identifier,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
val `type`: Type,
|
val `type`: Type,
|
||||||
val initializer: Element,
|
val initializer: Element,
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ open class Function(
|
|||||||
val converter: Converter,
|
val converter: Converter,
|
||||||
val name: Identifier,
|
val name: Identifier,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
val `type`: Type,
|
val `type`: Type,
|
||||||
val typeParameterList: TypeParameterList,
|
val typeParameterList: TypeParameterList,
|
||||||
|
|||||||
@@ -16,9 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.j2k.ast
|
package org.jetbrains.jet.j2k.ast
|
||||||
|
|
||||||
|
class Initializer(val block: Block, modifiers: Set<Modifier>) : Member(MemberComments.Empty, Annotations.Empty, modifiers) {
|
||||||
//TODO: is a member?
|
|
||||||
class Initializer(val block: Block, modifiers: Set<Modifier>) : Member(MemberComments.Empty, listOf(), modifiers) {
|
|
||||||
override fun toKotlin(): String {
|
override fun toKotlin(): String {
|
||||||
return block.toKotlin()
|
return block.toKotlin()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import org.jetbrains.jet.j2k.ConverterSettings
|
|||||||
|
|
||||||
class LocalVariable(
|
class LocalVariable(
|
||||||
private val identifier: Identifier,
|
private val identifier: Identifier,
|
||||||
private val annotations: List<Annotation>,
|
private val annotations: Annotations,
|
||||||
private val modifiers: Set<Modifier>,
|
private val modifiers: Set<Modifier>,
|
||||||
private val typeCalculator: () -> Type /* we use lazy type calculation for better performance */,
|
private val typeCalculator: () -> Type /* we use lazy type calculation for better performance */,
|
||||||
private val initializer: Expression,
|
private val initializer: Expression,
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class MemberComments(elements: List<Element>) : WhiteSpaceSeparatedElementList(e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Member(val comments: MemberComments, val annotations: List<Annotation>, val modifiers: Set<Modifier>) : Element {
|
abstract class Member(val comments: MemberComments, val annotations: Annotations, val modifiers: Set<Modifier>) : Element {
|
||||||
fun commentsToKotlin(): String = comments.toKotlin()
|
fun commentsToKotlin(): String = comments.toKotlin()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ package org.jetbrains.jet.j2k.ast
|
|||||||
class Parameter(val identifier: Identifier,
|
class Parameter(val identifier: Identifier,
|
||||||
val `type`: Type,
|
val `type`: Type,
|
||||||
val varVal: Parameter.VarValModifier,
|
val varVal: Parameter.VarValModifier,
|
||||||
val annotations: List<Annotation>,
|
val annotations: Annotations,
|
||||||
val modifiers: Collection<Modifier>) : Element {
|
val modifiers: Collection<Modifier>) : Element {
|
||||||
public enum class VarValModifier {
|
public enum class VarValModifier {
|
||||||
None
|
None
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import java.util.ArrayList
|
|||||||
|
|
||||||
class Trait(name: Identifier,
|
class Trait(name: Identifier,
|
||||||
comments: MemberComments,
|
comments: MemberComments,
|
||||||
annotations: List<Annotation>,
|
annotations: Annotations,
|
||||||
modifiers: Set<Modifier>,
|
modifiers: Set<Modifier>,
|
||||||
typeParameterList: TypeParameterList,
|
typeParameterList: TypeParameterList,
|
||||||
extendsTypes: List<Type>,
|
extendsTypes: List<Type>,
|
||||||
|
|||||||
@@ -8,4 +8,20 @@ import javaApi.*;
|
|||||||
@Anon5(1)
|
@Anon5(1)
|
||||||
@Anon6({"x", "y"})
|
@Anon6({"x", "y"})
|
||||||
class C {
|
class C {
|
||||||
|
@Anon5(1) @Deprecated private int field1 = 0;
|
||||||
|
|
||||||
|
@Anon5(1)
|
||||||
|
private int field2 = 0;
|
||||||
|
|
||||||
|
@Anon5(1) int field3 = 0;
|
||||||
|
|
||||||
|
@Anon5(1)
|
||||||
|
int field4 = 0;
|
||||||
|
|
||||||
|
@Anon6({})
|
||||||
|
void foo(@Deprecated int p1, @Deprecated @Anon5(2) char p2) {
|
||||||
|
@Deprecated @Anon5(3) char c = 'a';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Anon5(1) void bar(){}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,4 +6,22 @@ Anon3(e = E.A, stringArray = array<String>(), value = *array<String>("a", "b"))
|
|||||||
Anon4("x", "y")
|
Anon4("x", "y")
|
||||||
Anon5(1)
|
Anon5(1)
|
||||||
Anon6(array<String>("x", "y"))
|
Anon6(array<String>("x", "y"))
|
||||||
class C()
|
class C() {
|
||||||
|
Anon5(1) Deprecated private var field1: Int = 0
|
||||||
|
|
||||||
|
Anon5(1)
|
||||||
|
private var field2: Int = 0
|
||||||
|
|
||||||
|
Anon5(1) var field3: Int = 0
|
||||||
|
|
||||||
|
Anon5(1)
|
||||||
|
var field4: Int = 0
|
||||||
|
|
||||||
|
Anon6(array<String>())
|
||||||
|
fun foo(Deprecated p1: Int, Deprecated Anon5(2) p2: Char) {
|
||||||
|
[Deprecated] [Anon5(3)] val c = 'a'
|
||||||
|
}
|
||||||
|
|
||||||
|
Anon5(1) fun bar() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user