Converter from Java: added proper annotations formatting

This commit is contained in:
Valentin Kipyatkov
2014-06-11 22:50:48 +04:00
parent c0a5355928
commit 26b6626358
16 changed files with 96 additions and 33 deletions
+26 -10
View File
@@ -72,7 +72,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
is PsiComment -> Comment(element.getText()!!)
is PsiImportList -> convertImportList(element)
is PsiImportStatementBase -> convertImport(element, false)
is PsiAnnotation -> convertAnnotation(element)
is PsiAnnotation -> convertAnnotation(element, false)
is PsiPackageStatement -> PackageStatement(quoteKeywords(element.getPackageName() ?: ""))
is PsiWhiteSpace -> WhiteSpace(element.getText()!!)
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) })
}
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) })
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,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
comments: MemberComments,
membersToRemove: MutableSet<PsiMember>): PrimaryConstructor {
@@ -610,14 +610,30 @@ public class Converter private(val project: Project, val settings: ConverterSett
PsiModifier.PRIVATE to Modifier.PRIVATE
)
public fun convertAnnotations(owner: PsiModifierListOwner): List<Annotation> {
return owner.getModifierList()?.getAnnotations()
?.filter { it.getQualifiedName() !in ANNOTATIONS_TO_REMOVE }
?.map { convertAnnotation(it) }
?.filterNotNull() ?: listOf()
public fun convertAnnotations(owner: PsiModifierListOwner): Annotations {
val modifierList = owner.getModifierList()
val annotations = modifierList?.getAnnotations()?.filter { it.getQualifiedName() !in ANNOTATIONS_TO_REMOVE }
if (annotations == null || annotations.isEmpty()) return Annotations.Empty
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 annotationClass = annotation.getNameReferenceElement()?.resolve() as? PsiClass
val lastMethod = annotationClass?.getMethods()?.lastOrNull()
@@ -653,7 +669,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
attrValues.map { attrName to it }
}
return Annotation(name, arguments)
return Annotation(name, arguments, brackets)
}
private val TYPE_MAP: Map<String, String> = mapOf(
@@ -18,17 +18,32 @@ package org.jetbrains.jet.j2k.ast
import java.util.HashSet
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, Expression>>) : Element {
override fun toKotlin(): String {
if (arguments.isEmpty()) return name.toKotlin()
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, Expression>>, val brackets: Boolean) : Element {
private fun surroundWithBrackets(text: String) = if (brackets) "[$text]" else text
return name.toKotlin() + "(" + arguments.map {
override fun toKotlin(): String {
if (arguments.isEmpty()) {
return surroundWithBrackets(name.toKotlin())
}
val argsText = arguments.map {
if (it.first != null)
it.first!!.toKotlin() + " = " + it.second.toKotlin()
else
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
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)
}
+1 -1
View File
@@ -21,7 +21,7 @@ import java.util.ArrayList
open class Class(
val name: Identifier,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
val typeParameterList: TypeParameterList,
val extendsTypes: List<Type>,
@@ -23,7 +23,7 @@ import java.util.ArrayList
abstract class Constructor(
converter: Converter,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
parameterList: ParameterList,
block: Block
@@ -31,7 +31,7 @@ abstract class Constructor(
class PrimaryConstructor(converter: Converter,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
parameterList: ParameterList,
block: Block)
@@ -48,7 +48,7 @@ class PrimaryConstructor(converter: Converter,
class SecondaryConstructor(converter: Converter,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
parameterList: ParameterList,
block: Block)
+1 -1
View File
@@ -19,7 +19,7 @@ package org.jetbrains.jet.j2k.ast
class Enum(
name: Identifier,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
typeParameterList: TypeParameterList,
extendsTypes: List<Type>,
@@ -19,7 +19,7 @@ package org.jetbrains.jet.j2k.ast
class EnumConstant(
identifier: Identifier,
members: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
`type`: Type,
params: Element
+1 -1
View File
@@ -22,7 +22,7 @@ import java.util.ArrayList
open class Field(
val identifier: Identifier,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
val `type`: Type,
val initializer: Element,
@@ -23,7 +23,7 @@ open class Function(
val converter: Converter,
val name: Identifier,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
val `type`: Type,
val typeParameterList: TypeParameterList,
@@ -16,9 +16,7 @@
package org.jetbrains.jet.j2k.ast
//TODO: is a member?
class Initializer(val block: Block, modifiers: Set<Modifier>) : Member(MemberComments.Empty, listOf(), modifiers) {
class Initializer(val block: Block, modifiers: Set<Modifier>) : Member(MemberComments.Empty, Annotations.Empty, modifiers) {
override fun toKotlin(): String {
return block.toKotlin()
}
@@ -20,7 +20,7 @@ import org.jetbrains.jet.j2k.ConverterSettings
class LocalVariable(
private val identifier: Identifier,
private val annotations: List<Annotation>,
private val annotations: Annotations,
private val modifiers: Set<Modifier>,
private val typeCalculator: () -> Type /* we use lazy type calculation for better performance */,
private val initializer: Expression,
+1 -1
View File
@@ -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()
}
@@ -19,7 +19,7 @@ package org.jetbrains.jet.j2k.ast
class Parameter(val identifier: Identifier,
val `type`: Type,
val varVal: Parameter.VarValModifier,
val annotations: List<Annotation>,
val annotations: Annotations,
val modifiers: Collection<Modifier>) : Element {
public enum class VarValModifier {
None
+1 -1
View File
@@ -21,7 +21,7 @@ import java.util.ArrayList
class Trait(name: Identifier,
comments: MemberComments,
annotations: List<Annotation>,
annotations: Annotations,
modifiers: Set<Modifier>,
typeParameterList: TypeParameterList,
extendsTypes: List<Type>,
@@ -8,4 +8,20 @@ import javaApi.*;
@Anon5(1)
@Anon6({"x", "y"})
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")
Anon5(1)
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() {
}
}