Support for secondary constructors in j2k

This commit is contained in:
Valentin Kipyatkov
2015-03-06 19:21:30 +01:00
parent b44dbe6ca2
commit 569e4a68ae
33 changed files with 367 additions and 589 deletions
@@ -81,7 +81,6 @@ class ClassBodyConverter(private val psiClass: PsiClass,
val members = ArrayList<Member>() val members = ArrayList<Member>()
val defaultObjectMembers = ArrayList<Member>() val defaultObjectMembers = ArrayList<Member>()
val factoryFunctions = ArrayList<FactoryFunction>()
var primaryConstructorSignature: PrimaryConstructorSignature? = null var primaryConstructorSignature: PrimaryConstructorSignature? = null
for ((psiMember, member) in convertedMembers) { for ((psiMember, member) in convertedMembers) {
if (member is PrimaryConstructor) { if (member is PrimaryConstructor) {
@@ -89,11 +88,7 @@ class ClassBodyConverter(private val psiClass: PsiClass,
primaryConstructorSignature = member.createSignature(converter) primaryConstructorSignature = member.createSignature(converter)
members.add(member.initializer()) members.add(member.initializer())
} }
else if (member is FactoryFunction) { else if (useDefaultObject && member !is Class && psiMember.hasModifierProperty(PsiModifier.STATIC)) {
factoryFunctions.add(member)
}
else if (useDefaultObject
&& (if (member is Class) shouldGenerateIntoDefaultObject(member) else psiMember.hasModifierProperty(PsiModifier.STATIC))) {
defaultObjectMembers.add(member) defaultObjectMembers.add(member)
} }
else { else {
@@ -101,11 +96,19 @@ class ClassBodyConverter(private val psiClass: PsiClass,
} }
} }
if (primaryConstructorSignature != null
&& primaryConstructorSignature!!.annotations.isEmpty
&& primaryConstructorSignature!!.accessModifier == null
&& primaryConstructorSignature!!.parameterList.parameters.isEmpty()
&& members.none { it is SecondaryConstructor }
) {
primaryConstructorSignature = null // no "()" after class name is needed in this case
}
val lBrace = LBrace().assignPrototype(psiClass.getLBrace()) val lBrace = LBrace().assignPrototype(psiClass.getLBrace())
val rBrace = RBrace().assignPrototype(psiClass.getRBrace()) val rBrace = RBrace().assignPrototype(psiClass.getRBrace())
val classBody = ClassBody(primaryConstructorSignature, constructorConverter?.baseClassParams ?: listOf(), members, defaultObjectMembers, factoryFunctions, lBrace, rBrace)
return if (constructorConverter != null) constructorConverter.postProcessConstructors(classBody) else classBody return ClassBody(primaryConstructorSignature, constructorConverter?.baseClassParams ?: listOf(), members, defaultObjectMembers, lBrace, rBrace)
} }
private fun Converter.convertMember(member: PsiMember, private fun Converter.convertMember(member: PsiMember,
@@ -124,8 +127,6 @@ class ClassBodyConverter(private val psiClass: PsiClass,
private fun shouldGenerateDefaultObject(convertedMembers: Map<PsiMember, Member>): Boolean { private fun shouldGenerateDefaultObject(convertedMembers: Map<PsiMember, Member>): Boolean {
if (psiClass.isEnum()) return false if (psiClass.isEnum()) return false
if (convertedMembers.values().any { it is Class && shouldGenerateIntoDefaultObject(it) }) return true
val members = convertedMembers.keySet().filter { !it.isConstructor() } val members = convertedMembers.keySet().filter { !it.isConstructor() }
val defaultObjectMembers = members.filter { it !is PsiClass && it.hasModifierProperty(PsiModifier.STATIC) } val defaultObjectMembers = members.filter { it !is PsiClass && it.hasModifierProperty(PsiModifier.STATIC) }
val nestedClasses = members.filterIsInstance<PsiClass>().filter { it.hasModifierProperty(PsiModifier.STATIC) } val nestedClasses = members.filterIsInstance<PsiClass>().filter { it.hasModifierProperty(PsiModifier.STATIC) }
@@ -137,10 +138,6 @@ class ClassBodyConverter(private val psiClass: PsiClass,
} }
} }
// we generate nested classes with factory functions into default object as a workaround until secondary constructors supported by Kotlin
private fun shouldGenerateIntoDefaultObject(nestedClass: Class)
= !nestedClass.modifiers.contains(Modifier.INNER) && nestedClass.body.factoryFunctions.isNotEmpty()
private fun processAccessorsToDrop() { private fun processAccessorsToDrop() {
val fieldToGetterInfo = HashMap<PsiField, AccessorInfo>() val fieldToGetterInfo = HashMap<PsiField, AccessorInfo>()
val fieldToSetterInfo = HashMap<PsiField, AccessorInfo>() val fieldToSetterInfo = HashMap<PsiField, AccessorInfo>()
@@ -28,12 +28,6 @@ class ConstructorConverter(
private val converter: Converter, private val converter: Converter,
private val fieldCorrections: Map<PsiField, FieldCorrectionInfo> private val fieldCorrections: Map<PsiField, FieldCorrectionInfo>
) { ) {
private val typeConverter = converter.typeConverter
private val tempValName: String = "__"
private fun tempValIdentifier(): Identifier = Identifier(tempValName, false).assignNoPrototype()
private val className = psiClass.getName()!!
private val constructors = psiClass.getConstructors() private val constructors = psiClass.getConstructors()
private val constructorsToDrop = HashSet<PsiMethod>() private val constructorsToDrop = HashSet<PsiMethod>()
private val lastParamDefaults = ArrayList<PsiExpression>() // defaults for a few last parameters of primary constructor in reverse order private val lastParamDefaults = ArrayList<PsiExpression>() // defaults for a few last parameters of primary constructor in reverse order
@@ -166,39 +160,36 @@ class ConstructorConverter(
if (constructor in constructorsToDrop) return null if (constructor in constructorsToDrop) return null
val params = converter.convertParameterList(constructor.getParameterList()) val params = converter.convertParameterList(constructor.getParameterList())
val containingClass = constructor.getContainingClass()
val typeParameterList = converter.convertTypeParameterList(containingClass?.getTypeParameterList()) val thisOrSuper = findThisOrSuperCall(constructor)
val factoryFunctionType = ClassType(ReferenceElement(containingClass?.declarationIdentifier() ?: Identifier.Empty, typeParameterList.parameters).assignNoPrototype(), val thisOrSuperDeferred = if (thisOrSuper != null)
Nullability.NotNull, converter.deferredElement { it.convertExpression(thisOrSuper.getExpression()) }
converter.settings).assignNoPrototype() else
null
fun convertBody(codeConverter: CodeConverter): Block { fun convertBody(codeConverter: CodeConverter): Block {
val bodyConverter = codeConverter.withSpecialExpressionConverter( return postProcessBody(codeConverter.withSpecialExpressionConverter(object: SpecialExpressionConverter {
object : SpecialExpressionConverter { override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? {
override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? { if (expression.isThisConstructorCall() || expression.isSuperConstructorCall()) {
if (expression is PsiReferenceExpression && expression.isQualifierEmptyOrThis()) { return Expression.Empty // skip it
val member = expression.getReference()?.resolve() as? PsiMember }
if (member != null && return null
!member.isConstructor() && }
member.getContainingClass() == constructor.getContainingClass()) { }).convertBlock(constructor.getBody()))
val isNullable = member is PsiField && typeConverter.variableNullability(member).isNullable(codeConverter.settings)
val qualifier = if (member.hasModifierProperty(PsiModifier.STATIC)) constructor.declarationIdentifier() else tempValIdentifier()
val name = fieldCorrections[member]?.name ?: expression.getReferenceName()!!
return QualifiedExpression(qualifier, Identifier(name, isNullable).assignNoPrototype())
}
}
return null
}
})
return postProcessBody(bodyConverter.convertBlock(constructor.getBody()))
} }
return FactoryFunction(constructor.declarationIdentifier(), annotations, correctFactoryFunctionAccess(modifiers), return SecondaryConstructor(annotations, modifiers, params,
factoryFunctionType, params, typeParameterList, converter.deferredElement(::convertBody)) converter.deferredElement(::convertBody), thisOrSuperDeferred)
} }
} }
private fun findThisOrSuperCall(constructor: PsiMethod): PsiExpressionStatement? {
val statement = constructor.getBody()?.getStatements()?.firstOrNull() as? PsiExpressionStatement ?: return null
val methodCall = statement.getExpression() as? PsiMethodCallExpression ?: return null
val text = methodCall.getMethodExpression().getText()
return if (text == "this" || text == "super") statement else null
}
private fun convertPrimaryConstructor(annotations: Annotations, private fun convertPrimaryConstructor(annotations: Annotations,
modifiers: Modifiers, modifiers: Modifiers,
membersToRemove: MutableSet<PsiMember>, membersToRemove: MutableSet<PsiMember>,
@@ -246,7 +237,7 @@ class ConstructorConverter(
val bodyConverter = codeConverter.withSpecialExpressionConverter( val bodyConverter = codeConverter.withSpecialExpressionConverter(
object : ReplacingExpressionConverter(parameterUsageReplacementMap) { object : ReplacingExpressionConverter(parameterUsageReplacementMap) {
override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? { override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? {
if (expression is PsiMethodCallExpression && expression.isSuperConstructorCall()) { if (expression.isSuperConstructorCall()) {
return Expression.Empty // skip it return Expression.Empty // skip it
} }
return super.convertExpression(expression, codeConverter) return super.convertExpression(expression, codeConverter)
@@ -333,142 +324,8 @@ class ConstructorConverter(
return null return null
} }
public fun postProcessConstructors(classBody: ClassBody): ClassBody { private fun PsiExpression.isSuperConstructorCall() = (this as? PsiMethodCallExpression)?.getMethodExpression()?.getText() == "super"
if (primaryConstructor == null && constructors.size() > 1) { private fun PsiExpression.isThisConstructorCall() = (this as? PsiMethodCallExpression)?.getMethodExpression()?.getText() == "this"
return generateArtificialPrimaryConstructor(classBody)
}
else {
processFactoryFunctionsWithConstructorCall(classBody.factoryFunctions)
return classBody
}
}
private fun generateArtificialPrimaryConstructor(classBody: ClassBody): ClassBody {
assert(classBody.primaryConstructorSignature == null)
val propertiesToInitialize = classBody.members.filterIsInstance<Property>().filter { it.isVal }
for (function in classBody.factoryFunctions) {
function.body!!.updateGenerator { (codeConverter, block) ->
// 2 cases: secondary constructor either calls another constructor or does not call any
val newStatements = processFactoryFunctionWithConstructorCall(block) ?:
insertCallToArtificialPrimary(block, propertiesToInitialize)
Block(newStatements, LBrace().assignNoPrototype(), RBrace().assignNoPrototype()).assignNoPrototype()
}
}
val parameters = propertiesToInitialize.map { property ->
val varValModifier = if (property.isVal) Parameter.VarValModifier.Val else Parameter.VarValModifier.Var
Parameter(property.identifier, property.type, varValModifier, property.annotations, property.modifiers.filter { it in ACCESS_MODIFIERS }).assignPrototypesFrom(property)
}
val modifiers = Modifiers.Empty
//TODO: we can generate it private when secondary constructors are supported by Kotlin
//val modifiers = Modifiers(listOf(Modifier.PRIVATE)).assignNoPrototype()
val parameterList = ParameterList(parameters).assignNoPrototype()
val constructorSignature = PrimaryConstructorSignature(Annotations.Empty, modifiers, parameterList).assignNoPrototype()
val updatedMembers = classBody.members.filter { !propertiesToInitialize.contains(it) }
return ClassBody(constructorSignature, classBody.baseClassParams, updatedMembers, classBody.defaultObjectMembers, classBody.factoryFunctions, classBody.lBrace, classBody.rBrace)
}
private fun processFactoryFunctionsWithConstructorCall(functions: List<FactoryFunction>) {
for (function in functions) {
function.body!!.updateGenerator { (codeConverter, block) ->
val statements = processFactoryFunctionWithConstructorCall(block)
if (statements != null) {
Block(statements, block.lBrace, block.rBrace).assignPrototypesFrom(block)
}
else {
block
}
}
}
}
private fun processFactoryFunctionWithConstructorCall(body: Block): List<Statement>? {
val statements = ArrayList(body.statements)
// searching for other constructor call in form "this(...)"
// it's not necessary the first statement because of statements inserted for writable parameters
for ((i, statement) in statements.withIndex()) {
if (statement is MethodCallExpression) {
if ((statement.methodExpression as? Identifier)?.name == "this") {
val constructorCall = MethodCallExpression.buildNotNull(null, className, statement.arguments).assignPrototypesFrom(statement)
if (i == statements.lastIndex) { // constructor call is the last statement - no intermediate variable needed
statements[i] = ReturnStatement(constructorCall).assignNoPrototype()
return statements
}
val localVar = LocalVariable(tempValIdentifier(), Annotations.Empty, Modifiers.Empty, null, constructorCall, true).assignNoPrototype()
statements[i] = DeclarationStatement(listOf(localVar)).assignNoPrototype()
statements.add(ReturnStatement(tempValIdentifier()).assignNoPrototype())
return statements
}
}
}
return null
}
private fun insertCallToArtificialPrimary(body: Block, propertiesToInitialize: Collection<Property>): List<Statement> {
val initializers = HashMap<Property, Expression?>()
for (property in propertiesToInitialize) {
initializers.put(property, getDefaultInitializer(property))
}
val statements = ArrayList<Statement>()
for (statement in body.statements) {
var keepStatement = true
if (statement is AssignmentExpression) {
val assignee = statement.left
if (assignee is QualifiedExpression && (assignee.qualifier as? Identifier)?.name == tempValName) {
val name = (assignee.identifier as Identifier).name
for (property in propertiesToInitialize) {
if (name == property.identifier.name) {
initializers.put(property, statement.right)
keepStatement = false
}
}
}
}
if (keepStatement) {
statements.add(statement)
}
}
val arguments = propertiesToInitialize.map { initializers[it] ?: LiteralExpression("null").assignNoPrototype() }
val initializer = MethodCallExpression.buildNotNull(null, className, arguments).assignNoPrototype()
if (statements.isNotEmpty()) {
val localVar = LocalVariable(tempValIdentifier(),
Annotations.Empty,
Modifiers.Empty,
null,
initializer,
true).assignNoPrototype()
statements.add(0, DeclarationStatement(listOf(localVar)).assignNoPrototype())
statements.add(ReturnStatement(tempValIdentifier()).assignNoPrototype())
}
else {
statements.add(ReturnStatement(initializer).assignNoPrototype())
}
return statements
}
private fun correctFactoryFunctionAccess(modifiers: Modifiers): Modifiers {
val classAccess = converter.convertModifiers(psiClass).accessModifier()
return when(modifiers.accessModifier()) {
Modifier.PUBLIC -> modifiers.without(Modifier.PUBLIC).with(classAccess)
Modifier.PROTECTED -> modifiers.without(Modifier.PROTECTED).with(classAccess)
Modifier.PRIVATE -> modifiers
else/*internal*/ -> if (classAccess != Modifier.PUBLIC) modifiers.with(classAccess) else modifiers
}
}
private fun PsiMethodCallExpression.isSuperConstructorCall(): Boolean {
val ref = getMethodExpression()
return ref.getCanonicalText() == "super" && ref.resolve()?.isConstructor() ?: false
}
private inner open class ReplacingExpressionConverter(val parameterUsageReplacementMap: Map<String, String>) : SpecialExpressionConverter { private inner open class ReplacingExpressionConverter(val parameterUsageReplacementMap: Map<String, String>) : SpecialExpressionConverter {
override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? { override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? {
@@ -209,11 +209,14 @@ class Converter private(
annotationConverter.convertAnnotationMethodDefault(method)).assignPrototype(method, noBlankLinesInheritance) annotationConverter.convertAnnotationMethodDefault(method)).assignPrototype(method, noBlankLinesInheritance)
} }
val parameterList = ParameterList(parameters).assignNoPrototype() val parameterList = ParameterList(parameters).assignNoPrototype()
val constructorSignature = PrimaryConstructorSignature(Annotations.Empty, Modifiers.Empty, parameterList).assignNoPrototype() val constructorSignature = if (parameterList.parameters.isNotEmpty())
PrimaryConstructorSignature(Annotations.Empty, Modifiers.Empty, parameterList).assignNoPrototype()
else
null
// to convert fields and nested types - they are not allowed in Kotlin but we convert them and let user refactor code // to convert fields and nested types - they are not allowed in Kotlin but we convert them and let user refactor code
var classBody = ClassBodyConverter(psiClass, this, false).convertBody() var classBody = ClassBodyConverter(psiClass, this, false).convertBody()
classBody = ClassBody(constructorSignature, classBody.baseClassParams, classBody.members, classBody.defaultObjectMembers, listOf(), classBody.lBrace, classBody.rBrace) classBody = ClassBody(constructorSignature, classBody.baseClassParams, classBody.members, classBody.defaultObjectMembers, classBody.lBrace, classBody.rBrace)
val annotationAnnotation = Annotation(Identifier("annotation").assignNoPrototype(), listOf(), false, false).assignNoPrototype() val annotationAnnotation = Annotation(Identifier("annotation").assignNoPrototype(), listOf(), false, false).assignNoPrototype()
return Class(psiClass.declarationIdentifier(), return Class(psiClass.declarationIdentifier(),
@@ -346,6 +346,16 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
} }
override fun visitReferenceExpression(expression: PsiReferenceExpression) { override fun visitReferenceExpression(expression: PsiReferenceExpression) {
// to avoid quoting of 'this' and 'super' in calls to this/super class constructors
if (expression.getText() == "this") {
result = ThisExpression(Identifier.Empty)
return
}
if (expression.getText() == "super") {
result = SuperExpression(Identifier.Empty)
return
}
val referenceName = expression.getReferenceName()!! val referenceName = expression.getReferenceName()!!
val target = expression.getReference()?.resolve() val target = expression.getReference()?.resolve()
val isNullable = target is PsiVariable && typeConverter.variableNullability(target).isNullable(codeConverter.settings) val isNullable = target is PsiVariable && typeConverter.variableNullability(target).isNullable(codeConverter.settings)
@@ -30,8 +30,6 @@ open class Class(
) : Member(annotations, modifiers) { ) : Member(annotations, modifiers) {
override fun generateCode(builder: CodeBuilder) { override fun generateCode(builder: CodeBuilder) {
builder.append(body.factoryFunctions, "\n", "", "\n\n")
builder.append(annotations) builder.append(annotations)
.appendWithSpaceAfter(presentationModifiers()) .appendWithSpaceAfter(presentationModifiers())
.append(keyword) .append(keyword)
@@ -25,7 +25,6 @@ class ClassBody (
val baseClassParams: List<DeferredElement<Expression>>, val baseClassParams: List<DeferredElement<Expression>>,
val members: List<Member>, val members: List<Member>,
val defaultObjectMembers: List<Member>, val defaultObjectMembers: List<Member>,
val factoryFunctions: List<FactoryFunction>,
val lBrace: LBrace, val lBrace: LBrace,
val rBrace: RBrace) { val rBrace: RBrace) {
@@ -44,28 +44,45 @@ class PrimaryConstructor(
} }
} }
class PrimaryConstructorSignature(val annotations: Annotations, val modifiers: Modifiers, val parameterList: ParameterList) : Element() { class PrimaryConstructorSignature(val annotations: Annotations, private val modifiers: Modifiers, val parameterList: ParameterList) : Element() {
override fun generateCode(builder: CodeBuilder) { public val accessModifier: Modifier? = run {
val accessModifier = modifiers.filter { it in ACCESS_MODIFIERS && it != Modifier.PUBLIC } val modifier = modifiers.accessModifier()
if (accessModifier.isEmpty && annotations.isEmpty && parameterList.parameters.isEmpty()) return if (modifier != Modifier.PUBLIC) modifier else null
}
override fun generateCode(builder: CodeBuilder) {
if (!annotations.isEmpty) { if (!annotations.isEmpty) {
builder append " " append annotations.withBrackets() builder append " " append annotations.withBrackets()
} }
if (!accessModifier.isEmpty) { if (accessModifier != null) {
builder append " " append accessModifier builder append " " append Modifiers(listOf(accessModifier)).assignPrototypesFrom(modifiers)
} }
builder append "(" append parameterList append ")" builder append "(" append parameterList append ")"
} }
} }
class FactoryFunction(name: Identifier, class SecondaryConstructor(
annotations: Annotations, annotations: Annotations,
modifiers: Modifiers, modifiers: Modifiers,
returnType: Type, private val parameterList: ParameterList,
parameterList: ParameterList, private val body: DeferredElement<Block>,
typeParameterList: TypeParameterList, private val thisOrSuperCall: DeferredElement<Expression>?
body: DeferredElement<Block>) ) : Member(annotations, modifiers) {
: Function(name, annotations, modifiers, returnType, typeParameterList, parameterList, body, false)
override fun generateCode(builder: CodeBuilder) {
builder.append(annotations)
.appendWithSpaceAfter(modifiers)
.append("constructor(")
.append(parameterList)
.append(")")
if (thisOrSuperCall != null) {
builder append " : " append thisOrSuperCall
}
builder append " " append body
}
}
@@ -95,7 +95,7 @@ abstract class Element {
// this class should never be created directly - Converter.deferredElement() should be used! // this class should never be created directly - Converter.deferredElement() should be used!
class DeferredElement<TResult : Element>( class DeferredElement<TResult : Element>(
private var generator: (CodeConverter) -> TResult, private val generator: (CodeConverter) -> TResult,
public val converterState: Converter.PersonalState public val converterState: Converter.PersonalState
) : Element() { ) : Element() {
@@ -126,14 +126,5 @@ class DeferredElement<TResult : Element>(
assert(result != null) { "No code generated for deferred element $this. Possible reason is that it has been created directly instead of Converter.lazyElement() call." } assert(result != null) { "No code generated for deferred element $this. Possible reason is that it has been created directly instead of Converter.lazyElement() call." }
return result!! return result!!
} }
public fun updateGenerator(generatorUpdater: (codeConverter: CodeConverter, prevResult: TResult) -> TResult) {
assert(result == null, "Cannot update generator when code has been generated")
val prevGenerator = generator
generator = { codeConverter ->
val prevResult = prevGenerator(codeConverter)
generatorUpdater(codeConverter, prevResult)
}
}
} }
@@ -30,8 +30,6 @@ class Enum(
) : Class(name, annotations, modifiers, typeParameterList, extendsTypes, baseClassParams, implementsTypes, body) { ) : Class(name, annotations, modifiers, typeParameterList, extendsTypes, baseClassParams, implementsTypes, body) {
override fun generateCode(builder: CodeBuilder) { override fun generateCode(builder: CodeBuilder) {
builder.append(body.factoryFunctions, "\n", "", "\n\n")
builder append annotations appendWithSpaceAfter presentationModifiers() append "enum class " append name builder append annotations appendWithSpaceAfter presentationModifiers() append "enum class " append name
if (body.primaryConstructorSignature != null) { if (body.primaryConstructorSignature != null) {
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.j2k.ast
import org.jetbrains.kotlin.j2k.* import org.jetbrains.kotlin.j2k.*
open class Function( class Function(
val name: Identifier, val name: Identifier,
annotations: Annotations, annotations: Annotations,
modifiers: Modifiers, modifiers: Modifiers,
+5 -13
View File
@@ -1,24 +1,16 @@
// ERROR: 'internal fun Test(): demo.Test' is already defined in demo
// ERROR: 'public constructor Test()' is already defined in demo
// ERROR: Overload resolution ambiguity: internal fun Test(): demo.Test defined in demo public constructor Test() defined in demo.Test
// ERROR: Overload resolution ambiguity: internal fun Test(): demo.Test defined in demo public constructor Test() defined in demo.Test
// ERROR: Type inference failed: Not enough information to infer parameter K in constructor HashMap<K, V>(p0: kotlin.Int) Please specify it explicitly. // ERROR: Type inference failed: Not enough information to infer parameter K in constructor HashMap<K, V>(p0: kotlin.Int) Please specify it explicitly.
// ERROR: Type inference failed: Not enough information to infer parameter K in constructor HashMap<K, V>(p0: kotlin.Int) Please specify it explicitly. // ERROR: Type inference failed: Not enough information to infer parameter K in constructor HashMap<K, V>(p0: kotlin.Int) Please specify it explicitly.
// ERROR: Overload resolution ambiguity: internal fun Test(): demo.Test defined in demo public constructor Test() defined in demo.Test
package demo package demo
import java.util.HashMap import java.util.HashMap
fun Test(): Test { class Test {
return Test() constructor() {
}
constructor(s: String) {
}
} }
fun Test(s: String): Test {
return Test()
}
class Test
class User { class User {
fun main() { fun main() {
val m = HashMap(1) val m = HashMap(1)
@@ -1,8 +1,3 @@
// this is a secondary constructor 2
fun A(s: String): A {
return A(s.length())
} // end of secondary constructor 2 body
class A// this is a primary constructor class A// this is a primary constructor
(p: Int = 1) { (p: Int = 1) {
private val v: Int private val v: Int
@@ -10,6 +5,10 @@ class A// this is a primary constructor
{ {
v = 1 v = 1
} // end of primary constructor body } // end of primary constructor body
// this is a secondary constructor 2
constructor(s: String) : this(s.length()) {
} // end of secondary constructor 2 body
}// this is a secondary constructor 1 }// this is a secondary constructor 1
// end of secondary constructor 1 body // end of secondary constructor 1 body
@@ -1,21 +1,17 @@
fun C(arg1: Int, arg2: Int, arg3: Int): C {
val __ = C(arg1)
__.myArg2 = arg2
__.myArg3 = arg3
return __
}
fun C(arg1: Int, arg2: Int): C {
val __ = C(arg1)
__.myArg2 = arg2
__.myArg3 = 0
return __
}
class C(val myArg1: Int) { class C(val myArg1: Int) {
var myArg2: Int = 0 var myArg2: Int = 0
var myArg3: Int = 0 var myArg3: Int = 0
constructor(arg1: Int, arg2: Int, arg3: Int) : this(arg1) {
myArg2 = arg2
myArg3 = arg3
}
constructor(arg1: Int, arg2: Int) : this(arg1) {
myArg2 = arg2
myArg3 = 0
}
{ {
myArg2 = 0 myArg2 = 0
myArg3 = 0 myArg3 = 0
@@ -1,18 +1,14 @@
// ERROR: 'public fun Test(s: kotlin.String): Test' is already defined in root package // ERROR: Property must be initialized or be abstract
// ERROR: 'public constructor Test(s: kotlin.String)' is already defined in root package public class Test {
// ERROR: None of the following functions can be called with the arguments supplied: public fun Test(): Test defined in root package public fun Test(s: kotlin.String): Test defined in root package public constructor Test(s: kotlin.String) defined in Test private val s: String
// ERROR: Overload resolution ambiguity: public fun Test(s: kotlin.String): Test defined in root package public constructor Test(s: kotlin.String) defined in Test
public fun Test(): Test {
val __ = Test(null)
__.b = true
return __
}
public fun Test(s: String): Test {
return Test(s)
}
public class Test(private val s: String) {
var b: Boolean = false var b: Boolean = false
var d: Double = 0.toDouble() var d: Double = 0.toDouble()
public constructor() {
b = true
}
public constructor(s: String) {
this.s = s
}
} }
@@ -1,16 +1,13 @@
fun C(arg1: Int, arg2: Int): C { class C(arg1: Int, arg2: Int, arg3: Int) {
val __ = C(arg1, arg2, 0)
System.out.println()
return __
}
fun C(arg1: Int): C { constructor(arg1: Int, arg2: Int) : this(arg1, arg2, 0) {
val __ = C(arg1, 0) System.out.println()
System.out.println() }
return __
}
class C(arg1: Int, arg2: Int, arg3: Int) constructor(arg1: Int) : this(arg1, 0) {
System.out.println()
}
}
public class User { public class User {
default object { default object {
@@ -1,14 +1,13 @@
import javaApi.Anon5 import javaApi.Anon5
deprecated("") // this constructor will not be replaced by default parameter value in primary because of this annotation
fun A(a: Int): A {
return A(a, 1)
}
class A class A
[Anon5(10)] [Anon5(10)]
(private val a: Int, private val b: Int) (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)]
() ()
@@ -1,34 +1,30 @@
// ERROR: 'public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T>' is already defined in root package // ERROR: Property must be initialized or be abstract
// ERROR: 'public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean)' is already defined in root package public class Identifier<T> {
// ERROR: Cannot choose among the following candidates without completing type inference: public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T> defined in root package public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean) defined in Identifier public val name: T
// ERROR: Cannot choose among the following candidates without completing type inference: public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T> defined in root package public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean) defined in Identifier private val myHasDollar: Boolean
// ERROR: Cannot choose among the following candidates without completing type inference: public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T> defined in root package public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean) defined in Identifier
// ERROR: Overload resolution ambiguity: public fun <T> Identifier(name: kotlin.String, isNullable: kotlin.Boolean): Identifier<kotlin.String> defined in root package public constructor Identifier<T>(name: kotlin.String, myHasDollar: kotlin.Boolean) defined in Identifier
public fun <T> Identifier(name: T): Identifier<T> {
return Identifier(name, false)
}
public fun <T> Identifier(name: T, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, false)
__.myNullable = isNullable
return __
}
public fun <T> Identifier(name: T, hasDollar: Boolean, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, hasDollar)
__.myNullable = isNullable
return __
}
public class Identifier<T>(public val name: T, private val myHasDollar: Boolean) {
private var myNullable = true private var myNullable = true
public constructor(name: T) {
this.name = name
}
public constructor(name: T, isNullable: Boolean) {
this.name = name
myNullable = isNullable
}
public constructor(name: T, hasDollar: Boolean, isNullable: Boolean) {
this.name = name
myHasDollar = hasDollar
myNullable = isNullable
}
} }
public class User { public class User {
default object { default object {
public fun main() { public fun main() {
val i1 = Identifier("name", false, true) val i1 = Identifier("name", false, true)
val i2 = Identifier<String>("name", false) val i2 = Identifier("name", false)
val i3 = Identifier("name") val i3 = Identifier("name")
} }
} }
@@ -1,27 +1,23 @@
// ERROR: 'public fun Identifier(name: kotlin.String, isNullable: kotlin.Boolean): Identifier' is already defined in root package // ERROR: Property must be initialized or be abstract
// ERROR: 'public constructor Identifier(name: kotlin.String, myHasDollar: kotlin.Boolean)' is already defined in root package public class Identifier {
// ERROR: Overload resolution ambiguity: public fun Identifier(name: kotlin.String, isNullable: kotlin.Boolean): Identifier defined in root package public constructor Identifier(name: kotlin.String, myHasDollar: kotlin.Boolean) defined in Identifier public val name: String
// ERROR: Overload resolution ambiguity: public fun Identifier(name: kotlin.String, isNullable: kotlin.Boolean): Identifier defined in root package public constructor Identifier(name: kotlin.String, myHasDollar: kotlin.Boolean) defined in Identifier private val myHasDollar: Boolean
// ERROR: Overload resolution ambiguity: public fun Identifier(name: kotlin.String, isNullable: kotlin.Boolean): Identifier defined in root package public constructor Identifier(name: kotlin.String, myHasDollar: kotlin.Boolean) defined in Identifier
// ERROR: Overload resolution ambiguity: public fun Identifier(name: kotlin.String, isNullable: kotlin.Boolean): Identifier defined in root package public constructor Identifier(name: kotlin.String, myHasDollar: kotlin.Boolean) defined in Identifier
public fun Identifier(name: String): Identifier {
return Identifier(name, false)
}
public fun Identifier(name: String, isNullable: Boolean): Identifier {
val __ = Identifier(name, false)
__.myNullable = isNullable
return __
}
public fun Identifier(name: String, hasDollar: Boolean, isNullable: Boolean): Identifier {
val __ = Identifier(name, hasDollar)
__.myNullable = isNullable
return __
}
public class Identifier(public val name: String, private val myHasDollar: Boolean) {
private var myNullable = true private var myNullable = true
public constructor(name: String) {
this.name = name
}
public constructor(name: String, isNullable: Boolean) {
this.name = name
myNullable = isNullable
}
public constructor(name: String, hasDollar: Boolean, isNullable: Boolean) {
this.name = name
myHasDollar = hasDollar
myNullable = isNullable
}
} }
public class User { public class User {
@@ -1,17 +1,13 @@
// ERROR: Cannot access 'staticFoo': it is 'private' in 'Default'
// ERROR: Cannot access 'staticFoo': it is 'private' in 'Default'
fun C(arg1: Int, arg2: Int, other: C): C {
val __ = C(arg1, arg2, 0)
System.out.println(__.foo(1) + __.foo(2) + other.foo(3) + C.staticFoo(4) + C.staticFoo(5))
return __
}
class C(private val arg1: Int, private val arg2: Int, private val arg3: Int) { class C(private val arg1: Int, private val arg2: Int, private val arg3: Int) {
fun foo(p: Int): Int { fun foo(p: Int): Int {
return p return p
} }
constructor(arg1: Int, arg2: Int, other: C) : this(arg1, arg2, 0) {
System.out.println(foo(1) + this.foo(2) + other.foo(3) + staticFoo(4) + C.staticFoo(5))
}
default object { default object {
private fun staticFoo(p: Int): Int { private fun staticFoo(p: Int): Int {
return p return p
@@ -1,21 +1,16 @@
fun C(arg1: Int, arg2: Int, arg3: Int): C { class C {
return C() constructor(arg1: Int, arg2: Int, arg3: Int) {
} }
fun C(arg1: Int, arg2: Int): C { constructor(arg1: Int, arg2: Int) : this(arg1, arg2, 0) {
val __ = C(arg1, arg2, 0) System.out.println()
System.out.println() }
return __
}
fun C(arg: Int): C { constructor(arg: Int) {
val __ = C() System.out.println(arg)
System.out.println(arg) }
return __
} }
class C
public class User { public class User {
default object { default object {
public fun main() { public fun main() {
@@ -1,8 +1,7 @@
package pack package pack
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
fun C(a: Int): C { constructor(a: Int) : this(a, 0, 0, 0, 1) {
return C(a, 0, 0, 0, 1) }
} }
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0)
@@ -1,12 +1,10 @@
package pack package pack
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
fun C(a1: Int, b1: Int, c1: Int): C { constructor(a1: Int, b1: Int, c1: Int) : this(a1, b1, c1, 0, 0) {
return C(a1, b1, c1, 0, 0) }
constructor(b: Byte) : this(b.toInt(), 0, 0, 0, 0) {
}
} }
fun C(b: Byte): C {
return C(b.toInt(), 0, 0, 0, 0)
}
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0)
@@ -1,8 +1,7 @@
package pack package pack
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0) {
fun C(a: Int, b: Int, c: Int): C { constructor(a: Int, b: Int, c: Int) : this(b, a, c, 0, 0) {
return C(b, a, c, 0, 0) }
} }
class C(a: Int = 0, b: Int = 0, c: Int = 0, d: Int = 0, e: Int = 0)
@@ -1,11 +1,4 @@
// ERROR: Overload resolution ambiguity: internal fun C(arg1: kotlin.Int, arg2: kotlin.Int): C defined in root package 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 public constructor C(arg1: kotlin.Int, arg2: kotlin.Int = ..., arg3: kotlin.Int = ...) defined in C
fun C(arg1: Int, arg2: Int): C {
var arg2 = arg2
val __ = C(arg1, arg2, 0)
arg2++
return __
}
class C(arg1: Int, arg2: Int = 0, arg3: Int = 0) { class C(arg1: Int, arg2: Int = 0, arg3: Int = 0) {
private val field: Int private val field: Int
@@ -17,6 +10,11 @@ class C(arg1: Int, arg2: Int = 0, arg3: Int = 0) {
field = arg3 field = arg3
arg3++ arg3++
} }
constructor(arg1: Int, arg2: Int) : this(arg1, arg2, 0) {
var arg2 = arg2
arg2++
}
} }
public class User { public class User {
@@ -1,7 +1,5 @@
// ERROR: Too many arguments for internal fun C(arg1: kotlin.Int): C defined in root package class C private(arg1: Int, arg2: Int, arg3: Int = 0) {
// ERROR: Too many arguments for internal fun C(arg1: kotlin.Int): C defined in root package
fun C(arg1: Int): C {
return C(arg1, 0, 0)
}
class C private(arg1: Int, arg2: Int, arg3: Int = 0) public constructor(arg1: Int) : this(arg1, 0, 0) {
}
}
@@ -1,12 +1,9 @@
// ERROR: Cannot access 'arg1': it is 'private' in 'C' class C(private val arg1: Int, private val arg2: Int, private val arg3: Int) {
// ERROR: Cannot access 'arg2': it is 'private' in 'C'
fun C(arg1: Int, arg2: Int, other: C): C {
val __ = C(arg1, arg2, 0)
System.out.println(__.arg1 + other.arg2)
return __
}
class C(private val arg1: Int, private val arg2: Int, private val arg3: Int) constructor(arg1: Int, arg2: Int, other: C) : this(arg1, arg2, 0) {
System.out.println(this.arg1 + other.arg2)
}
}
class User { class User {
fun foo() { fun foo() {
@@ -1,64 +1,53 @@
class Outer { class Outer {
private inner class Inner1() {
private fun Inner1(a: Int): Inner1 { public constructor(a: Int) : this() {
return Inner1() }
protected constructor(c: Char) : this() {
}
private constructor(b: Boolean) : this() {
}
} }
private fun Inner1(c: Char): Inner1 { protected inner class Inner2() {
return Inner1()
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
private constructor(b: Boolean) : this() {
}
} }
private fun Inner1(b: Boolean): Inner1 { inner class Inner3() {
return Inner1()
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
private constructor(b: Boolean) : this() {
}
} }
private inner class Inner1 public inner class Inner4() {
public constructor(a: Int) : this() {
}
protected fun Inner2(a: Int): Inner2 { protected constructor(c: Char) : this() {
return Inner2() }
private constructor(b: Boolean) : this() {
}
} }
protected fun Inner2(c: Char): Inner2 {
return Inner2()
}
private fun Inner2(b: Boolean): Inner2 {
return Inner2()
}
protected inner class Inner2
fun Inner3(a: Int): Inner3 {
return Inner3()
}
fun Inner3(c: Char): Inner3 {
return Inner3()
}
private fun Inner3(b: Boolean): Inner3 {
return Inner3()
}
inner class Inner3
public fun Inner4(a: Int): Inner4 {
return Inner4()
}
public fun Inner4(c: Char): Inner4 {
return Inner4()
}
private fun Inner4(b: Boolean): Inner4 {
return Inner4()
}
public inner class Inner4
fun foo() { fun foo() {
val inner1 = Inner1(1) val inner1 = Inner1(1)
val inner2 = Inner2(2) val inner2 = Inner2(2)
@@ -1,65 +1,55 @@
class Outer { class Outer {
private class Nested1() {
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
private constructor(b: Boolean) : this() {
}
}
protected class Nested2() {
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
private constructor(b: Boolean) : this() {
}
}
class Nested3() {
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
private constructor(b: Boolean) : this() {
}
}
public class Nested4() {
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
private constructor(b: Boolean) : this() {
}
}
default object { default object {
private fun Nested1(a: Int): Nested1 {
return Nested1()
}
private fun Nested1(c: Char): Nested1 {
return Nested1()
}
private fun Nested1(b: Boolean): Nested1 {
return Nested1()
}
private class Nested1
protected fun Nested2(a: Int): Nested2 {
return Nested2()
}
protected fun Nested2(c: Char): Nested2 {
return Nested2()
}
private fun Nested2(b: Boolean): Nested2 {
return Nested2()
}
protected class Nested2
fun Nested3(a: Int): Nested3 {
return Nested3()
}
fun Nested3(c: Char): Nested3 {
return Nested3()
}
private fun Nested3(b: Boolean): Nested3 {
return Nested3()
}
class Nested3
public fun Nested4(a: Int): Nested4 {
return Nested4()
}
public fun Nested4(c: Char): Nested4 {
return Nested4()
}
private fun Nested4(b: Boolean): Nested4 {
return Nested4()
}
public class Nested4
fun foo() { fun foo() {
val nested1 = Nested1(1) val nested1 = Nested1(1)
val nested2 = Nested2(2) val nested2 = Nested2(2)
@@ -1,36 +1,29 @@
fun A(a: Int): A { class A() {
return A()
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
constructor(f: Float) : this() {
}
private constructor(d: Double) : this() {
}
} }
fun A(c: Char): A { public class B() {
return A()
public constructor(a: Int) : this() {
}
protected constructor(c: Char) : this() {
}
constructor(f: Float) : this() {
}
private constructor(d: Double) : this() {
}
} }
fun A(f: Float): A {
return A()
}
private fun A(d: Double): A {
return A()
}
class A
public fun B(a: Int): B {
return B()
}
public fun B(c: Char): B {
return B()
}
fun B(f: Float): B {
return B()
}
private fun B(d: Double): B {
return B()
}
public class B
@@ -1,12 +1,9 @@
// ERROR: Cannot access 'staticField1': it is 'private' in 'Default' class C() {
// ERROR: Cannot access 'staticField2': it is 'private' in 'Default'
fun C(p: Int): C { constructor(p: Int) : this() {
val __ = C() System.out.println(staticField1 + C.staticField2)
System.out.println(C.staticField1 + C.staticField2) }
return __
}
class C {
default object { default object {
private val staticField1 = 0 private val staticField1 = 0
private val staticField2 = 0 private val staticField2 = 0
@@ -1,17 +1,6 @@
// ERROR: 'public fun Test(name: kotlin.String): Test' is already defined in root package // ERROR: Property must be initialized or be abstract
// ERROR: 'public constructor Test(myName: kotlin.String)' is already defined in root package public class Test {
// ERROR: None of the following functions can be called with the arguments supplied: public fun Test(): Test defined in root package public fun Test(name: kotlin.String): Test defined in root package public constructor Test(myName: kotlin.String) defined in Test private val myName: String
// ERROR: Overload resolution ambiguity: public fun Test(name: kotlin.String): Test defined in root package public constructor Test(myName: kotlin.String) defined in Test
// ERROR: Overload resolution ambiguity: public fun Test(name: kotlin.String): Test defined in root package public constructor Test(myName: kotlin.String) defined in Test
public fun Test(): Test {
return Test(null)
}
public fun Test(name: String): Test {
return Test(Test.foo(name))
}
public class Test(private val myName: String) {
var a: Boolean = false var a: Boolean = false
var b: Double = 0.toDouble() var b: Double = 0.toDouble()
var c: Float = 0.toFloat() var c: Float = 0.toFloat()
@@ -20,6 +9,13 @@ public class Test(private val myName: String) {
protected var f: Short = 0 protected var f: Short = 0
protected var g: Char = ' ' protected var g: Char = ' '
public constructor() {
}
public constructor(name: String) {
myName = foo(name)
}
default object { default object {
fun foo(n: String): String { fun foo(n: String): String {
@@ -1,21 +1,17 @@
fun C(arg1: Int, arg2: Int, arg3: Int): C {
val __ = C(arg1)
__.arg2 = arg2
__.arg3 = arg3
return __
}
fun C(arg1: Int, arg2: Int): C {
val __ = C(arg1)
__.arg2 = arg2
__.arg3 = 0
return __
}
class C(val arg1: Int) { class C(val arg1: Int) {
var arg2: Int = 0 var arg2: Int = 0
var arg3: Int = 0 var arg3: Int = 0
constructor(arg1: Int, arg2: Int, arg3: Int) : this(arg1) {
this.arg2 = arg2
this.arg3 = arg3
}
constructor(arg1: Int, arg2: Int) : this(arg1) {
this.arg2 = arg2
arg3 = 0
}
{ {
arg2 = 0 arg2 = 0
arg3 = 0 arg3 = 0
+20 -24
View File
@@ -1,34 +1,30 @@
// ERROR: 'public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T>' is already defined in root package // ERROR: Property must be initialized or be abstract
// ERROR: 'public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean)' is already defined in root package public class Identifier<T> {
// ERROR: Cannot choose among the following candidates without completing type inference: public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T> defined in root package public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean) defined in Identifier public val name: T
// ERROR: Cannot choose among the following candidates without completing type inference: public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T> defined in root package public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean) defined in Identifier private val myHasDollar: Boolean
// ERROR: Cannot choose among the following candidates without completing type inference: public fun <T> Identifier(name: T, isNullable: kotlin.Boolean): Identifier<T> defined in root package public constructor Identifier<T>(name: T, myHasDollar: kotlin.Boolean) defined in Identifier
// ERROR: Overload resolution ambiguity: public fun <T> Identifier(name: kotlin.String, isNullable: kotlin.Boolean): Identifier<kotlin.String> defined in root package public constructor Identifier<T>(name: kotlin.String, myHasDollar: kotlin.Boolean) defined in Identifier
public fun <T> Identifier(name: T): Identifier<T> {
return Identifier(name, false)
}
public fun <T> Identifier(name: T, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, false)
__.myNullable = isNullable
return __
}
public fun <T> Identifier(name: T, hasDollar: Boolean, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, hasDollar)
__.myNullable = isNullable
return __
}
public class Identifier<T>(public val name: T, private val myHasDollar: Boolean) {
private var myNullable = true private var myNullable = true
public constructor(name: T) {
this.name = name
}
public constructor(name: T, isNullable: Boolean) {
this.name = name
myNullable = isNullable
}
public constructor(name: T, hasDollar: Boolean, isNullable: Boolean) {
this.name = name
myHasDollar = hasDollar
myNullable = isNullable
}
} }
public class User { public class User {
default object { default object {
public fun main(args: Array<String>) { public fun main(args: Array<String>) {
val i1 = Identifier("name", false, true) val i1 = Identifier("name", false, true)
val i2 = Identifier<String>("name", false) val i2 = Identifier("name", false)
val i3 = Identifier("name") val i3 = Identifier("name")
} }
} }