Java to Kotlin convertor: generating of val/var constructor parameters when possible
This commit is contained in:
@@ -23,6 +23,14 @@ import com.intellij.psi.JavaRecursiveElementVisitor
|
||||
import java.util.LinkedHashSet
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethodCallExpression
|
||||
import com.intellij.psi.PsiParameter
|
||||
import com.intellij.psi.PsiField
|
||||
import com.intellij.psi.PsiAssignmentExpression
|
||||
import com.intellij.psi.PsiThisExpression
|
||||
import com.intellij.psi.PsiStatement
|
||||
import com.intellij.psi.PsiExpressionStatement
|
||||
import com.intellij.psi.PsiBlockStatement
|
||||
import com.intellij.psi.util.PsiUtil
|
||||
|
||||
fun PsiMethod.isPrimaryConstructor(): Boolean {
|
||||
if (!isConstructor()) return false
|
||||
@@ -63,11 +71,11 @@ fun PsiClass.getPrimaryConstructor(): PsiMethod? {
|
||||
}
|
||||
}
|
||||
|
||||
fun isInsidePrimaryConstructor(element: PsiElement): Boolean
|
||||
= getContainingConstructor(element)?.isPrimaryConstructor() ?: false
|
||||
fun PsiElement.isInsidePrimaryConstructor(): Boolean
|
||||
= getContainingConstructor()?.isPrimaryConstructor() ?: false
|
||||
|
||||
fun getContainingConstructor(element: PsiElement): PsiMethod? {
|
||||
var context = element.getContext()
|
||||
fun PsiElement.getContainingConstructor(): PsiMethod? {
|
||||
var context = getContext()
|
||||
while (context != null) {
|
||||
val _context = context!!
|
||||
if (_context is PsiMethod) {
|
||||
@@ -90,3 +98,4 @@ fun PsiMethodCallExpression.isSuperConstructorCall(): Boolean {
|
||||
|
||||
fun PsiReferenceExpression.isThisConstructorCall(): Boolean
|
||||
= getReferences().filter { it.getCanonicalText() == "this" }.map { it.resolve() }.any { it is PsiMethod && it.isConstructor() }
|
||||
|
||||
|
||||
@@ -18,14 +18,12 @@ package org.jetbrains.jet.j2k
|
||||
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.jet.j2k.ast.*
|
||||
import org.jetbrains.jet.j2k.ast.types.ClassType
|
||||
import org.jetbrains.jet.j2k.ast.types.EmptyType
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.visitors.*
|
||||
import java.util.*
|
||||
import com.intellij.psi.CommonClassNames.*
|
||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.util.PsiUtil
|
||||
|
||||
public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
|
||||
@@ -54,7 +52,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
private fun convertTopElement(element: PsiElement?): Element? = when(element) {
|
||||
is PsiJavaFile -> convertFile(element)
|
||||
is PsiClass -> convertClass(element)
|
||||
is PsiMethod -> convertMethod(element)
|
||||
is PsiMethod -> convertMethod(element, HashSet())
|
||||
is PsiField -> convertField(element)
|
||||
is PsiStatement -> convertStatement(element)
|
||||
is PsiExpression -> convertExpression(element)
|
||||
@@ -72,13 +70,29 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
|
||||
public fun convertAnonymousClass(anonymousClass: PsiAnonymousClass): AnonymousClass {
|
||||
return AnonymousClass(this, convertMembers(anonymousClass))
|
||||
return AnonymousClass(this, convertClassBody(anonymousClass))
|
||||
}
|
||||
|
||||
private fun convertMembers(psiClass: PsiClass): List<Element> {
|
||||
val allChildren = psiClass.getChildren().toList()
|
||||
val lBraceIndex = allChildren.indexOf(psiClass.getLBrace())
|
||||
return allChildren.subList(lBraceIndex, allChildren.size).map { convertMember(it) }.filterNotNull()
|
||||
private fun convertClassBody(psiClass: PsiClass): List<Element> {
|
||||
val membersToRemove = HashSet<PsiMember>()
|
||||
val convertedMembers = LinkedHashMap<PsiElement, Element>()
|
||||
var inBody = false
|
||||
val lBrace = psiClass.getLBrace()
|
||||
for (element in psiClass.getChildren()) {
|
||||
if (element == lBrace) inBody = true
|
||||
if (inBody) {
|
||||
convertedMembers.put(element, convertMember(element, membersToRemove))
|
||||
}
|
||||
}
|
||||
return convertedMembers.keySet().filter { !membersToRemove.contains(it) }.map { convertedMembers[it]!! }
|
||||
}
|
||||
|
||||
private fun convertMember(element: PsiElement, membersToRemove: MutableSet<PsiMember>): Element = when(element) {
|
||||
is PsiMethod -> convertMethod(element, membersToRemove)
|
||||
is PsiField -> convertField(element)
|
||||
is PsiClass -> convertClass(element)
|
||||
is PsiClassInitializer -> convertInitializer(element)
|
||||
else -> convertElement(element)
|
||||
}
|
||||
|
||||
private fun getComments(member: PsiMember): MemberComments {
|
||||
@@ -93,39 +107,31 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
return MemberComments(whiteSpacesAndComments)
|
||||
}
|
||||
|
||||
private fun convertMember(e: PsiElement?): Element? = when(e) {
|
||||
is PsiMethod -> convertMethod(e, true)
|
||||
is PsiField -> convertField(e)
|
||||
is PsiClass -> convertClass(e)
|
||||
is PsiClassInitializer -> convertInitializer(e)
|
||||
else -> convertElement(e)
|
||||
}
|
||||
|
||||
private fun convertClass(psiClass: PsiClass): Class {
|
||||
val modifiers = convertModifierList(psiClass.getModifierList())
|
||||
val typeParameters = convertTypeParameterList(psiClass.getTypeParameterList())
|
||||
val implementsTypes = convertToNotNullableTypes(psiClass.getImplementsListTypes())
|
||||
val extendsTypes = convertToNotNullableTypes(psiClass.getExtendsListTypes())
|
||||
val name = Identifier(psiClass.getName()!!)
|
||||
val members = ArrayList(convertMembers(psiClass))
|
||||
val classBodyElements = ArrayList(convertClassBody(psiClass))
|
||||
|
||||
when {
|
||||
psiClass.isInterface() -> return Trait(this, name, getComments(psiClass), modifiers, typeParameters, extendsTypes, listOf(), implementsTypes, members)
|
||||
psiClass.isInterface() -> return Trait(this, name, getComments(psiClass), modifiers, typeParameters, extendsTypes, listOf(), implementsTypes, classBodyElements)
|
||||
|
||||
psiClass.isEnum() -> return Enum(this, name, getComments(psiClass), modifiers, typeParameters, listOf(), listOf(), implementsTypes, members)
|
||||
psiClass.isEnum() -> return Enum(this, name, getComments(psiClass), modifiers, typeParameters, listOf(), listOf(), implementsTypes, classBodyElements)
|
||||
|
||||
else -> {
|
||||
if (psiClass.getConstructors().size > 1 && psiClass.getPrimaryConstructor() == null) {
|
||||
val finalOrWithEmptyInitializerFields = members.filterIsInstance(javaClass<Field>()).filter { it.isVal() || it.initializer.toKotlin().isEmpty() }
|
||||
if (psiClass.getPrimaryConstructor() == null && psiClass.getConstructors().size > 1) {
|
||||
val finalOrWithEmptyInitializerFields = classBodyElements.filterIsInstance(javaClass<Field>()).filter { it.isVal() || it.initializer.toKotlin().isEmpty() }
|
||||
val initializers = HashMap<String, String>()
|
||||
for (member in members) {
|
||||
if (member is Constructor && !member.isPrimary) {
|
||||
for (element in classBodyElements) {
|
||||
if (element is SecondaryConstructor) {
|
||||
for (field in finalOrWithEmptyInitializerFields) {
|
||||
initializers.put(field.identifier.toKotlin(), getDefaultInitializer(field))
|
||||
}
|
||||
|
||||
val newStatements = ArrayList<Statement>()
|
||||
for (statement in member.block!!.statements) {
|
||||
for (statement in element.block!!.statements) {
|
||||
var keepStatement = true
|
||||
if (statement is AssignmentExpression) {
|
||||
val assignee = statement.left
|
||||
@@ -148,17 +154,15 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
|
||||
}
|
||||
newStatements.add(0, DummyStringExpression("val __ = " + createPrimaryConstructorInvocation(name.toKotlin(), finalOrWithEmptyInitializerFields, initializers)))
|
||||
member.block = Block(newStatements)
|
||||
element.block = Block(newStatements)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: comments?
|
||||
members.add(Constructor(this, Identifier.Empty, MemberComments.Empty, Collections.emptySet<Modifier>(),
|
||||
ClassType(name, listOf(), false, this),
|
||||
TypeParameterList.Empty,
|
||||
ParameterList(createParametersFromFields(finalOrWithEmptyInitializerFields)),
|
||||
Block(createInitStatementsFromFields(finalOrWithEmptyInitializerFields)),
|
||||
true))
|
||||
val parameters = finalOrWithEmptyInitializerFields.map { Parameter(Identifier("_" + it.identifier.name), it.`type`, Parameter.VarValModifier.None, listOf()) }
|
||||
classBodyElements.add(PrimaryConstructor(this, MemberComments.Empty, Collections.emptySet<Modifier>(),
|
||||
ParameterList(parameters),
|
||||
Block(createInitStatementsFromFields(finalOrWithEmptyInitializerFields))))
|
||||
}
|
||||
|
||||
val baseClassParams: List<Expression> = run {
|
||||
@@ -173,13 +177,43 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
}
|
||||
|
||||
return Class(this, name, getComments(psiClass), modifiers, typeParameters, extendsTypes, baseClassParams, implementsTypes, members)
|
||||
return Class(this, name, getComments(psiClass), modifiers, typeParameters, extendsTypes, baseClassParams, implementsTypes, classBodyElements)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun findBackingFieldForConstructorParameter(parameter: PsiParameter, constructor: PsiMethod): Pair<PsiField, PsiStatement>? {
|
||||
val body = constructor.getBody() ?: return null
|
||||
|
||||
val refs = findExpressionReferences(parameter, body)
|
||||
|
||||
if (refs.any { PsiUtil.isAccessedForWriting(it) }) return null
|
||||
|
||||
for(ref in refs) {
|
||||
val assignment = ref.getParent() as? PsiAssignmentExpression ?: continue
|
||||
if (assignment.getOperationSign().getTokenType() != JavaTokenType.EQ) continue
|
||||
val assignee = assignment.getLExpression() as? PsiReferenceExpression ?: continue
|
||||
if (!isQualifierEmptyOrThis(assignee)) continue
|
||||
val field = assignee.resolve() as? PsiField ?: continue
|
||||
if (field.getContainingClass() != constructor.getContainingClass()) continue
|
||||
if (field.getInitializer() != null) continue
|
||||
|
||||
// assignment should be a top-level statement
|
||||
val statement = assignment.getParent() as? PsiExpressionStatement ?: continue
|
||||
if (statement.getParent() != body) continue
|
||||
|
||||
// and no other assignments to field should exist in the constructor
|
||||
if (findExpressionReferences(field, body).any { it != assignee && PsiUtil.isAccessedForWriting(it) && isQualifierEmptyOrThis(it) }) continue
|
||||
//TODO: check access to field before assignment
|
||||
|
||||
return field to statement
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun convertInitializer(initializer: PsiClassInitializer): Initializer {
|
||||
return Initializer(convertBlock(initializer.getBody(), true), convertModifierList(initializer.getModifierList()))
|
||||
return Initializer(convertBlock(initializer.getBody()), convertModifierList(initializer.getModifierList()))
|
||||
}
|
||||
|
||||
private fun convertField(field: PsiField): Field {
|
||||
@@ -192,9 +226,9 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
convertElement(field.getArgumentList()))
|
||||
}
|
||||
|
||||
var kType = convertType(field.getType(), field.isAnnotatedAsNotNull())
|
||||
var kType = convertVariableType(field)
|
||||
if (field.hasModifierProperty(PsiModifier.FINAL) && field.getInitializer().isDefinitelyNotNull()) {
|
||||
kType = kType.convertedToNotNull();
|
||||
kType = kType.toNotNullType();
|
||||
}
|
||||
|
||||
return Field(Identifier(field.getName()!!),
|
||||
@@ -205,60 +239,97 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
field.countWriteAccesses(field.getContainingClass()))
|
||||
}
|
||||
|
||||
private fun convertMethod(method: PsiMethod): Function {
|
||||
return convertMethod(method, true)
|
||||
}
|
||||
|
||||
private fun convertMethod(method: PsiMethod, notEmpty: Boolean): Function {
|
||||
private fun convertMethod(method: PsiMethod, membersToRemove: MutableSet<PsiMember>): Function {
|
||||
if (directlyOverridesMethodFromObject(method)) {
|
||||
dispatcher.expressionVisitor = ExpressionVisitorForDirectObjectInheritors(this)
|
||||
}
|
||||
else {
|
||||
dispatcher.expressionVisitor = ExpressionVisitor(this)
|
||||
}
|
||||
methodReturnType = method.getReturnType()
|
||||
val identifier = Identifier(method.getName())
|
||||
val returnType = convertType(method.getReturnType(), method.isAnnotatedAsNotNull())
|
||||
val body = convertBlock(method.getBody(), notEmpty)
|
||||
|
||||
val params = createFunctionParameters(method)
|
||||
val typeParameterList = convertTypeParameterList(method.getTypeParameterList())
|
||||
val modifiers = HashSet(convertModifierList(method.getModifierList()))
|
||||
if (isOverride(method)) {
|
||||
modifiers.add(Modifier.OVERRIDE)
|
||||
try {
|
||||
methodReturnType = method.getReturnType()
|
||||
val returnType = convertType(method.getReturnType(), method.isAnnotatedAsNotNull())
|
||||
|
||||
val modifiers = HashSet(convertModifierList(method.getModifierList()))
|
||||
if (isOverride(method)) {
|
||||
modifiers.add(Modifier.OVERRIDE)
|
||||
}
|
||||
|
||||
val containingClass = method.getContainingClass()
|
||||
if (containingClass != null && containingClass.isInterface()) {
|
||||
modifiers.remove(Modifier.ABSTRACT)
|
||||
}
|
||||
|
||||
if (isNotOpenMethod(method)) {
|
||||
modifiers.add(Modifier.NOT_OPEN)
|
||||
}
|
||||
|
||||
val comments = getComments(method)
|
||||
|
||||
if (method.isConstructor()) {
|
||||
if (method.isPrimaryConstructor()) {
|
||||
val params = method.getParameterList().getParameters()
|
||||
val parameterToField = HashMap<PsiParameter, PsiField>()
|
||||
val body = method.getBody()
|
||||
val block = if (body != null) {
|
||||
val statementsToRemove = HashSet<PsiStatement>()
|
||||
val usageReplacementMap = HashMap<PsiVariable, String>()
|
||||
for (parameter in params) {
|
||||
val (field, initializationStatement) = findBackingFieldForConstructorParameter(parameter, method) ?: continue
|
||||
if (membersToRemove.contains(field)) continue // already used as backing field
|
||||
if (convertVariableType(field) != convertVariableType(parameter)) continue
|
||||
|
||||
parameterToField.put(parameter, field)
|
||||
statementsToRemove.add(initializationStatement)
|
||||
|
||||
if (field.getName() != parameter.getName()) {
|
||||
usageReplacementMap.put(parameter, field.getName()!!)
|
||||
}
|
||||
}
|
||||
dispatcher.expressionVisitor = ExpressionVisitor(this, usageReplacementMap)
|
||||
Block(convertStatements(body.getStatements().filter{ !statementsToRemove.contains(it) }), false)
|
||||
}
|
||||
else {
|
||||
Block.Empty
|
||||
}
|
||||
|
||||
val parameterList = ParameterList(params.map {
|
||||
val field = parameterToField[it]
|
||||
if (field == null) {
|
||||
convertParameter(it)
|
||||
}
|
||||
else {
|
||||
membersToRemove.add(field)
|
||||
Parameter(Identifier(field.getName()!!),
|
||||
convertVariableType(it),
|
||||
if (field.hasModifierProperty(PsiModifier.FINAL)) Parameter.VarValModifier.Val else Parameter.VarValModifier.Var,
|
||||
convertModifierList(field.getModifierList()).filter { ACCESS_MODIFIERS.contains(it) })
|
||||
}
|
||||
})
|
||||
return PrimaryConstructor(this, comments, modifiers, parameterList, block)
|
||||
}
|
||||
else {
|
||||
val params = convertParameterList(method.getParameterList())
|
||||
return SecondaryConstructor(this, comments, modifiers, params, convertBlock(method.getBody()))
|
||||
}
|
||||
}
|
||||
else {
|
||||
val params = convertParameterList(method.getParameterList())
|
||||
val typeParameterList = convertTypeParameterList(method.getTypeParameterList())
|
||||
val block = convertBlock(method.getBody())
|
||||
return Function(this, Identifier(method.getName()), comments, modifiers, returnType, typeParameterList, params, block)
|
||||
}
|
||||
}
|
||||
|
||||
val containingClass = method.getContainingClass()
|
||||
if (containingClass != null && containingClass.isInterface()) {
|
||||
modifiers.remove(Modifier.ABSTRACT)
|
||||
finally {
|
||||
dispatcher.expressionVisitor = ExpressionVisitor(this)
|
||||
}
|
||||
|
||||
if (isNotOpenMethod(method)) {
|
||||
modifiers.add(Modifier.NOT_OPEN)
|
||||
}
|
||||
|
||||
if (method.isConstructor()) {
|
||||
return Constructor(this, identifier, getComments(method), modifiers, returnType, typeParameterList, params,
|
||||
Block(body.statements), method.isPrimaryConstructor())
|
||||
}
|
||||
|
||||
return Function(this, identifier, getComments(method), modifiers, returnType, typeParameterList, params, body)
|
||||
}
|
||||
|
||||
private fun createFunctionParameters(method: PsiMethod): ParameterList {
|
||||
val result = ArrayList<Parameter>()
|
||||
for (parameter in method.getParameterList().getParameters()) {
|
||||
result.add(Parameter(Identifier(parameter.getName()!!),
|
||||
convertType(parameter.getType(), parameter.isAnnotatedAsNotNull()),
|
||||
parameter.countWriteAccesses(method.getBody()) == 0))
|
||||
}
|
||||
return ParameterList(result)
|
||||
}
|
||||
|
||||
public fun convertBlock(block: PsiCodeBlock?, notEmpty: Boolean = true): Block {
|
||||
public fun convertBlock(block: PsiCodeBlock?): Block {
|
||||
if (block == null) return Block.Empty
|
||||
|
||||
return Block(convertStatements(block.getChildren().toList()), notEmpty)
|
||||
return Block(convertStatements(block.getChildren().toList()), true)
|
||||
}
|
||||
|
||||
public fun convertStatements(statements: List<PsiElement>): StatementList {
|
||||
@@ -302,13 +373,13 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
|
||||
public fun convertTypeElement(element: PsiTypeElement?): TypeElement {
|
||||
return TypeElement(if (element == null)
|
||||
EmptyType()
|
||||
Type.Empty
|
||||
else
|
||||
convertType(element.getType()))
|
||||
}
|
||||
|
||||
public fun convertType(`type`: PsiType?): Type {
|
||||
if (`type` == null) return EmptyType()
|
||||
if (`type` == null) return Type.Empty
|
||||
|
||||
return `type`.accept<Type>(TypeVisitor(this))!!
|
||||
}
|
||||
@@ -320,22 +391,30 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
public fun convertType(`type`: PsiType?, notNull: Boolean): Type {
|
||||
val result = convertType(`type`)
|
||||
if (notNull) {
|
||||
return result.convertedToNotNull()
|
||||
return result.toNotNullType()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
public fun convertVariableType(variable: PsiVariable): Type
|
||||
= convertType(variable.getType(), variable.isAnnotatedAsNotNull())
|
||||
|
||||
private fun convertToNotNullableTypes(types: Array<out PsiType?>): List<Type>
|
||||
= types.map { convertType(it).convertedToNotNull() }
|
||||
= types.map { convertType(it).toNotNullType() }
|
||||
|
||||
public fun convertParameterList(parameters: Array<PsiParameter>): List<Parameter>
|
||||
= parameters.map { convertParameter(it) }
|
||||
public fun convertParameterList(parameters: PsiParameterList): ParameterList
|
||||
= ParameterList(parameters.getParameters().map { convertParameter(it) })
|
||||
|
||||
public fun convertParameter(parameter: PsiParameter, forceNotNull: Boolean = false): Parameter {
|
||||
return Parameter(Identifier(parameter.getName()!!),
|
||||
convertType(parameter.getType(),
|
||||
forceNotNull || parameter.isAnnotatedAsNotNull()), true)
|
||||
public fun convertParameter(parameter: PsiParameter,
|
||||
forceNotNull: Boolean = false,
|
||||
varValModifier: Parameter.VarValModifier = Parameter.VarValModifier.None,
|
||||
modifiers: Collection<Modifier> = listOf()): Parameter {
|
||||
var `type` = convertVariableType(parameter)
|
||||
if (forceNotNull) {
|
||||
`type` = `type`.toNotNullType()
|
||||
}
|
||||
return Parameter(Identifier(parameter.getName()!!), `type`, varValModifier, modifiers)
|
||||
}
|
||||
|
||||
public fun convertArguments(expression: PsiCallExpression): List<Expression> {
|
||||
@@ -380,10 +459,6 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
return expression
|
||||
}
|
||||
|
||||
private fun createParametersFromFields(fields: List<Field>): List<Parameter> {
|
||||
return fields.map { Parameter(Identifier("_" + it.identifier.name), it.`type`, true) }
|
||||
}
|
||||
|
||||
private fun createInitStatementsFromFields(fields: List<Field>): List<Statement> {
|
||||
val result = ArrayList<Statement>()
|
||||
for (field in fields) {
|
||||
|
||||
@@ -24,26 +24,34 @@ import com.intellij.psi.PsiLiteralExpression
|
||||
import com.intellij.psi.PsiNewExpression
|
||||
import org.jetbrains.jet.j2k.ast.Field
|
||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
||||
import com.intellij.psi.util.PsiUtil
|
||||
import com.intellij.psi.PsiModifierListOwner
|
||||
import java.util.ArrayList
|
||||
import com.intellij.psi.util.PsiUtil
|
||||
import com.intellij.psi.PsiThisExpression
|
||||
import java.util.HashMap
|
||||
|
||||
fun quoteKeywords(packageName: String): String = packageName.split("\\.").map { Identifier(it).toKotlin() }.makeString(".")
|
||||
|
||||
fun PsiElement.countWriteAccesses(scope: PsiElement?): Int {
|
||||
if (scope == null) return 0
|
||||
fun findExpressionReferences(element: PsiElement, scope: PsiElement): Collection<PsiReferenceExpression> {
|
||||
class Visitor : JavaRecursiveElementVisitor() {
|
||||
val refs = ArrayList<PsiReferenceExpression>()
|
||||
|
||||
var writes = 0
|
||||
scope.accept(object: JavaRecursiveElementVisitor() {
|
||||
override fun visitReferenceExpression(expression: PsiReferenceExpression) {
|
||||
super.visitReferenceExpression(expression)
|
||||
if (PsiUtil.isAccessedForWriting(expression) && expression.isReferenceTo(this@countWriteAccesses)) {
|
||||
writes++
|
||||
if (expression.isReferenceTo(element)) {
|
||||
refs.add(expression)
|
||||
}
|
||||
}
|
||||
})
|
||||
return writes
|
||||
}
|
||||
|
||||
val visitor = Visitor()
|
||||
scope.accept(visitor)
|
||||
return visitor.refs
|
||||
}
|
||||
|
||||
fun PsiElement.countWriteAccesses(scope: PsiElement?): Int
|
||||
= if (scope != null) findExpressionReferences(this, scope).count { PsiUtil.isAccessedForWriting(it) } else 0
|
||||
|
||||
fun PsiModifierListOwner.isAnnotatedAsNotNull(): Boolean
|
||||
= getModifierList()?.getAnnotations()?.any { NOT_NULL_ANNOTATIONS.contains(it.getQualifiedName()) } ?: false
|
||||
|
||||
@@ -66,4 +74,9 @@ fun getDefaultInitializer(field: Field): String {
|
||||
else -> "0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun isQualifierEmptyOrThis(ref: PsiReferenceExpression): Boolean {
|
||||
val qualifier = ref.getQualifierExpression()
|
||||
return qualifier == null || (qualifier is PsiThisExpression && qualifier.getQualifier() == null)
|
||||
}
|
||||
|
||||
@@ -17,10 +17,9 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import java.util.Collections
|
||||
|
||||
class AnonymousClass(converter: Converter, members: List<Element>)
|
||||
class AnonymousClass(converter: Converter, bodyElements: List<Element>)
|
||||
: Class(converter,
|
||||
Identifier("anonClass"),
|
||||
MemberComments.Empty,
|
||||
@@ -29,6 +28,6 @@ class AnonymousClass(converter: Converter, members: List<Element>)
|
||||
listOf(),
|
||||
listOf(),
|
||||
listOf(),
|
||||
members) {
|
||||
bodyElements) {
|
||||
override fun toKotlin() = bodyToKotlin()
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
||||
import org.jetbrains.jet.j2k.ast.types.ArrayType
|
||||
import org.jetbrains.jet.j2k.ast.types.isPrimitive
|
||||
|
||||
open class ArrayInitializerExpression(val arrayType: ArrayType, val initializers: List<Expression>) : Expression() {
|
||||
override fun toKotlin(): String {
|
||||
@@ -32,14 +30,14 @@ open class ArrayInitializerExpression(val arrayType: ArrayType, val initializers
|
||||
private fun createArrayFunction(): String {
|
||||
val elementType = arrayType.elementType
|
||||
if (elementType.isPrimitive()) {
|
||||
return (elementType.convertedToNotNull().toKotlin() + "Array").decapitalize()
|
||||
return (elementType.toNotNullType().toKotlin() + "Array").decapitalize()
|
||||
}
|
||||
|
||||
return arrayType.convertedToNotNull().toKotlin().decapitalize()
|
||||
return arrayType.toNotNullType().toKotlin().decapitalize()
|
||||
}
|
||||
|
||||
private fun innerTypeStr(): String {
|
||||
return arrayType.convertedToNotNull().toKotlin().replace("Array", "").toLowerCase()
|
||||
return arrayType.toNotNullType().toKotlin().replace("Array", "").toLowerCase()
|
||||
}
|
||||
|
||||
private fun explicitConvertIfNeeded(i: Expression): String {
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.ArrayType
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.ast.types.PrimitiveType
|
||||
|
||||
open class ArrayWithoutInitializationExpression(val `type`: Type, val expressions: List<Expression>) : Expression() {
|
||||
override fun toKotlin(): String {
|
||||
if (`type` is ArrayType) {
|
||||
@@ -55,17 +51,17 @@ open class ArrayWithoutInitializationExpression(val `type`: Type, val expression
|
||||
return if (`type` is ArrayType)
|
||||
when (`type`.elementType) {
|
||||
is PrimitiveType ->
|
||||
`type`.convertedToNotNull().toKotlin()
|
||||
`type`.toNotNullType().toKotlin()
|
||||
is ArrayType ->
|
||||
if (hasInit)
|
||||
`type`.convertedToNotNull().toKotlin()
|
||||
`type`.toNotNullType().toKotlin()
|
||||
else
|
||||
"arrayOfNulls<" + `type`.elementType.toKotlin() + ">"
|
||||
else ->
|
||||
"arrayOfNulls<" + `type`.elementType.toKotlin() + ">"
|
||||
}
|
||||
else
|
||||
`type`.convertedToNotNull().toKotlin()
|
||||
`type`.toNotNullType().toKotlin()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.types.ClassType
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import java.util.HashSet
|
||||
import java.util.ArrayList
|
||||
|
||||
@@ -31,53 +29,50 @@ open class Class(
|
||||
val extendsTypes: List<Type>,
|
||||
val baseClassParams: List<Expression>,
|
||||
val implementsTypes: List<Type>,
|
||||
val members: List<Element>
|
||||
val bodyElements: List<Element>
|
||||
) : Member(comments, modifiers) {
|
||||
open val TYPE: String
|
||||
|
||||
override fun toKotlin(): String =
|
||||
commentsToKotlin() +
|
||||
modifiersToKotlin() +
|
||||
keyword + " " + name.toKotlin() +
|
||||
typeParameterList.toKotlin() +
|
||||
primaryConstructorSignatureToKotlin() +
|
||||
implementTypesToKotlin() +
|
||||
typeParameterList.whereToKotlin().withPrefix(" ") +
|
||||
bodyToKotlin()
|
||||
|
||||
protected open val keyword: String
|
||||
get() = "class"
|
||||
|
||||
val classMembers = parseClassMembers(members)
|
||||
protected val classMembers: ClassMembers = ClassMembers.fromBodyElements(bodyElements)
|
||||
|
||||
open fun primaryConstructorSignatureToKotlin(): String {
|
||||
protected open fun primaryConstructorSignatureToKotlin(): String {
|
||||
val constructor = classMembers.primaryConstructor
|
||||
return if (constructor != null) constructor.primarySignatureToKotlin() else "()"
|
||||
return if (constructor != null) constructor.signatureToKotlin() else "()"
|
||||
}
|
||||
|
||||
fun primaryConstructorBodyToKotlin(): String? {
|
||||
val maybeConstructor = classMembers.primaryConstructor
|
||||
if (maybeConstructor != null && !(maybeConstructor.block?.isEmpty ?: true)) {
|
||||
return "\n" + maybeConstructor.primaryBodyToKotlin() + "\n"
|
||||
protected fun primaryConstructorBodyToKotlin(): String {
|
||||
val constructor = classMembers.primaryConstructor
|
||||
if (constructor != null && !(constructor.block?.isEmpty ?: true)) {
|
||||
return "\n" + constructor.bodyToKotlin() + "\n"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
fun secondaryConstructorsAsStaticInitFunctions(): MemberList {
|
||||
return MemberList(classMembers.secondaryConstructors.elements.map { if (it is Constructor) constructorToInit(it) else it })
|
||||
private fun secondaryConstructorsAsStaticInitFunctions(): MemberList {
|
||||
return MemberList(classMembers.secondaryConstructors.elements.map { if (it is SecondaryConstructor) it.toInitFunction(this) else it })
|
||||
}
|
||||
|
||||
private fun constructorToInit(f: Function): Function {
|
||||
val modifiers = HashSet<Modifier>(f.modifiers)
|
||||
modifiers.add(Modifier.STATIC)
|
||||
val statements = ArrayList(f.block?.statements ?: ArrayList())
|
||||
statements.add(ReturnStatement(Identifier("__")))
|
||||
val block = Block(statements)
|
||||
val constructorTypeParameters = ArrayList<TypeParameter>()
|
||||
constructorTypeParameters.addAll(typeParameterList.parameters)
|
||||
constructorTypeParameters.addAll(f.typeParameterList.parameters)
|
||||
return Function(converter, Identifier("init"), MemberComments.Empty, modifiers,
|
||||
ClassType(name, constructorTypeParameters, false, converter),
|
||||
TypeParameterList(constructorTypeParameters), f.params, block)
|
||||
}
|
||||
|
||||
fun baseClassSignatureWithParams(): List<String> {
|
||||
if (TYPE.equals("class") && extendsTypes.size() == 1) {
|
||||
private fun baseClassSignatureWithParams(): List<String> {
|
||||
if (keyword.equals("class") && extendsTypes.size() == 1) {
|
||||
val baseParams = baseClassParams.toKotlin(", ")
|
||||
return arrayListOf(extendsTypes[0].toKotlin() + "(" + baseParams + ")")
|
||||
}
|
||||
return extendsTypes.map { it.toKotlin() }
|
||||
}
|
||||
|
||||
fun implementTypesToKotlin(): String {
|
||||
protected fun implementTypesToKotlin(): String {
|
||||
val allTypes = ArrayList<String>()
|
||||
allTypes.addAll(baseClassSignatureWithParams())
|
||||
allTypes.addAll(implementsTypes.map { it.toKotlin() })
|
||||
@@ -87,9 +82,9 @@ open class Class(
|
||||
" : " + allTypes.makeString(", ")
|
||||
}
|
||||
|
||||
fun modifiersToKotlin(): String {
|
||||
protected fun modifiersToKotlin(): String {
|
||||
val modifierList = ArrayList<Modifier>()
|
||||
val modifier = accessModifier()
|
||||
val modifier = modifiers.accessModifier()
|
||||
if (modifier != null) {
|
||||
modifierList.add(modifier)
|
||||
}
|
||||
@@ -102,15 +97,20 @@ open class Class(
|
||||
return modifierList.toKotlin()
|
||||
}
|
||||
|
||||
open fun isDefinitelyFinal() = modifiers.contains(Modifier.FINAL)
|
||||
protected open fun isDefinitelyFinal(): Boolean
|
||||
= modifiers.contains(Modifier.FINAL)
|
||||
|
||||
open fun needsOpenModifier() = !isDefinitelyFinal() && converter.settings.openByDefault
|
||||
protected open fun needsOpenModifier(): Boolean
|
||||
= !isDefinitelyFinal() && converter.settings.openByDefault
|
||||
|
||||
fun bodyToKotlin(): String {
|
||||
return " {" + classMembers.nonStaticMembers.toKotlin() + primaryConstructorBodyToKotlin() + classObjectToKotlin() + "}"
|
||||
//TODO:
|
||||
//val insideBody = classMembers.nonStaticMembers.toKotlin() + primaryConstructorBodyToKotlin() + classObjectToKotlin()
|
||||
//return if (insideBody.trim().isNotEmpty()) " {" + insideBody + "}" else ""
|
||||
}
|
||||
|
||||
fun classObjectToKotlin(): String {
|
||||
private fun classObjectToKotlin(): String {
|
||||
val secondaryConstructorsAsStaticInitFunctions = secondaryConstructorsAsStaticInitFunctions()
|
||||
val staticMembers = classMembers.staticMembers
|
||||
if (secondaryConstructorsAsStaticInitFunctions.isEmpty() && staticMembers.isEmpty()) {
|
||||
@@ -118,14 +118,4 @@ open class Class(
|
||||
}
|
||||
return "\nclass object {${secondaryConstructorsAsStaticInitFunctions.toKotlin()}${staticMembers.toKotlin()}}"
|
||||
}
|
||||
|
||||
override fun toKotlin(): String =
|
||||
commentsToKotlin() +
|
||||
modifiersToKotlin() +
|
||||
TYPE + " " + name.toKotlin() +
|
||||
typeParameterList.toKotlin() +
|
||||
primaryConstructorSignatureToKotlin() +
|
||||
implementTypesToKotlin() +
|
||||
typeParameterList.whereToKotlin().withPrefix(" ") +
|
||||
bodyToKotlin()
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
class Constructor(
|
||||
converter: Converter,
|
||||
identifier: Identifier,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
`type`: Type,
|
||||
typeParameters: TypeParameterList,
|
||||
params: Element,
|
||||
block: Block,
|
||||
val isPrimary: Boolean
|
||||
) : Function(converter, identifier, comments, modifiers,
|
||||
`type`, typeParameters, params, block) {
|
||||
|
||||
fun primarySignatureToKotlin(): String {
|
||||
return "(" + params.toKotlin() + ")"
|
||||
}
|
||||
|
||||
fun primaryBodyToKotlin(): String {
|
||||
return block!!.toKotlin()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import java.util.HashSet
|
||||
import java.util.ArrayList
|
||||
|
||||
abstract class Constructor(
|
||||
converter: Converter,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
parameterList: ParameterList,
|
||||
block: Block
|
||||
) : Function(converter, Identifier.Empty, comments, modifiers, Type.Empty, TypeParameterList.Empty, parameterList, block)
|
||||
|
||||
class PrimaryConstructor(converter: Converter,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
parameterList: ParameterList,
|
||||
block: Block)
|
||||
: Constructor(converter, comments, modifiers, parameterList, block) {
|
||||
|
||||
public fun signatureToKotlin(): String = "(" + parameterList.toKotlin() + ")"
|
||||
|
||||
public fun bodyToKotlin(): String = block!!.toKotlin()
|
||||
}
|
||||
|
||||
class SecondaryConstructor(converter: Converter,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
parameterList: ParameterList,
|
||||
block: Block)
|
||||
: Constructor(converter, comments, modifiers, parameterList, block) {
|
||||
|
||||
public fun toInitFunction(containingClass: Class): Function {
|
||||
val modifiers = HashSet(modifiers)
|
||||
modifiers.add(Modifier.STATIC)
|
||||
val statements = ArrayList(block?.statements ?: listOf())
|
||||
statements.add(ReturnStatement(Identifier("__")))
|
||||
val block = Block(statements)
|
||||
val typeParameters = ArrayList<TypeParameter>()
|
||||
typeParameters.addAll(containingClass.typeParameterList.parameters)
|
||||
return Function(converter, Identifier("init"), MemberComments.Empty, modifiers,
|
||||
ClassType(containingClass.name, typeParameters, false, converter),
|
||||
TypeParameterList(typeParameters), parameterList, block)
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
class Enum(
|
||||
converter: Converter,
|
||||
@@ -28,9 +27,9 @@ class Enum(
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Element>
|
||||
bodyElements: List<Element>
|
||||
) : Class(converter, name, comments, modifiers, typeParameterList,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
extendsTypes, baseClassParams, implementsTypes, bodyElements) {
|
||||
|
||||
override fun primaryConstructorSignatureToKotlin(): String {
|
||||
val s: String = super.primaryConstructorSignatureToKotlin()
|
||||
|
||||
@@ -16,15 +16,13 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
open class EnumConstant(
|
||||
identifier: Identifier,
|
||||
members: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
`type`: Type,
|
||||
params: Element
|
||||
) : Field(identifier, members, modifiers, `type`.convertedToNotNull(), params, 0) {
|
||||
) : Field(identifier, members, modifiers, `type`.toNotNullType(), params, 0) {
|
||||
|
||||
override fun toKotlin(): String {
|
||||
if (initializer.toKotlin().isEmpty()) {
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
open class ArrayAccessExpression(val expression: Expression, val index: Expression, val lvalue: Boolean) : Expression() {
|
||||
override fun toKotlin() = expression.toKotlin() +
|
||||
(if (!lvalue && expression.isNullable) "!!" else "") +
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.*
|
||||
import java.util.ArrayList
|
||||
|
||||
@@ -35,7 +34,7 @@ open class Field(
|
||||
modifierList.add(Modifier.ABSTRACT)
|
||||
}
|
||||
|
||||
val modifier = accessModifier()
|
||||
val modifier = modifiers.accessModifier()
|
||||
if (modifier != null) {
|
||||
modifierList.add(modifier)
|
||||
}
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import java.util.ArrayList
|
||||
import org.jetbrains.jet.j2k.ast.types.isUnit
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
open class Function(
|
||||
@@ -28,7 +26,7 @@ open class Function(
|
||||
modifiers: Set<Modifier>,
|
||||
val `type`: Type,
|
||||
val typeParameterList: TypeParameterList,
|
||||
val params: Element,
|
||||
val parameterList: ParameterList,
|
||||
var block: Block?
|
||||
) : Member(comments, modifiers) {
|
||||
|
||||
@@ -39,7 +37,7 @@ open class Function(
|
||||
resultingModifiers.add(Modifier.OVERRIDE)
|
||||
}
|
||||
|
||||
val accessModifier = accessModifier()
|
||||
val accessModifier = modifiers.accessModifier()
|
||||
if (accessModifier != null && !isOverride) {
|
||||
resultingModifiers.add(accessModifier)
|
||||
}
|
||||
@@ -49,10 +47,10 @@ open class Function(
|
||||
}
|
||||
|
||||
if (converter.settings.openByDefault &&
|
||||
!modifiers.contains(Modifier.ABSTRACT) &&
|
||||
!isOverride &&
|
||||
!modifiers.contains(Modifier.FINAL) &&
|
||||
!modifiers.contains(Modifier.PRIVATE)) {
|
||||
!modifiers.contains(Modifier.ABSTRACT) &&
|
||||
!isOverride &&
|
||||
!modifiers.contains(Modifier.FINAL) &&
|
||||
!modifiers.contains(Modifier.PRIVATE)) {
|
||||
resultingModifiers.add(Modifier.OPEN)
|
||||
}
|
||||
|
||||
@@ -69,7 +67,7 @@ open class Function(
|
||||
return commentsToKotlin() +
|
||||
modifiersToKotlin() +
|
||||
"fun ${typeParameterList.toKotlin().withSuffix(" ")}${name.toKotlin()}" +
|
||||
"(${params.toKotlin()})" +
|
||||
"(${parameterList.toKotlin()})" +
|
||||
returnTypeToKotlin() +
|
||||
typeParameterList.whereToKotlin() +
|
||||
block?.toKotlin()
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
class LocalVariable(
|
||||
|
||||
@@ -25,10 +25,6 @@ class MemberComments(elements: List<Element>) : WhiteSpaceSeparatedElementList(e
|
||||
}
|
||||
|
||||
abstract class Member(val comments: MemberComments, val modifiers: Set<Modifier>) : Element {
|
||||
fun accessModifier(): Modifier? {
|
||||
return modifiers.find { m -> m == Modifier.PUBLIC || m == Modifier.PROTECTED || m == Modifier.PRIVATE }
|
||||
}
|
||||
|
||||
fun isAbstract(): Boolean = modifiers.contains(Modifier.ABSTRACT)
|
||||
fun isStatic(): Boolean = modifiers.contains(Modifier.STATIC)
|
||||
fun commentsToKotlin(): String = comments.toKotlin()
|
||||
@@ -42,28 +38,28 @@ class MemberList(elements: List<Element>) : WhiteSpaceSeparatedElementList(eleme
|
||||
get() = elements.filter { it is Member }.map { it as Member }
|
||||
}
|
||||
|
||||
class ClassMembers(
|
||||
val primaryConstructor: Constructor?,
|
||||
class ClassMembers private(
|
||||
val primaryConstructor: PrimaryConstructor?,
|
||||
val secondaryConstructors: MemberList,
|
||||
val allMembers: MemberList,
|
||||
val staticMembers: MemberList,
|
||||
val nonStaticMembers: MemberList
|
||||
) {
|
||||
}
|
||||
|
||||
fun parseClassMembers(elements: List<Element>): ClassMembers {
|
||||
val groups = splitInGroups(elements)
|
||||
val constructors = groups.filter { it.member is Constructor }
|
||||
val primaryConstructor = constructors.map { it.member }.find { (it as Constructor).isPrimary }
|
||||
val secondaryConstructors = constructors.filter { !(it.member as Constructor).isPrimary }
|
||||
val nonConstructors = groups.filter { it.member !is Constructor }
|
||||
val staticMembers = nonConstructors.filter { it.member.isStatic() }
|
||||
val nonStaticMembers = nonConstructors.filter { !it.member.isStatic() }
|
||||
return ClassMembers(primaryConstructor as Constructor?,
|
||||
secondaryConstructors.toMemberList(),
|
||||
nonConstructors.toMemberList(),
|
||||
staticMembers.toMemberList(),
|
||||
nonStaticMembers.toMemberList())
|
||||
val nonStaticMembers: MemberList) {
|
||||
class object {
|
||||
public fun fromBodyElements(elements: List<Element>): ClassMembers {
|
||||
val groups = splitInGroups(elements)
|
||||
val constructors = groups.filter { it.member is Constructor }
|
||||
val primaryConstructor = constructors.map { it.member }.filterIsInstance(javaClass<PrimaryConstructor>()).firstOrNull()
|
||||
val secondaryConstructors = constructors.filter { it.member is SecondaryConstructor }
|
||||
val nonConstructors = groups.filter { it.member !is Constructor }
|
||||
val staticMembers = nonConstructors.filter { it.member.isStatic() }
|
||||
val nonStaticMembers = nonConstructors.filter { !it.member.isStatic() }
|
||||
return ClassMembers(primaryConstructor,
|
||||
secondaryConstructors.toMemberList(),
|
||||
nonConstructors.toMemberList(),
|
||||
staticMembers.toMemberList(),
|
||||
nonStaticMembers.toMemberList())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<MemberHolder>.toMemberList() = MemberList(flatMap { it.elements })
|
||||
@@ -74,8 +70,8 @@ private fun splitInGroups(elements: List<Element>): List<MemberHolder> {
|
||||
for (element in elements) {
|
||||
currentGroup.add(element)
|
||||
if (element is Member) {
|
||||
result.add(Pair(element, currentGroup))
|
||||
currentGroup = ArrayList<Element>()
|
||||
result.add(element to currentGroup)
|
||||
currentGroup = ArrayList()
|
||||
}
|
||||
}
|
||||
if (result.isNotEmpty()) {
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import java.util.ArrayList
|
||||
|
||||
open class MethodCallExpression(
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.Modifier.PUBLIC
|
||||
import org.jetbrains.jet.j2k.ast.Modifier.PROTECTED
|
||||
import org.jetbrains.jet.j2k.ast.Modifier.PRIVATE
|
||||
import org.jetbrains.jet.j2k.ast.Modifier.INTERNAL
|
||||
|
||||
enum class Modifier(val name: String) {
|
||||
PUBLIC: Modifier("public")
|
||||
PROTECTED: Modifier("protected")
|
||||
@@ -25,6 +30,25 @@ enum class Modifier(val name: String) {
|
||||
ABSTRACT: Modifier("abstract")
|
||||
FINAL: Modifier("final")
|
||||
OPEN: Modifier("open")
|
||||
NOT_OPEN: Modifier("not open")
|
||||
NOT_OPEN: Modifier("not open") //TODO: drop it
|
||||
OVERRIDE: Modifier("override")
|
||||
|
||||
public fun toKotlin(): String? {
|
||||
return when(this) {
|
||||
INTERNAL -> null
|
||||
NOT_OPEN -> throw IllegalArgumentException()
|
||||
else -> name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val ACCESS_MODIFIERS = setOf(PUBLIC, PROTECTED, PRIVATE, INTERNAL)
|
||||
|
||||
fun Collection<Modifier>.accessModifier(): Modifier? {
|
||||
return firstOrNull { ACCESS_MODIFIERS.contains(it) }
|
||||
}
|
||||
|
||||
fun Collection<Modifier>.toKotlin(): String
|
||||
= if (isNotEmpty()) map { it.toKotlin() }.filterNotNull().makeString(" ") + " " else ""
|
||||
|
||||
|
||||
|
||||
@@ -16,19 +16,31 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.ast.types.VarArg
|
||||
class Parameter(val identifier: Identifier, val `type`: Type, val varVal: Parameter.VarValModifier, val modifiers: Collection<Modifier>) : Expression() {
|
||||
//TODO: merge with modifiers?
|
||||
//TODO: maybe vararg is modifier too?
|
||||
public enum class VarValModifier {
|
||||
None
|
||||
Val
|
||||
Var
|
||||
}
|
||||
|
||||
open class Parameter(val identifier: Identifier, val `type`: Type, val readOnly: Boolean = true) : Expression() {
|
||||
override fun toKotlin(): String {
|
||||
val vararg: String = (if (`type` is VarArg)
|
||||
"vararg "
|
||||
else
|
||||
"")
|
||||
val `var`: String? = (if (readOnly)
|
||||
""
|
||||
else
|
||||
"var ")
|
||||
return vararg + `var` + identifier.toKotlin() + " : " + `type`.toKotlin()
|
||||
val builder = StringBuilder()
|
||||
|
||||
builder.append(modifiers.toKotlin())
|
||||
|
||||
if (`type` is VarArgType) {
|
||||
assert(varVal == VarValModifier.None)
|
||||
builder.append("vararg ")
|
||||
}
|
||||
|
||||
when (varVal) {
|
||||
VarValModifier.Var -> builder.append("var ")
|
||||
VarValModifier.Val -> builder.append("val ")
|
||||
}
|
||||
|
||||
builder.append(identifier.toKotlin()).append(": ").append(`type`.toKotlin())
|
||||
return builder.toString()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
class ReferenceElement(val reference: Identifier, val types: List<Type>) : Element {
|
||||
override fun toKotlin() = reference.toKotlin() + types.toKotlin(", ", "<", ">")
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
class Trait(
|
||||
converter: Converter,
|
||||
@@ -28,11 +27,11 @@ class Trait(
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Element>
|
||||
bodyElements: List<Element>
|
||||
) : Class(converter, name, comments, modifiers, typeParameterList,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
extendsTypes, baseClassParams, implementsTypes, bodyElements) {
|
||||
|
||||
override val TYPE: String
|
||||
override val keyword: String
|
||||
get() = "trait"
|
||||
|
||||
override fun primaryConstructorSignatureToKotlin() = ""
|
||||
|
||||
@@ -16,10 +16,8 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
open class TypeElement(val `type`: Type) : Element {
|
||||
override fun toKotlin() = `type`.toKotlin()
|
||||
|
||||
fun toKotlinNotNull(): String = `type`.convertedToNotNull().toKotlin()
|
||||
fun toKotlinNotNull(): String = `type`.toNotNullType().toKotlin()
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import com.intellij.psi.PsiTypeParameter
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import com.intellij.psi.PsiTypeParameterList
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 2010-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import java.util.ArrayList
|
||||
|
||||
fun Type.isPrimitive(): Boolean = this is PrimitiveType
|
||||
fun Type.isUnit(): Boolean = this == Type.Unit
|
||||
|
||||
abstract class MayBeNullableType(nullable: Boolean, val converter: Converter) : Type {
|
||||
override val isNullable: Boolean = !converter.settings.forceNotNullTypes && nullable
|
||||
}
|
||||
|
||||
trait NotNullType : Type {
|
||||
override val isNullable: Boolean
|
||||
get() = false
|
||||
}
|
||||
|
||||
trait Type : Element {
|
||||
val isNullable: Boolean
|
||||
|
||||
open fun toNotNullType(): Type {
|
||||
if (isNullable) throw UnsupportedOperationException("toNotNullType must be defined")
|
||||
return this
|
||||
}
|
||||
|
||||
protected fun isNullableStr(): String? {
|
||||
return if (isNullable) "?" else ""
|
||||
}
|
||||
|
||||
object Empty : NotNullType {
|
||||
override fun toKotlin(): String = "UNRESOLVED_TYPE"
|
||||
}
|
||||
|
||||
object Unit: NotNullType {
|
||||
override fun toKotlin() = "Unit"
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = other is Type && other.toKotlin() == this.toKotlin()
|
||||
|
||||
override fun hashCode(): Int = toKotlin().hashCode()
|
||||
}
|
||||
|
||||
open class ClassType(val `type`: Identifier, val parameters: List<Element>, nullable: Boolean,
|
||||
converter: Converter) : MayBeNullableType(nullable, converter) {
|
||||
|
||||
override fun toKotlin(): String {
|
||||
// TODO change to map() when KT-2051 is fixed
|
||||
val parametersToKotlin = ArrayList<String>()
|
||||
for (param in parameters) {
|
||||
parametersToKotlin.add(param.toKotlin())
|
||||
}
|
||||
var params: String = if (parametersToKotlin.size() == 0)
|
||||
""
|
||||
else
|
||||
"<" + parametersToKotlin.makeString(", ") + ">"
|
||||
return `type`.toKotlin() + params + isNullableStr()
|
||||
}
|
||||
|
||||
|
||||
override fun toNotNullType(): Type = ClassType(`type`, parameters, false, converter)
|
||||
}
|
||||
|
||||
class ArrayType(
|
||||
val elementType: Type,
|
||||
nullable: Boolean,
|
||||
converter: Converter
|
||||
) : MayBeNullableType(nullable, converter) {
|
||||
override fun toKotlin(): String {
|
||||
if (elementType is PrimitiveType) {
|
||||
return elementType.toKotlin() + "Array" + isNullableStr()
|
||||
}
|
||||
|
||||
return "Array<" + elementType.toKotlin() + ">" + isNullableStr()
|
||||
}
|
||||
|
||||
override fun toNotNullType(): Type = ArrayType(elementType, false, converter)
|
||||
}
|
||||
|
||||
open class InProjectionType(val bound: Type) : NotNullType {
|
||||
override fun toKotlin(): String = "in " + bound.toKotlin()
|
||||
}
|
||||
|
||||
open class OutProjectionType(val bound: Type) : NotNullType {
|
||||
override fun toKotlin(): String = "out " + bound.toKotlin()
|
||||
}
|
||||
|
||||
open class StarProjectionType() : NotNullType {
|
||||
override fun toKotlin(): String = "*"
|
||||
}
|
||||
|
||||
open class PrimitiveType(val `type`: Identifier) : NotNullType {
|
||||
override fun toKotlin(): String = `type`.toKotlin()
|
||||
}
|
||||
|
||||
open class VarArgType(val `type`: Type) : NotNullType {
|
||||
override fun toKotlin(): String = `type`.toKotlin()
|
||||
}
|
||||
@@ -21,9 +21,6 @@ import java.util.ArrayList
|
||||
fun List<Node>.toKotlin(separator: String, prefix: String = "", postfix: String = ""): String
|
||||
= if (isNotEmpty()) map { it.toKotlin() }.makeString(separator, prefix, postfix) else ""
|
||||
|
||||
fun Collection<Modifier>.toKotlin(separator: String = " "): String
|
||||
= if (isNotEmpty()) map { it.name }.makeString(separator) + separator else ""
|
||||
|
||||
fun String.withSuffix(suffix: String): String = if (isEmpty()) "" else this + suffix
|
||||
fun String.withPrefix(prefix: String): String = if (isEmpty()) "" else prefix + this
|
||||
fun Expression.withPrefix(prefix: String): String = if (isEmpty) "" else prefix + toKotlin()
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
class ArrayType(
|
||||
val elementType: Type,
|
||||
nullable: Boolean,
|
||||
converter: Converter
|
||||
) : MayBeNullableType(nullable, converter) {
|
||||
override fun toKotlin(): String {
|
||||
if (elementType is PrimitiveType) {
|
||||
return elementType.toKotlin() + "Array" + isNullableStr()
|
||||
}
|
||||
|
||||
return "Array<" + elementType.toKotlin() + ">" + isNullableStr()
|
||||
}
|
||||
|
||||
override fun convertedToNotNull(): Type = ArrayType(elementType, false, converter)
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.Element
|
||||
import org.jetbrains.jet.j2k.ast.Identifier
|
||||
import java.util.ArrayList
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
open class ClassType(val `type`: Identifier, val parameters: List<Element>, nullable: Boolean,
|
||||
converter: Converter) : MayBeNullableType(nullable, converter) {
|
||||
|
||||
override fun toKotlin(): String {
|
||||
// TODO change to map() when KT-2051 is fixed
|
||||
val parametersToKotlin = ArrayList<String>()
|
||||
for (param in parameters) {
|
||||
parametersToKotlin.add(param.toKotlin())
|
||||
}
|
||||
var params: String = if (parametersToKotlin.size() == 0)
|
||||
""
|
||||
else
|
||||
"<" + parametersToKotlin.makeString(", ") + ">"
|
||||
return `type`.toKotlin() + params + isNullableStr()
|
||||
}
|
||||
|
||||
|
||||
override fun convertedToNotNull(): Type = ClassType(`type`, parameters, false, converter)
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
open class EmptyType() : NotNullType {
|
||||
override fun toKotlin(): String = "UNRESOLVED_TYPE"
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
open class InProjectionType(val bound: Type) : NotNullType {
|
||||
override fun toKotlin(): String = "in " + bound.toKotlin()
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
open class OutProjectionType(val bound: Type) : NotNullType {
|
||||
override fun toKotlin(): String = "out " + bound.toKotlin()
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.Identifier
|
||||
|
||||
open class PrimitiveType(val `type`: Identifier) : NotNullType {
|
||||
override fun toKotlin(): String = `type`.toKotlin()
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
open class StarProjectionType() : NotNullType {
|
||||
override fun toKotlin(): String = "*"
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.Element
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
fun Type.isPrimitive(): Boolean = this is PrimitiveType
|
||||
fun Type.isUnit(): Boolean = this == UnitType
|
||||
|
||||
abstract class MayBeNullableType(nullable: Boolean, val converter: Converter) : Type {
|
||||
override val isNullable: Boolean = !converter.settings.forceNotNullTypes && nullable
|
||||
}
|
||||
|
||||
trait NotNullType : Type {
|
||||
override val isNullable: Boolean
|
||||
get() = false
|
||||
}
|
||||
|
||||
object UnitType: NotNullType {
|
||||
override fun toKotlin() = "Unit"
|
||||
}
|
||||
|
||||
trait Type : Element {
|
||||
val isNullable: Boolean
|
||||
|
||||
open fun convertedToNotNull(): Type {
|
||||
if (isNullable) throw UnsupportedOperationException("convertedToNotNull must be defined")
|
||||
return this
|
||||
}
|
||||
|
||||
protected fun isNullableStr(): String? {
|
||||
return if (isNullable) "?" else ""
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast.types
|
||||
|
||||
open class VarArg(val `type`: Type) : NotNullType {
|
||||
override fun toKotlin(): String = `type`.toKotlin()
|
||||
}
|
||||
@@ -19,16 +19,15 @@ package org.jetbrains.jet.j2k.visitors
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.jet.j2k.*
|
||||
import org.jetbrains.jet.j2k.ast.*
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
class ElementVisitor(public val converter: Converter) : JavaElementVisitor() {
|
||||
public var result: Element = Element.Empty
|
||||
protected set
|
||||
|
||||
override fun visitLocalVariable(variable: PsiLocalVariable) {
|
||||
var kType = converter.convertType(variable.getType(), variable.isAnnotatedAsNotNull())
|
||||
var kType = converter.convertVariableType(variable)
|
||||
if (variable.hasModifierProperty(PsiModifier.FINAL) && variable.getInitializer().isDefinitelyNotNull()) {
|
||||
kType = kType.convertedToNotNull()
|
||||
kType = kType.toNotNullType()
|
||||
}
|
||||
result = LocalVariable(Identifier(variable.getName()!!),
|
||||
converter.convertModifierList(variable.getModifierList()),
|
||||
@@ -68,7 +67,7 @@ class ElementVisitor(public val converter: Converter) : JavaElementVisitor() {
|
||||
}
|
||||
|
||||
override fun visitParameterList(list: PsiParameterList) {
|
||||
result = ParameterList(converter.convertParameterList(list.getParameters()))
|
||||
result = converter.convertParameterList(list)
|
||||
}
|
||||
|
||||
override fun visitComment(comment: PsiComment) {
|
||||
|
||||
@@ -25,9 +25,9 @@ import com.intellij.psi.CommonClassNames.*
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.jet.lang.types.lang.PrimitiveType
|
||||
import org.jetbrains.jet.j2k.*
|
||||
import org.jetbrains.jet.j2k.ast.types.ArrayType
|
||||
|
||||
open class ExpressionVisitor(public val converter: Converter) : JavaElementVisitor() {
|
||||
open class ExpressionVisitor(protected val converter: Converter,
|
||||
private val usageReplacementMap: Map<PsiVariable, String> = mapOf()) : JavaElementVisitor() {
|
||||
public var result: Expression = Expression.Empty
|
||||
protected set
|
||||
|
||||
@@ -141,7 +141,7 @@ open class ExpressionVisitor(public val converter: Converter) : JavaElementVisit
|
||||
}
|
||||
|
||||
protected fun convertMethodCallExpression(expression: PsiMethodCallExpression) {
|
||||
if (!expression.isSuperConstructorCall() || !isInsidePrimaryConstructor(expression)) {
|
||||
if (!expression.isSuperConstructorCall() || !expression.isInsidePrimaryConstructor()) {
|
||||
result = MethodCallExpression(converter.convertExpression(expression.getMethodExpression()),
|
||||
converter.convertArguments(expression),
|
||||
converter.convertTypes(expression.getTypeArguments()),
|
||||
@@ -212,7 +212,7 @@ open class ExpressionVisitor(public val converter: Converter) : JavaElementVisit
|
||||
}
|
||||
|
||||
override fun visitReferenceExpression(expression: PsiReferenceExpression) {
|
||||
val containingConstructor = getContainingConstructor(expression)
|
||||
val containingConstructor = expression.getContainingConstructor()
|
||||
val insideSecondaryConstructor = containingConstructor != null && !containingConstructor.isPrimaryConstructor()
|
||||
val addReceiver = insideSecondaryConstructor && (expression.getReference()?.resolve() as? PsiField)?.getContainingClass() == containingConstructor!!.getContainingClass()
|
||||
|
||||
@@ -231,20 +231,21 @@ open class ExpressionVisitor(public val converter: Converter) : JavaElementVisit
|
||||
identifier = Identifier("size", isNullable)
|
||||
}
|
||||
else if (qualifier == null) {
|
||||
val resolved = expression.getReference()?.resolve()
|
||||
if (resolved is PsiClass) {
|
||||
if (PrimitiveType.values() any { it.getTypeName().asString() == resolved.getName() }) {
|
||||
result = Identifier(resolved.getQualifiedName()!!, false)
|
||||
val target = expression.getReference()?.resolve()
|
||||
|
||||
if (target is PsiClass) {
|
||||
if (PrimitiveType.values() any { it.getTypeName().asString() == target.getName() }) {
|
||||
result = Identifier(target.getQualifiedName()!!, false)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (resolved is PsiMember
|
||||
&& resolved.hasModifierProperty(PsiModifier.STATIC)
|
||||
&& resolved.getContainingClass() != null
|
||||
&& PsiTreeUtil.getParentOfType(expression, javaClass<PsiClass>()) != resolved.getContainingClass()
|
||||
&& !isStaticallyImported(resolved, expression)) {
|
||||
var member = resolved as PsiMember
|
||||
if (target is PsiMember
|
||||
&& target.hasModifierProperty(PsiModifier.STATIC)
|
||||
&& target.getContainingClass() != null
|
||||
&& PsiTreeUtil.getParentOfType(expression, javaClass<PsiClass>()) != target.getContainingClass()
|
||||
&& !isStaticallyImported(target, expression)) {
|
||||
var member = target as PsiMember
|
||||
var code = Identifier(referencedName).toKotlin()
|
||||
while (member.getContainingClass() != null) {
|
||||
code = Identifier(member.getContainingClass()!!.getName()!!).toKotlin() + "." + code
|
||||
@@ -253,6 +254,13 @@ open class ExpressionVisitor(public val converter: Converter) : JavaElementVisit
|
||||
result = Identifier(code, false, false)
|
||||
return
|
||||
}
|
||||
|
||||
if (target is PsiVariable) {
|
||||
val replacement = usageReplacementMap[target]
|
||||
if (replacement != null) {
|
||||
identifier = Identifier(replacement, isNullable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = CallChainExpression(converter.convertExpression(qualifier), identifier)
|
||||
@@ -268,19 +276,13 @@ open class ExpressionVisitor(public val converter: Converter) : JavaElementVisit
|
||||
}
|
||||
|
||||
override fun visitSuperExpression(expression: PsiSuperExpression) {
|
||||
val qualifier: PsiJavaCodeReferenceElement? = expression.getQualifier()
|
||||
result = SuperExpression((if (qualifier != null)
|
||||
Identifier(qualifier.getQualifiedName()!!)
|
||||
else
|
||||
Identifier.Empty))
|
||||
val qualifier = expression.getQualifier()
|
||||
result = SuperExpression(if (qualifier != null) Identifier(qualifier.getQualifiedName()!!) else Identifier.Empty)
|
||||
}
|
||||
|
||||
override fun visitThisExpression(expression: PsiThisExpression) {
|
||||
val qualifier: PsiJavaCodeReferenceElement? = expression.getQualifier()
|
||||
result = ThisExpression((if (qualifier != null)
|
||||
Identifier(qualifier.getQualifiedName()!!)
|
||||
else
|
||||
Identifier.Empty))
|
||||
val qualifier = expression.getQualifier()
|
||||
result = ThisExpression(if (qualifier != null) Identifier(qualifier.getQualifiedName()!!) else Identifier.Empty)
|
||||
}
|
||||
|
||||
override fun visitTypeCastExpression(expression: PsiTypeCastExpression) {
|
||||
|
||||
+8
-14
@@ -23,16 +23,17 @@ import org.jetbrains.jet.j2k.ast.Identifier
|
||||
import com.intellij.psi.CommonClassNames.JAVA_LANG_OBJECT
|
||||
import org.jetbrains.jet.j2k.ast.MethodCallExpression
|
||||
|
||||
open class ExpressionVisitorForDirectObjectInheritors(converter: Converter) : ExpressionVisitor(converter) {
|
||||
open class ExpressionVisitorForDirectObjectInheritors(converter: Converter, usageReplacementMap: Map<PsiVariable, String> = mapOf())
|
||||
: ExpressionVisitor(converter, usageReplacementMap) {
|
||||
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
|
||||
val methodExpression = expression.getMethodExpression()
|
||||
if (superMethodInvocation(methodExpression, "hashCode")) {
|
||||
if (isSuperMethodInvocation(methodExpression, "hashCode")) {
|
||||
result = MethodCallExpression.build(Identifier("System", false), "identityHashCode", listOf(Identifier("this")))
|
||||
}
|
||||
else if (superMethodInvocation(methodExpression, "equals")) {
|
||||
else if (isSuperMethodInvocation(methodExpression, "equals")) {
|
||||
result = MethodCallExpression.build(Identifier("this", false), "identityEquals", converter.convertArguments(expression))
|
||||
}
|
||||
else if (superMethodInvocation(methodExpression, "toString")) {
|
||||
else if (isSuperMethodInvocation(methodExpression, "toString")) {
|
||||
result = DummyStringExpression("getJavaClass<${getClassName(methodExpression)}>.getName() + '@' + Integer.toHexString(hashCode())")
|
||||
}
|
||||
else {
|
||||
@@ -40,14 +41,7 @@ open class ExpressionVisitorForDirectObjectInheritors(converter: Converter) : Ex
|
||||
}
|
||||
}
|
||||
|
||||
private fun superMethodInvocation(expression: PsiReferenceExpression, methodName: String?): Boolean {
|
||||
val referenceName: String? = expression.getReferenceName()
|
||||
val qualifierExpression: PsiExpression? = expression.getQualifierExpression()
|
||||
if (referenceName == methodName && qualifierExpression is PsiSuperExpression) {
|
||||
if (qualifierExpression.getType()?.getCanonicalText() == JAVA_LANG_OBJECT) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
private fun isSuperMethodInvocation(expression: PsiReferenceExpression, methodName: String): Boolean
|
||||
= expression.getReferenceName() == methodName
|
||||
&& (expression.getQualifierExpression() as? PsiSuperExpression)?.getType()?.getCanonicalText() == JAVA_LANG_OBJECT
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class StatementVisitor(public val converter: Converter) : JavaElementVisitor() {
|
||||
}
|
||||
|
||||
override fun visitBlockStatement(statement: PsiBlockStatement) {
|
||||
result = converter.convertBlock(statement.getCodeBlock(), true)
|
||||
result = converter.convertBlock(statement.getCodeBlock())
|
||||
}
|
||||
|
||||
override fun visitBreakStatement(statement: PsiBreakStatement) {
|
||||
@@ -217,10 +217,10 @@ class StatementVisitor(public val converter: Converter) : JavaElementVisitor() {
|
||||
val catchBlockParameters = statement.getCatchBlockParameters()
|
||||
for (i in 0..catchBlocks.size - 1) {
|
||||
catches.add(CatchStatement(converter.convertParameter(catchBlockParameters[i], true),
|
||||
converter.convertBlock(catchBlocks[i], true)))
|
||||
converter.convertBlock(catchBlocks[i])))
|
||||
}
|
||||
result = TryStatement(converter.convertBlock(statement.getTryBlock(), true),
|
||||
catches, converter.convertBlock(statement.getFinallyBlock(), true))
|
||||
result = TryStatement(converter.convertBlock(statement.getTryBlock()),
|
||||
catches, converter.convertBlock(statement.getFinallyBlock()))
|
||||
}
|
||||
|
||||
override fun visitWhileStatement(statement: PsiWhileStatement) {
|
||||
|
||||
@@ -20,7 +20,6 @@ import com.intellij.psi.*
|
||||
import com.intellij.psi.impl.source.PsiClassReferenceType
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.*
|
||||
import org.jetbrains.jet.j2k.ast.types.*
|
||||
import java.util.LinkedList
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import java.util.ArrayList
|
||||
@@ -32,7 +31,7 @@ open class TypeVisitor(private val converter: Converter) : PsiTypeVisitor<Type>(
|
||||
override fun visitPrimitiveType(primitiveType: PsiPrimitiveType): Type {
|
||||
val name = primitiveType.getCanonicalText()
|
||||
return if (name == "void") {
|
||||
UnitType
|
||||
Type.Unit
|
||||
}
|
||||
else if (PRIMITIVE_TYPES_NAMES.contains(name)) {
|
||||
PrimitiveType(Identifier(StringUtil.capitalize(name)))
|
||||
@@ -120,7 +119,7 @@ open class TypeVisitor(private val converter: Converter) : PsiTypeVisitor<Type>(
|
||||
}
|
||||
|
||||
override fun visitEllipsisType(ellipsisType: PsiEllipsisType): Type {
|
||||
return VarArg(converter.convertType(ellipsisType.getComponentType()))
|
||||
return VarArgType(converter.convertType(ellipsisType.getComponentType()))
|
||||
}
|
||||
|
||||
private fun createQualifiedName(classType: PsiClassType): String {
|
||||
|
||||
@@ -42,10 +42,7 @@ import java.io.StringReader
|
||||
public abstract class AbstractJavaToKotlinConverterPluginTest() : AbstractJavaToKotlinConverterTest("ide.kt", PluginSettings)
|
||||
public abstract class AbstractJavaToKotlinConverterBasicTest() : AbstractJavaToKotlinConverterTest("kt", TestSettings)
|
||||
|
||||
abstract class AbstractJavaToKotlinConverterTest(
|
||||
val kotlinFileExtension: String,
|
||||
val settings: ConverterSettings
|
||||
) : LightIdeaTestCase() {
|
||||
abstract class AbstractJavaToKotlinConverterTest(val kotlinFileExtension: String, val settings: ConverterSettings ) : LightIdeaTestCase() {
|
||||
|
||||
val testHeaderPattern = Pattern.compile("//(element|expression|statement|method|class|file|comp)\n")
|
||||
|
||||
|
||||
+35
@@ -720,6 +720,41 @@ public class JavaToKotlinConverterBasicTestGenerated extends AbstractJavaToKotli
|
||||
doTest("j2k/tests/testData/ast/constructors/customerBuilder.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams1.java")
|
||||
public void testFieldsInitializedFromParams1() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams1.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams2.java")
|
||||
public void testFieldsInitializedFromParams2() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams2.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams3.java")
|
||||
public void testFieldsInitializedFromParams3() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams3.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams4.java")
|
||||
public void testFieldsInitializedFromParams4() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams4.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams5.java")
|
||||
public void testFieldsInitializedFromParams5() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams5.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams6.java")
|
||||
public void testFieldsInitializedFromParams6() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams6.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams7.java")
|
||||
public void testFieldsInitializedFromParams7() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams7.java");
|
||||
}
|
||||
|
||||
@TestMetadata("genericIdentifier.java")
|
||||
public void testGenericIdentifier() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/genericIdentifier.java");
|
||||
|
||||
+35
@@ -720,6 +720,41 @@ public class JavaToKotlinConverterPluginTestGenerated extends AbstractJavaToKotl
|
||||
doTest("j2k/tests/testData/ast/constructors/customerBuilder.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams1.java")
|
||||
public void testFieldsInitializedFromParams1() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams1.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams2.java")
|
||||
public void testFieldsInitializedFromParams2() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams2.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams3.java")
|
||||
public void testFieldsInitializedFromParams3() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams3.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams4.java")
|
||||
public void testFieldsInitializedFromParams4() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams4.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams5.java")
|
||||
public void testFieldsInitializedFromParams5() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams5.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams6.java")
|
||||
public void testFieldsInitializedFromParams6() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams6.java");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldsInitializedFromParams7.java")
|
||||
public void testFieldsInitializedFromParams7() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/fieldsInitializedFromParams7.java");
|
||||
}
|
||||
|
||||
@TestMetadata("genericIdentifier.java")
|
||||
public void testGenericIdentifier() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/constructors/genericIdentifier.java");
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
class C(arg1: Int) {
|
||||
val myArg1: Int
|
||||
class C(val myArg1: Int) {
|
||||
var myArg2: Int = 0
|
||||
var myArg3: Int = 0
|
||||
|
||||
{
|
||||
myArg1 = arg1
|
||||
myArg2 = 0
|
||||
myArg3 = 0
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
open class C(arg1: Int) {
|
||||
val myArg1: Int
|
||||
open class C(val myArg1: Int) {
|
||||
var myArg2: Int = 0
|
||||
var myArg3: Int = 0
|
||||
|
||||
{
|
||||
myArg1 = arg1
|
||||
myArg2 = 0
|
||||
myArg3 = 0
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.test.customer
|
||||
|
||||
class Customer(first: String, last: String) {
|
||||
public val _firstName: String
|
||||
public val _lastName: String
|
||||
class Customer(public val _firstName: String, public val _lastName: String) {
|
||||
|
||||
public fun getFirstName(): String {
|
||||
return _firstName
|
||||
@@ -19,8 +17,6 @@ class Customer(first: String, last: String) {
|
||||
|
||||
{
|
||||
doSmthBefore()
|
||||
_firstName = first
|
||||
_lastName = last
|
||||
doSmthAfter()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.test.customer
|
||||
|
||||
open class Customer(first: String?, last: String?) {
|
||||
public val _firstName: String?
|
||||
public val _lastName: String?
|
||||
open class Customer(public val _firstName: String?, public val _lastName: String?) {
|
||||
|
||||
public open fun getFirstName(): String? {
|
||||
return _firstName
|
||||
@@ -19,8 +17,6 @@ open class Customer(first: String?, last: String?) {
|
||||
|
||||
{
|
||||
doSmthBefore()
|
||||
_firstName = first
|
||||
_lastName = last
|
||||
doSmthAfter()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
class C(private val p1: Int, private val myP2: Int, public var p3: Int) {}
|
||||
@@ -0,0 +1,12 @@
|
||||
//file
|
||||
class C {
|
||||
private final int p1;
|
||||
private final int myP2;
|
||||
public int p3;
|
||||
|
||||
public C(int p1, int p2, int p3) {
|
||||
this.p1 = p1;
|
||||
myP2 = p2;
|
||||
this.p3 = p3;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
open class C(private val p1: Int, private val myP2: Int, public var p3: Int) {}
|
||||
@@ -0,0 +1,5 @@
|
||||
class C(private val field: Int) {
|
||||
{
|
||||
System.out.println(field)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//file
|
||||
class C {
|
||||
private final int field;
|
||||
|
||||
public C(int p) {
|
||||
field = p
|
||||
System.out.println(p);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
open class C(private val field: Int) {
|
||||
{
|
||||
System.out?.println(field)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class C(p: Int) {
|
||||
private val p: Int
|
||||
|
||||
{
|
||||
this.p = p
|
||||
System.out.println(p++)
|
||||
System.out.println(p)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
//file
|
||||
class C {
|
||||
private final int p;
|
||||
|
||||
public C(int p) {
|
||||
this.p = p
|
||||
System.out.println(p++);
|
||||
System.out.println(p);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
open class C(p: Int) {
|
||||
private val p: Int
|
||||
|
||||
{
|
||||
this.p = p
|
||||
System.out?.println(p++)
|
||||
System.out?.println(p)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class C(p: Int, c: C) {
|
||||
public var p: Int = 0
|
||||
|
||||
{
|
||||
c.p = p
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//file
|
||||
class C {
|
||||
public int p;
|
||||
|
||||
public C(int p, C c) {
|
||||
c.p = p;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
open class C(p: Int, c: C?) {
|
||||
public var p: Int = 0
|
||||
|
||||
{
|
||||
c?.p = p
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class C(p: Int) {
|
||||
public var p: Int = 0
|
||||
|
||||
{
|
||||
this.p = 0
|
||||
if (p > 0) {
|
||||
this.p = p
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//file
|
||||
class C {
|
||||
public int p;
|
||||
|
||||
public C(int p) {
|
||||
this.p = 0
|
||||
if (p > 0) {
|
||||
this.p = p
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
open class C(p: Int) {
|
||||
public var p: Int = 0
|
||||
|
||||
{
|
||||
this.p = 0
|
||||
if (p > 0) {
|
||||
this.p = p
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class C(x: String) {
|
||||
public var x: Any = 0
|
||||
|
||||
{
|
||||
this.x = x
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//file
|
||||
class C {
|
||||
public Object x;
|
||||
|
||||
public C(String x) {
|
||||
this.x = x;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
open class C(x: String?) {
|
||||
public var x: Any? = null
|
||||
|
||||
{
|
||||
this.x = x
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class C(x: Any, b: Boolean) {
|
||||
public var x: Any = 0
|
||||
|
||||
{
|
||||
if (b) {
|
||||
this.x = x
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
//file
|
||||
class C {
|
||||
public Object x;
|
||||
|
||||
public C(Object x, boolean b) {
|
||||
if (b) {
|
||||
this.x = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
open class C(x: Any?, b: Boolean) {
|
||||
public var x: Any? = null
|
||||
|
||||
{
|
||||
if (b) {
|
||||
this.x = x
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,10 @@
|
||||
package demo
|
||||
|
||||
enum class MyEnum(_color: Int) {
|
||||
enum class MyEnum(private val color: Int) {
|
||||
RED : MyEnum(10)
|
||||
BLUE : MyEnum(20)
|
||||
|
||||
private val color: Int
|
||||
|
||||
public fun getColor(): Int {
|
||||
return color
|
||||
}
|
||||
|
||||
{
|
||||
color = _color
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,10 @@
|
||||
package demo
|
||||
|
||||
enum class MyEnum(_color: Int) {
|
||||
enum class MyEnum(private val color: Int) {
|
||||
RED : MyEnum(10)
|
||||
BLUE : MyEnum(20)
|
||||
|
||||
private val color: Int
|
||||
|
||||
public fun getColor(): Int {
|
||||
return color
|
||||
}
|
||||
|
||||
{
|
||||
color = _color
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,11 @@
|
||||
enum class Color(c: Int) {
|
||||
enum class Color(private var code: Int) {
|
||||
WHITE : Color(21)
|
||||
BLACK : Color(22)
|
||||
RED : Color(23)
|
||||
YELLOW : Color(24)
|
||||
BLUE : Color(25)
|
||||
|
||||
private var code: Int = 0
|
||||
|
||||
public fun getCode(): Int {
|
||||
return code
|
||||
}
|
||||
|
||||
{
|
||||
code = c
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
//class
|
||||
enum Color {
|
||||
WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);
|
||||
WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);
|
||||
|
||||
private int code;
|
||||
private int code;
|
||||
|
||||
private Color(int c) {
|
||||
code = c;
|
||||
}
|
||||
private Color(int c) {
|
||||
code = c;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,11 @@
|
||||
enum class Color(c: Int) {
|
||||
enum class Color(private var code: Int) {
|
||||
WHITE : Color(21)
|
||||
BLACK : Color(22)
|
||||
RED : Color(23)
|
||||
YELLOW : Color(24)
|
||||
BLUE : Color(25)
|
||||
|
||||
private var code: Int = 0
|
||||
|
||||
public fun getCode(): Int {
|
||||
return code
|
||||
}
|
||||
|
||||
{
|
||||
code = c
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,8 @@
|
||||
package demo
|
||||
|
||||
enum class Color(c: Int) {
|
||||
private var code: Int = 0
|
||||
enum class Color(private var code: Int) {
|
||||
|
||||
public fun getCode(): Int {
|
||||
return code
|
||||
}
|
||||
|
||||
{
|
||||
code = c
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,8 @@
|
||||
package demo
|
||||
|
||||
enum class Color(c: Int) {
|
||||
private var code: Int = 0
|
||||
enum class Color(private var code: Int) {
|
||||
|
||||
public fun getCode(): Int {
|
||||
return code
|
||||
}
|
||||
|
||||
{
|
||||
code = c
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package demo
|
||||
|
||||
class Test() {
|
||||
fun test(vararg var args: Any) {
|
||||
fun test(vararg args: Any) {
|
||||
args = array<Int>(1, 2, 3)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package demo
|
||||
|
||||
open class Test() {
|
||||
open fun test(vararg var args: Any?) {
|
||||
open fun test(vararg args: Any?) {
|
||||
args = array<Int?>(1, 2, 3)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package demo
|
||||
|
||||
class Test() {
|
||||
fun test(var i: Int): Int {
|
||||
fun test(i: Int): Int {
|
||||
i = 10
|
||||
return i + 20
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package demo
|
||||
|
||||
open class Test() {
|
||||
open fun test(var i: Int): Int {
|
||||
open fun test(i: Int): Int {
|
||||
i = 10
|
||||
return i + 20
|
||||
}
|
||||
|
||||
@@ -2,14 +2,9 @@ class `$$$$$`() {}
|
||||
|
||||
class `$`() {}
|
||||
|
||||
class `$$`(`$$$$`: `$$$$$`) : `$`() {
|
||||
val `$$$`: `$$$$$`
|
||||
class `$$`(val `$$$`: `$$$$$`) : `$`() {
|
||||
|
||||
public fun `$$$$$$`(): `$$$$$` {
|
||||
return `$$$`
|
||||
}
|
||||
|
||||
{
|
||||
`$$$` = `$$$$`
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,9 @@ open class `$$$$$`() {}
|
||||
|
||||
open class `$`() {}
|
||||
|
||||
open class `$$`(`$$$$`: `$$$$$`?) : `$`() {
|
||||
val `$$$`: `$$$$$`?
|
||||
open class `$$`(val `$$$`: `$$$$$`?) : `$`() {
|
||||
|
||||
public open fun `$$$$$$`(): `$$$$$`? {
|
||||
return `$$$`
|
||||
}
|
||||
|
||||
{
|
||||
`$$$` = `$$$$`
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,3 @@
|
||||
class Base<T>(name: T) {}
|
||||
|
||||
class One<T, K>(name: T, second: K) : Base<T>(name) {
|
||||
private var mySecond: K = 0
|
||||
|
||||
{
|
||||
mySecond = second
|
||||
}
|
||||
}
|
||||
class One<T, K>(name: T, private var mySecond: K) : Base<T>(name) {}
|
||||
@@ -1,9 +1,3 @@
|
||||
open class Base<T>(name: T?) {}
|
||||
|
||||
open class One<T, K>(name: T?, second: K?) : Base<T?>(name) {
|
||||
private var mySecond: K? = null
|
||||
|
||||
{
|
||||
mySecond = second
|
||||
}
|
||||
}
|
||||
open class One<T, K>(name: T?, private var mySecond: K?) : Base<T?>(name) {}
|
||||
+1
-7
@@ -1,9 +1,3 @@
|
||||
class Base(name: String) {}
|
||||
|
||||
class One(name: String, second: String) : Base(name) {
|
||||
private var mySecond: String = 0
|
||||
|
||||
{
|
||||
mySecond = second
|
||||
}
|
||||
}
|
||||
class One(name: String, private var mySecond: String) : Base(name) {}
|
||||
+1
-7
@@ -1,9 +1,3 @@
|
||||
open class Base(name: String?) {}
|
||||
|
||||
open class One(name: String?, second: String?) : Base(name) {
|
||||
private var mySecond: String? = null
|
||||
|
||||
{
|
||||
mySecond = second
|
||||
}
|
||||
}
|
||||
open class One(name: String?, private var mySecond: String?) : Base(name) {}
|
||||
@@ -1,9 +1,3 @@
|
||||
package demo
|
||||
|
||||
class C(i: Int) {
|
||||
private val i: Int
|
||||
|
||||
{
|
||||
this.i = i
|
||||
}
|
||||
}
|
||||
class C(private val i: Int) {}
|
||||
@@ -1,9 +1,3 @@
|
||||
package demo
|
||||
|
||||
open class C(i: Int) {
|
||||
private val i: Int
|
||||
|
||||
{
|
||||
this.i = i
|
||||
}
|
||||
}
|
||||
open class C(private val i: Int) {}
|
||||
@@ -2,16 +2,11 @@ package com.voltvoodoo.saplo4j.model
|
||||
|
||||
import java.io.Serializable
|
||||
|
||||
public class Language(code: String) : Serializable {
|
||||
protected var code: String = 0
|
||||
public class Language(protected var code: String) : Serializable {
|
||||
|
||||
override fun toString(): String {
|
||||
return this.code
|
||||
}
|
||||
|
||||
{
|
||||
this.code = code
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,16 +2,11 @@ package com.voltvoodoo.saplo4j.model
|
||||
|
||||
import java.io.Serializable
|
||||
|
||||
public open class Language(code: String?) : Serializable {
|
||||
protected var code: String? = null
|
||||
public open class Language(protected var code: String?) : Serializable {
|
||||
|
||||
override fun toString(): String? {
|
||||
return this.code
|
||||
}
|
||||
|
||||
{
|
||||
this.code = code
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,21 +2,16 @@ package com.voltvoodoo.saplo4j.model
|
||||
|
||||
import java.io.Serializable
|
||||
|
||||
public class Language(code: String) : Serializable {
|
||||
|
||||
protected var code: String = 0
|
||||
public class Language(protected var code: String) : Serializable {
|
||||
|
||||
public fun equals(other: Language): Boolean {
|
||||
return other.toString().equals(this.toString())
|
||||
}
|
||||
|
||||
{
|
||||
this.code = code
|
||||
}
|
||||
|
||||
class object {
|
||||
public var ENGLISH: Language = Language("en")
|
||||
public var SWEDISH: Language = Language("sv")
|
||||
|
||||
private val serialVersionUID: Long = -2442762969929206780
|
||||
}
|
||||
}
|
||||
@@ -2,21 +2,16 @@ package com.voltvoodoo.saplo4j.model
|
||||
|
||||
import java.io.Serializable
|
||||
|
||||
public open class Language(code: String?) : Serializable {
|
||||
|
||||
protected var code: String? = null
|
||||
public open class Language(protected var code: String?) : Serializable {
|
||||
|
||||
public open fun equals(other: Language?): Boolean {
|
||||
return other?.toString()?.equals(this.toString())!!
|
||||
}
|
||||
|
||||
{
|
||||
this.code = code
|
||||
}
|
||||
|
||||
class object {
|
||||
public var ENGLISH: Language? = Language("en")
|
||||
public var SWEDISH: Language? = Language("sv")
|
||||
|
||||
private val serialVersionUID: Long = -2442762969929206780
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user