Updating java to kotlin convert to new language syntax
This commit is contained in:
@@ -43,9 +43,9 @@ class AnnotationConverter(private val converter: Converter) {
|
||||
var child: PsiElement? = modifierList
|
||||
while (true) {
|
||||
child = child!!.getNextSibling()
|
||||
if (child == null || child!!.getTextLength() != 0) break
|
||||
if (child == null || child.getTextLength() != 0) break
|
||||
}
|
||||
if (child is PsiWhiteSpace) !child!!.isInSingleLine() else false
|
||||
if (child is PsiWhiteSpace) !child.isInSingleLine() else false
|
||||
}
|
||||
|
||||
annotations.map { convertAnnotation(it, owner is PsiLocalVariable, newLines) }.filterNotNull() //TODO: brackets are also needed for local classes
|
||||
@@ -88,11 +88,11 @@ class AnnotationConverter(private val converter: Converter) {
|
||||
PsiModifier.TRANSIENT to "transient"
|
||||
)
|
||||
|
||||
public fun convertAnnotation(annotation: PsiAnnotation, brackets: Boolean, newLineAfter: Boolean): Annotation? {
|
||||
public fun convertAnnotation(annotation: PsiAnnotation, withAt: Boolean, newLineAfter: Boolean): Annotation? {
|
||||
val qualifiedName = annotation.getQualifiedName()
|
||||
if (qualifiedName == CommonClassNames.JAVA_LANG_DEPRECATED && annotation.getParameterList().getAttributes().isEmpty()) {
|
||||
val deferredExpression = converter.deferredElement<Expression> { LiteralExpression("\"\"").assignNoPrototype() }
|
||||
return Annotation(Identifier("deprecated").assignNoPrototype(), listOf(null to deferredExpression), brackets, newLineAfter).assignPrototype(annotation) //TODO: insert comment
|
||||
return Annotation(Identifier("deprecated").assignNoPrototype(), listOf(null to deferredExpression), withAt, newLineAfter).assignPrototype(annotation) //TODO: insert comment
|
||||
}
|
||||
|
||||
val nameRef = annotation.getNameReferenceElement()
|
||||
@@ -111,7 +111,7 @@ class AnnotationConverter(private val converter: Converter) {
|
||||
|
||||
attrValues.map { attrName to converter.deferredElement(it) }
|
||||
}
|
||||
return Annotation(name, arguments, brackets, newLineAfter).assignPrototype(annotation)
|
||||
return Annotation(name, arguments, withAt, newLineAfter).assignPrototype(annotation)
|
||||
}
|
||||
|
||||
public fun convertAnnotationMethodDefault(method: PsiAnnotationMethod): DeferredElement<Expression>? {
|
||||
|
||||
@@ -424,7 +424,7 @@ class Converter private(
|
||||
function.annotations += Annotations(
|
||||
listOf(Annotation(Identifier("jvmOverloads").assignNoPrototype(),
|
||||
listOf(),
|
||||
brackets = function is PrimaryConstructor,
|
||||
withAt = function is PrimaryConstructor,
|
||||
newLineAfter = false).assignNoPrototype())).assignNoPrototype()
|
||||
}
|
||||
|
||||
|
||||
@@ -18,33 +18,26 @@ package org.jetbrains.kotlin.j2k.ast
|
||||
|
||||
import org.jetbrains.kotlin.j2k.*
|
||||
|
||||
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, DeferredElement<Expression>>>, val brackets: Boolean, val newLineAfter: Boolean) : Element() {
|
||||
private fun CodeBuilder.surroundWithBrackets(action: () -> Unit) {
|
||||
if (brackets) append("[")
|
||||
action()
|
||||
if (brackets) append("]")
|
||||
}
|
||||
|
||||
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, DeferredElement<Expression>>>, val withAt: Boolean, val newLineAfter: Boolean) : Element() {
|
||||
override fun generateCode(builder: CodeBuilder) {
|
||||
if (withAt) builder.append("@")
|
||||
if (arguments.isEmpty()) {
|
||||
builder.surroundWithBrackets { builder.append(name) }
|
||||
builder.append(name)
|
||||
}
|
||||
else {
|
||||
builder.surroundWithBrackets {
|
||||
builder.append(name)
|
||||
.append("(")
|
||||
.append(arguments.map {
|
||||
{
|
||||
if (it.first != null) {
|
||||
builder append it.first!! append " = " append it.second
|
||||
}
|
||||
else {
|
||||
builder append it.second
|
||||
}
|
||||
builder.append(name)
|
||||
.append("(")
|
||||
.append(arguments.map {
|
||||
{
|
||||
if (it.first != null) {
|
||||
builder append it.first!! append " = " append it.second
|
||||
}
|
||||
}, ", ")
|
||||
.append(")")
|
||||
}
|
||||
else {
|
||||
builder append it.second
|
||||
}
|
||||
}
|
||||
}, ", ")
|
||||
.append(")")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,5 +62,5 @@ class Annotations(val annotations: List<Annotation>) : Element() {
|
||||
}
|
||||
}
|
||||
|
||||
fun Annotations.withBrackets(): Annotations
|
||||
fun Annotations.withAt(): Annotations
|
||||
= Annotations(annotations.map { Annotation(it.name, it.arguments, true, it.newLineAfter).assignPrototypesFrom(it) }).assignPrototypesFrom(this)
|
||||
|
||||
@@ -58,12 +58,20 @@ class PrimaryConstructorSignature(val annotations: Annotations, private val modi
|
||||
}
|
||||
|
||||
override fun generateCode(builder: CodeBuilder) {
|
||||
var needConstructorKeyword = false
|
||||
|
||||
if (!annotations.isEmpty) {
|
||||
builder append " " append annotations.withBrackets()
|
||||
builder append " " append annotations.withAt()
|
||||
needConstructorKeyword = true
|
||||
}
|
||||
|
||||
if (accessModifier != null) {
|
||||
builder append " " append Modifiers(listOf(accessModifier)).assignPrototypesFrom(modifiers)
|
||||
needConstructorKeyword = true
|
||||
}
|
||||
|
||||
if (needConstructorKeyword) {
|
||||
builder append " constructor"
|
||||
}
|
||||
|
||||
builder append "(" append parameterList append ")"
|
||||
|
||||
@@ -21,7 +21,7 @@ class C {
|
||||
|
||||
Anon6
|
||||
fun foo(deprecated("") p1: Int, deprecated("") Anon5(2) p2: Char) {
|
||||
[deprecated("")] [Anon5(3)] val c = 'a'
|
||||
@deprecated("") @Anon5(3) val c = 'a'
|
||||
}
|
||||
|
||||
Anon5(1) fun bar() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class A// this is a primary constructor
|
||||
[jvmOverloads] (p: Int = 1) {
|
||||
@jvmOverloads constructor(p: Int = 1) {
|
||||
private val v: Int
|
||||
|
||||
init {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package pack
|
||||
|
||||
class C [jvmOverloads] (arg1: Int, arg2: Int = 0, arg3: Int = 0)
|
||||
class C @jvmOverloads constructor(arg1: Int, arg2: Int = 0, arg3: Int = 0)
|
||||
|
||||
public object User {
|
||||
public fun main() {
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import javaApi.Anon5
|
||||
|
||||
class A
|
||||
[Anon5(10)]
|
||||
(private val a: Int, private val b: Int) {
|
||||
@Anon5(10)
|
||||
constructor(private val a: Int, private val b: Int) {
|
||||
|
||||
deprecated("") // this constructor will not be replaced by default parameter value in primary because of this annotation
|
||||
public constructor(a: Int) : this(a, 1) {
|
||||
}
|
||||
}
|
||||
|
||||
class B [Anon5(11)]
|
||||
()
|
||||
class B @Anon5(11)
|
||||
constructor()
|
||||
|
||||
class C [Anon5(12)]
|
||||
private ()
|
||||
class C @Anon5(12)
|
||||
private constructor()
|
||||
@@ -1 +1 @@
|
||||
class C [jvmOverloads] (private val string: String, a: Int = string.length())
|
||||
class C @jvmOverloads constructor(private val string: String, a: Int = string.length())
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class A [jvmOverloads] (nested: A.Nested = A.Nested(A.Nested.FIELD)) {
|
||||
class A @jvmOverloads constructor(nested: A.Nested = A.Nested(A.Nested.FIELD)) {
|
||||
|
||||
class Nested(p: Int) {
|
||||
companion object {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// ERROR: Property must be initialized or be abstract
|
||||
import A.Nested
|
||||
|
||||
class A [jvmOverloads] (nested: Nested = Nested(Nested.FIELD)) {
|
||||
class A @jvmOverloads constructor(nested: Nested = Nested(Nested.FIELD)) {
|
||||
|
||||
class Nested(p: Int) {
|
||||
companion object {
|
||||
|
||||
@@ -3,7 +3,7 @@ package pack
|
||||
|
||||
import pack.A.Nested
|
||||
|
||||
class A [jvmOverloads] (nested: Nested = Nested(Nested.FIELD)) {
|
||||
class A @jvmOverloads constructor(nested: Nested = Nested(Nested.FIELD)) {
|
||||
|
||||
class Nested(p: Int) {
|
||||
companion object {
|
||||
|
||||
@@ -3,7 +3,7 @@ package pack
|
||||
|
||||
import pack.A.*
|
||||
|
||||
class A [jvmOverloads] (nested: Nested = Nested(Nested.FIELD)) {
|
||||
class A @jvmOverloads constructor(nested: Nested = Nested(Nested.FIELD)) {
|
||||
|
||||
class Nested(p: Int) {
|
||||
companion object {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package pack
|
||||
|
||||
class C [jvmOverloads] (a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
||||
class C @jvmOverloads constructor(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
||||
|
||||
constructor(a: Int) : this(a, 0, 0, 0, 1) {
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package pack
|
||||
|
||||
class C [jvmOverloads] (a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
||||
class C @jvmOverloads constructor(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
||||
|
||||
constructor(a1: Int, b1: Int, c1: Int) : this(a1, b1, c1, 0, 0) {
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package pack
|
||||
|
||||
class C [jvmOverloads] (a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
||||
class C @jvmOverloads constructor(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
||||
|
||||
constructor(a: Int, b: Int, c: Int) : this(b, a, c, 0, 0) {
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package pack
|
||||
|
||||
class C [jvmOverloads] (a: Int = 1, b: Int = 2, c: Int = 3, d: Int = 4, e: Int = 5)
|
||||
class C @jvmOverloads constructor(a: Int = 1, b: Int = 2, c: Int = 3, d: Int = 4, e: Int = 5)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package pack
|
||||
|
||||
class C [jvmOverloads] (a: Int = 1, b: Int = 2, c: Int = 3, d: Int = 4, e: Int = 5)
|
||||
class C @jvmOverloads constructor(a: Int = 1, b: Int = 2, c: Int = 3, d: Int = 4, e: Int = 5)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// ERROR: Overload resolution ambiguity: public constructor C(arg1: kotlin.Int, arg2: kotlin.Int) defined in C kotlin.jvm.jvmOverloads public constructor C(arg1: kotlin.Int, arg2: kotlin.Int = ..., arg3: kotlin.Int = ...) defined in C
|
||||
class C [jvmOverloads] (arg1: Int, arg2: Int = 0, arg3: Int = 0) {
|
||||
class C @jvmOverloads constructor(arg1: Int, arg2: Int = 0, arg3: Int = 0) {
|
||||
private val field: Int
|
||||
|
||||
init {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class C [jvmOverloads] private (arg1: Int, arg2: Int, arg3: Int = 0) {
|
||||
class C @jvmOverloads private constructor(arg1: Int, arg2: Int, arg3: Int = 0) {
|
||||
|
||||
public constructor(arg1: Int) : this(arg1, 0, 0) {
|
||||
}
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
public class C [jvmOverloads] (c: C, public val x: Int = c.x)
|
||||
public class C @jvmOverloads constructor(c: C, public val x: Int = c.x)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package demo
|
||||
|
||||
enum class MyEnum private (public val color: Int) {
|
||||
enum class MyEnum private constructor(public val color: Int) {
|
||||
RED : MyEnum(10)
|
||||
BLUE : MyEnum(20)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
enum class Color private (public val code: Int) {
|
||||
enum class Color private constructor(public val code: Int) {
|
||||
WHITE : Color(21)
|
||||
BLACK : Color(22)
|
||||
RED : Color(23)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package demo
|
||||
|
||||
enum class Color private (public val code: Int)
|
||||
enum class Color private constructor(public val code: Int)
|
||||
Reference in New Issue
Block a user