Generated constructors with default parameter values are marked with kotlin.jvm.overloads annotation
This commit is contained in:
@@ -17,10 +17,11 @@
|
|||||||
package org.jetbrains.kotlin.j2k
|
package org.jetbrains.kotlin.j2k
|
||||||
|
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import org.jetbrains.kotlin.j2k.ast.*
|
|
||||||
import com.intellij.psi.util.PsiUtil
|
import com.intellij.psi.util.PsiUtil
|
||||||
import java.util.HashMap
|
import org.jetbrains.kotlin.j2k.ast.*
|
||||||
|
import org.jetbrains.kotlin.j2k.ast.Annotation
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
import java.util.HashMap
|
||||||
import java.util.HashSet
|
import java.util.HashSet
|
||||||
|
|
||||||
class ConstructorConverter(
|
class ConstructorConverter(
|
||||||
@@ -152,9 +153,9 @@ class ConstructorConverter(
|
|||||||
annotations: Annotations,
|
annotations: Annotations,
|
||||||
modifiers: Modifiers,
|
modifiers: Modifiers,
|
||||||
membersToRemove: MutableSet<PsiMember>,
|
membersToRemove: MutableSet<PsiMember>,
|
||||||
postProcessBody: (Block) -> Block): Member? {
|
postProcessBody: (Block) -> Block): Constructor? {
|
||||||
if (constructor == primaryConstructor) {
|
val result = if (constructor == primaryConstructor) {
|
||||||
return convertPrimaryConstructor(annotations, modifiers, membersToRemove, postProcessBody)
|
convertPrimaryConstructor(annotations, modifiers, membersToRemove, postProcessBody)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (constructor in constructorsToDrop) return null
|
if (constructor in constructorsToDrop) return null
|
||||||
@@ -178,9 +179,17 @@ class ConstructorConverter(
|
|||||||
}).convertBlock(constructor.getBody()))
|
}).convertBlock(constructor.getBody()))
|
||||||
}
|
}
|
||||||
|
|
||||||
return SecondaryConstructor(annotations, modifiers, params,
|
SecondaryConstructor(annotations, modifiers, params, converter.deferredElement(::convertBody), thisOrSuperDeferred)
|
||||||
converter.deferredElement(::convertBody), thisOrSuperDeferred)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result.parameterList.parameters.any { it.defaultValue != null }) {
|
||||||
|
result.annotations += Annotations(listOf(Annotation(Identifier("overloads").assignNoPrototype(),
|
||||||
|
listOf(),
|
||||||
|
brackets = result is PrimaryConstructor,
|
||||||
|
newLineAfter = false).assignNoPrototype())).assignNoPrototype()
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findThisOrSuperCall(constructor: PsiMethod): PsiExpressionStatement? {
|
private fun findThisOrSuperCall(constructor: PsiMethod): PsiExpressionStatement? {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.j2k.ast
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.j2k.*
|
import org.jetbrains.kotlin.j2k.*
|
||||||
|
|
||||||
abstract class Member(val annotations: Annotations, val modifiers: Modifiers) : Element()
|
abstract class Member(var annotations: Annotations, val modifiers: Modifiers) : Element()
|
||||||
|
|
||||||
class ClassBody (
|
class ClassBody (
|
||||||
val primaryConstructorSignature: PrimaryConstructorSignature?,
|
val primaryConstructorSignature: PrimaryConstructorSignature?,
|
||||||
|
|||||||
@@ -19,12 +19,19 @@ package org.jetbrains.kotlin.j2k.ast
|
|||||||
import org.jetbrains.kotlin.j2k.*
|
import org.jetbrains.kotlin.j2k.*
|
||||||
import com.intellij.util.IncorrectOperationException
|
import com.intellij.util.IncorrectOperationException
|
||||||
|
|
||||||
|
abstract class Constructor(
|
||||||
|
annotations: Annotations,
|
||||||
|
modifiers: Modifiers,
|
||||||
|
public val parameterList: ParameterList,
|
||||||
|
protected val body: DeferredElement<Block>
|
||||||
|
) : Member(annotations, modifiers)
|
||||||
|
|
||||||
class PrimaryConstructor(
|
class PrimaryConstructor(
|
||||||
annotations: Annotations,
|
annotations: Annotations,
|
||||||
modifiers: Modifiers,
|
modifiers: Modifiers,
|
||||||
val parameterList: ParameterList,
|
parameterList: ParameterList,
|
||||||
val body: DeferredElement<Block>
|
body: DeferredElement<Block>
|
||||||
) : Member(annotations, modifiers) {
|
) : Constructor(annotations, modifiers, parameterList, body) {
|
||||||
|
|
||||||
override fun generateCode(builder: CodeBuilder) { throw IncorrectOperationException() }
|
override fun generateCode(builder: CodeBuilder) { throw IncorrectOperationException() }
|
||||||
|
|
||||||
@@ -66,10 +73,10 @@ class PrimaryConstructorSignature(val annotations: Annotations, private val modi
|
|||||||
class SecondaryConstructor(
|
class SecondaryConstructor(
|
||||||
annotations: Annotations,
|
annotations: Annotations,
|
||||||
modifiers: Modifiers,
|
modifiers: Modifiers,
|
||||||
private val parameterList: ParameterList,
|
parameterList: ParameterList,
|
||||||
private val body: DeferredElement<Block>,
|
body: DeferredElement<Block>,
|
||||||
private val thisOrSuperCall: DeferredElement<Expression>?
|
private val thisOrSuperCall: DeferredElement<Expression>?
|
||||||
) : Member(annotations, modifiers) {
|
) : Constructor(annotations, modifiers, parameterList, body) {
|
||||||
|
|
||||||
override fun generateCode(builder: CodeBuilder) {
|
override fun generateCode(builder: CodeBuilder) {
|
||||||
builder.append(annotations)
|
builder.append(annotations)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class A// this is a primary constructor
|
class A// this is a primary constructor
|
||||||
(p: Int = 1) {
|
[overloads] (p: Int = 1) {
|
||||||
private val v: Int
|
private val v: Int
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package pack
|
package pack
|
||||||
|
|
||||||
class C(arg1: Int, arg2: Int = 0, arg3: Int = 0)
|
class C [overloads] (arg1: Int, arg2: Int = 0, arg3: Int = 0)
|
||||||
|
|
||||||
public object User {
|
public object User {
|
||||||
public fun main() {
|
public fun main() {
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
class C(private val string: String, a: Int = string.length())
|
class C [overloads] (private val string: String, a: Int = string.length())
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
class A(nested: A.Nested = A.Nested(A.Nested.FIELD)) {
|
class A [overloads] (nested: A.Nested = A.Nested(A.Nested.FIELD)) {
|
||||||
|
|
||||||
class Nested(p: Int) {
|
class Nested(p: Int) {
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// ERROR: Property must be initialized or be abstract
|
// ERROR: Property must be initialized or be abstract
|
||||||
import A.Nested
|
import A.Nested
|
||||||
|
|
||||||
class A(nested: Nested = Nested(Nested.FIELD)) {
|
class A [overloads] (nested: Nested = Nested(Nested.FIELD)) {
|
||||||
|
|
||||||
class Nested(p: Int) {
|
class Nested(p: Int) {
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package pack
|
|||||||
|
|
||||||
import pack.A.Nested
|
import pack.A.Nested
|
||||||
|
|
||||||
class A(nested: Nested = Nested(Nested.FIELD)) {
|
class A [overloads] (nested: Nested = Nested(Nested.FIELD)) {
|
||||||
|
|
||||||
class Nested(p: Int) {
|
class Nested(p: Int) {
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package pack
|
|||||||
|
|
||||||
import pack.A.*
|
import pack.A.*
|
||||||
|
|
||||||
class A(nested: Nested = Nested(Nested.FIELD)) {
|
class A [overloads] (nested: Nested = Nested(Nested.FIELD)) {
|
||||||
|
|
||||||
class Nested(p: Int) {
|
class Nested(p: Int) {
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package pack
|
package pack
|
||||||
|
|
||||||
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
class C [overloads] (a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
||||||
|
|
||||||
constructor(a: Int) : this(a, 0, 0, 0, 1) {
|
constructor(a: Int) : this(a, 0, 0, 0, 1) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package pack
|
package pack
|
||||||
|
|
||||||
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
class C [overloads] (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) {
|
constructor(a1: Int, b1: Int, c1: Int) : this(a1, b1, c1, 0, 0) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package pack
|
package pack
|
||||||
|
|
||||||
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
|
class C [overloads] (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) {
|
constructor(a: Int, b: Int, c: Int) : this(b, a, c, 0, 0) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
package pack
|
package pack
|
||||||
|
|
||||||
class C(a: Int = 1, b: Int = 2, c: Int = 3, d: Int = 4, e: Int = 5)
|
class C [overloads] (a: Int = 1, b: Int = 2, c: Int = 3, d: Int = 4, e: Int = 5)
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
package pack
|
package pack
|
||||||
|
|
||||||
class C(a: Int = 1, b: Int = 2, c: Int = 3, d: Int = 4, e: Int = 5)
|
class C [overloads] (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 public constructor C(arg1: kotlin.Int, arg2: kotlin.Int = ..., arg3: kotlin.Int = ...) defined in C
|
// ERROR: Overload resolution ambiguity: public constructor C(arg1: kotlin.Int, arg2: kotlin.Int) defined in C kotlin.jvm.overloads public constructor C(arg1: kotlin.Int, arg2: kotlin.Int = ..., arg3: kotlin.Int = ...) defined in C
|
||||||
class C(arg1: Int, arg2: Int = 0, arg3: Int = 0) {
|
class C [overloads] (arg1: Int, arg2: Int = 0, arg3: Int = 0) {
|
||||||
private val field: Int
|
private val field: Int
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
class C private (arg1: Int, arg2: Int, arg3: Int = 0) {
|
class C [overloads] private (arg1: Int, arg2: Int, arg3: Int = 0) {
|
||||||
|
|
||||||
public constructor(arg1: Int) : this(arg1, 0, 0) {
|
public constructor(arg1: Int) : this(arg1, 0, 0) {
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
public class C(c: C, public val x: Int = c.x)
|
public class C [overloads] (c: C, public val x: Int = c.x)
|
||||||
|
|||||||
Reference in New Issue
Block a user