Java to Kotlin converter: secondary constructors are converted to top-level functions instead of "create" functions in class object

This commit is contained in:
Valentin Kipyatkov
2014-06-26 20:01:28 +04:00
parent f698ca69d7
commit 66005a3469
25 changed files with 225 additions and 256 deletions
@@ -43,10 +43,10 @@ class ConstructorConverter(private val converter: Converter) {
val member = expression.getReference()?.resolve() as? PsiMember val member = expression.getReference()?.resolve() as? PsiMember
if (member != null && if (member != null &&
!member.isConstructor() && !member.isConstructor() &&
member.getContainingClass() == constructor.getContainingClass() && member.getContainingClass() == constructor.getContainingClass()) {
!member.hasModifierProperty(PsiModifier.STATIC)) {
val isNullable = member is PsiField && typeConverter.variableNullability(member).isNullable(converter.settings) val isNullable = member is PsiField && typeConverter.variableNullability(member).isNullable(converter.settings)
result = QualifiedExpression(tempValIdentifier(), Identifier(expression.getReferenceName()!!, isNullable).assignNoPrototype()) val qualifier = if (member.hasModifierProperty(PsiModifier.STATIC)) constructor.declarationIdentifier() else tempValIdentifier()
result = QualifiedExpression(qualifier, Identifier(expression.getReferenceName()!!, isNullable).assignNoPrototype())
return return
} }
} }
@@ -61,7 +61,7 @@ class ConstructorConverter(private val converter: Converter) {
typeParameterList.parameters, typeParameterList.parameters,
Nullability.NotNull, Nullability.NotNull,
converter.settings).assignNoPrototype() converter.settings).assignNoPrototype()
return FactoryFunction(annotations, modifiers, factoryFunctionType, params, typeParameterList, body) return FactoryFunction(constructor.declarationIdentifier(), annotations, modifiers, factoryFunctionType, params, typeParameterList, body)
} }
} }
@@ -176,12 +176,13 @@ class ConstructorConverter(private val converter: Converter) {
assert(classBody.primaryConstructorSignature == null) assert(classBody.primaryConstructorSignature == null)
val fieldsToInitialize = classBody.members.filterIsInstance(javaClass<Field>()).filter { it.isVal } val fieldsToInitialize = classBody.members.filterIsInstance(javaClass<Field>()).filter { it.isVal }
for (factoryFunction in classBody.factoryFunctions()) { for (factoryFunction in classBody.factoryFunctions) {
val body = factoryFunction.body!! val body = factoryFunction.body!!
// 2 cases: secondary constructor either calls another constructor or does not call any // 2 cases: secondary constructor either calls another constructor or does not call any
val newStatements = replaceConstructorCallInFactoryFunction(body, className) ?: val newStatements = replaceConstructorCallInFactoryFunction(body, className) ?:
insertCallToArtificialPrimary(body, className, fieldsToInitialize) insertCallToArtificialPrimary(body, className, fieldsToInitialize)
factoryFunction.body = Block(newStatements, LBrace().assignNoPrototype(), RBrace().assignNoPrototype()).assignNoPrototype() factoryFunction.body = Block(newStatements, LBrace().assignNoPrototype(), RBrace().assignNoPrototype()).assignNoPrototype()
//TODO: no assignment here
} }
val parameters = fieldsToInitialize.map { field -> val parameters = fieldsToInitialize.map { field ->
@@ -189,15 +190,17 @@ class ConstructorConverter(private val converter: Converter) {
Parameter(field.identifier, field.`type`, varValModifier, field.annotations, field.modifiers.filter { it in ACCESS_MODIFIERS }).assignPrototypesFrom(field) Parameter(field.identifier, field.`type`, varValModifier, field.annotations, field.modifiers.filter { it in ACCESS_MODIFIERS }).assignPrototypesFrom(field)
} }
val modifiers = Modifiers(listOf(Modifier.PRIVATE)).assignNoPrototype() 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 parameterList = ParameterList(parameters).assignNoPrototype()
val constructorSignature = PrimaryConstructorSignature(modifiers, parameterList).assignNoPrototype() val constructorSignature = PrimaryConstructorSignature(modifiers, parameterList).assignNoPrototype()
val updatedMembers = classBody.members.filter { !fieldsToInitialize.contains(it) } val updatedMembers = classBody.members.filter { !fieldsToInitialize.contains(it) }
return ClassBody(constructorSignature, updatedMembers, classBody.classObjectMembers, classBody.lBrace, classBody.rBrace) return ClassBody(constructorSignature, updatedMembers, classBody.classObjectMembers, classBody.factoryFunctions, classBody.lBrace, classBody.rBrace)
} }
private fun replaceConstructorCallsInFactoryFunctions(classBody: ClassBody, className: String) { private fun replaceConstructorCallsInFactoryFunctions(classBody: ClassBody, className: String) {
for (factoryFunction in classBody.factoryFunctions()) { for (factoryFunction in classBody.factoryFunctions) {
val body = factoryFunction.body!! val body = factoryFunction.body!!
val statements = replaceConstructorCallInFactoryFunction(body, className) val statements = replaceConstructorCallInFactoryFunction(body, className)
if (statements != null) { if (statements != null) {
@@ -278,8 +281,6 @@ class ConstructorConverter(private val converter: Converter) {
return statements return statements
} }
private fun ClassBody.factoryFunctions() = classObjectMembers.filterIsInstance(javaClass<FactoryFunction>())
private val tempValName: String = "__" private val tempValName: String = "__"
private fun tempValIdentifier(): Identifier = Identifier(tempValName, false).assignNoPrototype() private fun tempValIdentifier(): Identifier = Identifier(tempValName, false).assignNoPrototype()
} }
+6 -4
View File
@@ -125,6 +125,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
val members = ArrayList<Member>() val members = ArrayList<Member>()
val classObjectMembers = ArrayList<Member>() val classObjectMembers = 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) {
@@ -135,8 +136,10 @@ public class Converter private(val project: Project, val settings: ConverterSett
members.add(initializer) members.add(initializer)
} }
} }
else if (useClassObject && psiMember !is PsiClass && psiMember.hasModifierProperty(PsiModifier.STATIC) || else if (member is FactoryFunction) {
member is FactoryFunction) { factoryFunctions.add(member)
}
else if (useClassObject && psiMember !is PsiClass && psiMember.hasModifierProperty(PsiModifier.STATIC)) {
classObjectMembers.add(member) classObjectMembers.add(member)
} }
else { else {
@@ -146,13 +149,12 @@ public class Converter private(val project: Project, val settings: ConverterSett
val lBrace = LBrace().assignPrototype(psiClass.getLBrace()) val lBrace = LBrace().assignPrototype(psiClass.getLBrace())
val rBrace = RBrace().assignPrototype(psiClass.getRBrace()) val rBrace = RBrace().assignPrototype(psiClass.getRBrace())
return ClassBody(primaryConstructorSignature, members, classObjectMembers, lBrace, rBrace) return ClassBody(primaryConstructorSignature, members, classObjectMembers, factoryFunctions, lBrace, rBrace)
} }
// do not convert private static methods into class object if possible // do not convert private static methods into class object if possible
private fun shouldGenerateClassObject(psiClass: PsiClass, convertedMembers: Map<PsiMember, Member>): Boolean { private fun shouldGenerateClassObject(psiClass: PsiClass, convertedMembers: Map<PsiMember, Member>): Boolean {
if (psiClass.isEnum()) return false if (psiClass.isEnum()) return false
if (convertedMembers.values().any { it is FactoryFunction }) return true
val members = convertedMembers.keySet().filter { !it.isConstructor() } val members = convertedMembers.keySet().filter { !it.isConstructor() }
val classObjectMembers = members.filter { it !is PsiClass && it.hasModifierProperty(PsiModifier.STATIC) } val classObjectMembers = members.filter { it !is PsiClass && it.hasModifierProperty(PsiModifier.STATIC) }
val nestedClasses = members.filterIsInstance(javaClass<PsiClass>()).filter { it.hasModifierProperty(PsiModifier.STATIC) } val nestedClasses = members.filterIsInstance(javaClass<PsiClass>()).filter { it.hasModifierProperty(PsiModifier.STATIC) }
@@ -30,17 +30,22 @@ 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)
.append(" ") .append(" ")
.append(name) .append(name)
.append(typeParameterList) .append(typeParameterList)
if (body.primaryConstructorSignature != null) { if (body.primaryConstructorSignature != null) {
builder.append(body.primaryConstructorSignature) builder.append(body.primaryConstructorSignature)
} }
appendBaseTypes(builder) appendBaseTypes(builder)
typeParameterList.appendWhere(builder) typeParameterList.appendWhere(builder)
body.append(builder) body.append(builder)
} }
@@ -25,6 +25,7 @@ class ClassBody (
val primaryConstructorSignature: PrimaryConstructorSignature?, val primaryConstructorSignature: PrimaryConstructorSignature?,
val members: List<Member>, val members: List<Member>,
val classObjectMembers: List<Member>, val classObjectMembers: List<Member>,
val factoryFunctions: List<FactoryFunction>,
val lBrace: LBrace, val lBrace: LBrace,
val rBrace: RBrace) { val rBrace: RBrace) {
@@ -52,10 +52,11 @@ class PrimaryConstructorSignature(val modifiers: Modifiers, val parameterList: P
} }
} }
class FactoryFunction(annotations: Annotations, class FactoryFunction(name: Identifier,
annotations: Annotations,
modifiers: Modifiers, modifiers: Modifiers,
returnType: Type, returnType: Type,
parameterList: ParameterList, parameterList: ParameterList,
typeParameterList: TypeParameterList, typeParameterList: TypeParameterList,
block: Block) block: Block)
: Function(Identifier("create").assignNoPrototype(), annotations, modifiers, returnType, typeParameterList, parameterList, block, false) : Function(name, annotations, modifiers, returnType, typeParameterList, parameterList, block, false)
@@ -31,12 +31,18 @@ class Enum(
extendsTypes, baseClassParams, implementsTypes, body) { 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) {
builder.append(body.primaryConstructorSignature) builder.append(body.primaryConstructorSignature)
} }
builder append typeParameterList builder append typeParameterList
appendBaseTypes(builder) appendBaseTypes(builder)
body.append(builder) body.append(builder)
} }
} }
@@ -264,18 +264,6 @@ open class ExpressionVisitor(private val converter: Converter,
private fun createNewClassExpression(expression: PsiNewExpression): Expression { private fun createNewClassExpression(expression: PsiNewExpression): Expression {
val anonymousClass = expression.getAnonymousClass() val anonymousClass = expression.getAnonymousClass()
val classReference = expression.getClassOrAnonymousClassReference() val classReference = expression.getClassOrAnonymousClassReference()
var arguments = expression.getArgumentList()?.getExpressions() ?: array()
val constructor = expression.resolveMethod()
if (constructor != null && converter.conversionScope.contains(constructor) && !constructor.isPrimaryConstructor()) {
//TODO: handle anonymous class!
// non-primary constructor converted to factory method in class object
val reference = expression.getClassReference()
val typeParameters = if (reference != null) typeConverter.convertTypes(reference.getTypeParameters()) else listOf()
return QualifiedExpression(Identifier(constructor.getName(), false).assignNoPrototype(),
MethodCallExpression.buildNotNull(null, "create", converter.convertExpressions(arguments), typeParameters).assignNoPrototype())
}
return NewClassExpression(converter.convertElement(classReference), return NewClassExpression(converter.convertElement(classReference),
convertArguments(expression), convertArguments(expression),
converter.convertExpression(expression.getQualifier()), converter.convertExpression(expression.getQualifier()),
@@ -94,10 +94,7 @@ abstract class AbstractJavaToKotlinConverterTest() : LightIdeaTestCase() {
"using the first line of test data file") "using the first line of test data file")
} }
val reformatInFun = when (prefix) { val reformatInFun = prefix in setOf("element", "expression", "statement")
"element", "expression", "statement" -> true
else -> false
}
val actual = reformat(rawConverted, project, reformatInFun) val actual = reformat(rawConverted, project, reformatInFun)
val kotlinPath = javaPath.replace(".java", ".kt") val kotlinPath = javaPath.replace(".java", ".kt")
+9 -11
View File
@@ -2,23 +2,21 @@ package demo
import java.util.HashMap import java.util.HashMap
class Test private() { fun Test(): Test {
class object { return Test()
fun create(): Test {
return Test()
}
fun create(s: String): Test {
return Test()
}
}
} }
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)
val m2 = HashMap(10) val m2 = HashMap(10)
val t1 = Test.create() val t1 = Test()
val t2 = Test.create("") val t2 = Test("")
} }
} }
@@ -1,3 +1,8 @@
// this is a secondary constructor
fun A(): A {
return A(1)
} // end of secondary constructor body
class A// this is a primary constructor class A// this is a primary constructor
(p: Int) { (p: Int) {
private val v: Int private val v: Int
@@ -5,14 +10,6 @@ class A// this is a primary constructor
{ {
v = 1 v = 1
} // end of primary constructor body } // end of primary constructor body
class object {
// this is a secondary constructor
fun create(): A {
return A(1)
} // end of secondary constructor body
}
} }
class B// this constructor will disappear class B// this constructor will disappear
@@ -1,4 +1,6 @@
//file //file
package pack
class C { class C {
C(int arg1, int arg2, int arg3) { C(int arg1, int arg2, int arg3) {
} }
@@ -1,22 +1,22 @@
class C(arg1: Int, arg2: Int, arg3: Int) { package pack
class object {
fun create(arg1: Int, arg2: Int): C {
return C(arg1, arg2, 0)
}
fun create(arg1: Int): C { fun C(arg1: Int, arg2: Int): C {
return C(arg1, 0, 0) return C(arg1, arg2, 0)
}
}
} }
fun C(arg1: Int): C {
return C(arg1, 0, 0)
}
class C(arg1: Int, arg2: Int, arg3: Int)
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val c1 = C(100, 100, 100) val c1 = C(100, 100, 100)
val c2 = C.create(100, 100) val c2 = C(100, 100)
val c3 = C.create(100) val c3 = C(100)
} }
} }
} }
@@ -1,3 +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
@@ -6,30 +20,13 @@ class C(val myArg1: Int) {
myArg2 = 0 myArg2 = 0
myArg3 = 0 myArg3 = 0
} }
class object {
fun create(arg1: Int, arg2: Int, arg3: Int): C {
val __ = C(arg1)
__.myArg2 = arg2
__.myArg3 = arg3
return __
}
fun create(arg1: Int, arg2: Int): C {
val __ = C(arg1)
__.myArg2 = arg2
__.myArg3 = 0
return __
}
}
} }
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val c1 = C.create(100, 100, 100) val c1 = C(100, 100, 100)
val c2 = C.create(100, 100) val c2 = C(100, 100)
val c3 = C(100) val c3 = C(100)
} }
} }
+14 -17
View File
@@ -1,26 +1,23 @@
class C(arg1: Int, arg2: Int, arg3: Int) { fun C(arg1: Int, arg2: Int): C {
class object { val __ = C(arg1, arg2, 0)
System.out.println()
fun create(arg1: Int, arg2: Int): C { return __
val __ = C(arg1, arg2, 0)
System.out.println()
return __
}
fun create(arg1: Int): C {
val __ = C(arg1, 0)
System.out.println()
return __
}
}
} }
fun C(arg1: Int): C {
val __ = C(arg1, 0)
System.out.println()
return __
}
class C(arg1: Int, arg2: Int, arg3: Int)
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val c1 = C(1, 2, 3) val c1 = C(1, 2, 3)
val c2 = C.create(5, 6) val c2 = C(5, 6)
val c3 = C.create(7) val c3 = C(7)
} }
} }
} }
@@ -1,36 +1,33 @@
public class Identifier<T> private(private val myName: T, private val myHasDollar: Boolean) { 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>(private val myName: T, private val myHasDollar: Boolean) {
private var myNullable = true private var myNullable = true
public fun getName(): T { public fun getName(): T {
return myName return myName
} }
class object {
public fun <T> create(name: T): Identifier<T> {
return Identifier(name, false)
}
public fun <T> create(name: T, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, false)
__.myNullable = isNullable
return __
}
public fun <T> create(name: T, hasDollar: Boolean, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, hasDollar)
__.myNullable = isNullable
return __
}
}
} }
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val i1 = Identifier.create<String>("name", false, true) val i1 = Identifier<String>("name", false, true)
val i2 = Identifier.create<String>("name", false) val i2 = Identifier<String>("name", false)
val i3 = Identifier.create<String>("name") val i3 = Identifier<String>("name")
} }
} }
} }
@@ -1,36 +1,33 @@
public class Identifier private(private val myName: String, private val myHasDollar: Boolean) { 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(private val myName: String, private val myHasDollar: Boolean) {
private var myNullable = true private var myNullable = true
public fun getName(): String { public fun getName(): String {
return myName return myName
} }
class object {
public fun create(name: String): Identifier {
return Identifier(name, false)
}
public fun create(name: String, isNullable: Boolean): Identifier {
val __ = Identifier(name, false)
__.myNullable = isNullable
return __
}
public fun create(name: String, hasDollar: Boolean, isNullable: Boolean): Identifier {
val __ = Identifier(name, hasDollar)
__.myNullable = isNullable
return __
}
}
} }
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val i1 = Identifier.create("name", false, true) val i1 = Identifier("name", false, true)
val i2 = Identifier.create("name", false) val i2 = Identifier("name", false)
val i3 = Identifier.create("name") val i3 = Identifier("name")
} }
} }
} }
@@ -1,18 +1,15 @@
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
} }
private fun staticFoo(p: Int): Int {
class object { return p
private fun staticFoo(p: Int): Int {
return p
}
fun create(arg1: Int, arg2: Int, other: C): C {
val __ = C(arg1, arg2, 0)
System.out.println(__.foo(1) + __.foo(2) + other.foo(3) + staticFoo(4) + C.staticFoo(5))
return __
}
} }
} }
@@ -1,29 +1,27 @@
class C private() { fun C(arg1: Int, arg2: Int, arg3: Int): C {
class object { return C()
fun create(arg1: Int, arg2: Int, arg3: Int): C {
return C()
}
fun create(arg1: Int, arg2: Int): C {
val __ = C(arg1, arg2, 0)
System.out.println()
return __
}
fun create(arg: Int): C {
val __ = C()
System.out.println(arg)
return __
}
}
} }
fun C(arg1: Int, arg2: Int): C {
val __ = C(arg1, arg2, 0)
System.out.println()
return __
}
fun C(arg: Int): C {
val __ = C()
System.out.println(arg)
return __
}
class C
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val c1 = C.create(1, 2, 3) val c1 = C(1, 2, 3)
val c2 = C.create(5, 6) val c2 = C(5, 6)
val c3 = C.create(7) val c3 = C(7)
} }
} }
} }
@@ -1,6 +1,6 @@
//file //file
class C { class C {
private val field; private int field;
C(int arg1, int arg2, int arg3) { C(int arg1, int arg2, int arg3) {
arg1++; arg1++;
@@ -1,5 +1,16 @@
fun C(arg1: Int, arg2: Int): C {
var arg2 = arg2
val __ = C(arg1, arg2, 0)
arg2++
return __
}
fun C(arg1: Int): C {
return C(arg1, 0, 0)
}
class C(arg1: Int, arg2: Int, arg3: Int) { class C(arg1: Int, arg2: Int, arg3: Int) {
private val field: `val` private val field: Int
{ {
var arg1 = arg1 var arg1 = arg1
@@ -9,28 +20,14 @@ class C(arg1: Int, arg2: Int, arg3: Int) {
field = arg3 field = arg3
arg3++ arg3++
} }
class object {
fun create(arg1: Int, arg2: Int): C {
var arg2 = arg2
val __ = C(arg1, arg2, 0)
arg2++
return __
}
fun create(arg1: Int): C {
return C(arg1, 0, 0)
}
}
} }
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val c1 = C(100, 100, 100) val c1 = C(100, 100, 100)
val c2 = C.create(100, 100) val c2 = C(100, 100)
val c3 = C.create(100) val c3 = C(100)
} }
} }
} }
@@ -1,12 +1,9 @@
class C private(arg1: Int, arg2: Int, arg3: Int) { private fun C(arg1: Int, arg2: Int): C {
class object { return C(arg1, arg2, 0)
private fun create(arg1: Int, arg2: Int): C {
return C(arg1, arg2, 0)
}
public fun create(arg1: Int): C {
return C(arg1, 0, 0)
}
}
} }
public fun C(arg1: Int): C {
return C(arg1, 0, 0)
}
class C private(arg1: Int, arg2: Int, arg3: Int)
@@ -1,17 +1,14 @@
class C(private val arg1: Int, private val arg2: Int, private val arg3: Int) { fun C(arg1: Int, arg2: Int, other: C): C {
class object { val __ = C(arg1, arg2, 0)
System.out.println(__.arg1 + other.arg2)
fun create(arg1: Int, arg2: Int, other: C): C { return __
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)
class User { class User {
fun foo() { fun foo() {
val c1 = C(100, 100, 100) val c1 = C(100, 100, 100)
val c2 = C.create(100, 100, c1) val c2 = C(100, 100, c1)
} }
} }
@@ -1,12 +1,12 @@
fun C(p: Int): C {
val __ = C()
System.out.println(C.staticField1 + C.staticField2)
return __
}
class C { class C {
class object { class object {
private val staticField1 = 0 private val staticField1 = 0
private val staticField2 = 0 private val staticField2 = 0
fun create(p: Int): C {
val __ = C()
System.out.println(staticField1 + C.staticField2)
return __
}
} }
} }
@@ -1,4 +1,12 @@
public class Test private(private val myName: String) { 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()
@@ -9,14 +17,6 @@ public class Test private(private val myName: String) {
class object { class object {
public fun create(): Test {
return Test(null)
}
public fun create(name: String): Test {
return Test(foo(name))
}
fun foo(n: String): String { fun foo(n: String): String {
return "" return ""
} }
@@ -26,7 +26,7 @@ public class Test private(private val myName: String) {
public class User { public class User {
class object { class object {
public fun main() { public fun main() {
val t = Test.create("name") val t = Test("name")
} }
} }
} }
+20 -23
View File
@@ -1,36 +1,33 @@
public class Identifier<T> private(private val myName: T, private val myHasDollar: Boolean) { 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>(private val myName: T, private val myHasDollar: Boolean) {
private var myNullable = true private var myNullable = true
public fun getName(): T { public fun getName(): T {
return myName return myName
} }
class object {
public fun <T> create(name: T): Identifier<T> {
return Identifier(name, false)
}
public fun <T> create(name: T, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, false)
__.myNullable = isNullable
return __
}
public fun <T> create(name: T, hasDollar: Boolean, isNullable: Boolean): Identifier<T> {
val __ = Identifier(name, hasDollar)
__.myNullable = isNullable
return __
}
}
} }
public class User { public class User {
class object { class object {
public fun main(args: Array<String>) { public fun main(args: Array<String>) {
val i1 = Identifier.create<String>("name", false, true) val i1 = Identifier<String>("name", false, true)
val i2 = Identifier.create<String>("name", false) val i2 = Identifier<String>("name", false)
val i3 = Identifier.create<String>("name") val i3 = Identifier<String>("name")
} }
} }
} }