Java to Kotlin converter: minor corrections after code review

This commit is contained in:
Valentin Kipyatkov
2014-06-06 20:49:18 +04:00
parent f652c50c8c
commit e947ad7294
32 changed files with 145 additions and 56 deletions
+7 -10
View File
@@ -89,19 +89,16 @@ class TypeConverter(val settings: ConverterSettings) {
var nullability = method.nullabilityFromAnnotations()
if (nullability == Nullability.Default) {
var isInAnonymousClass = false
method.getBody()?.accept(object: JavaRecursiveElementVisitor() {
override fun visitAnonymousClass(aClass: PsiAnonymousClass) {
isInAnonymousClass = true
super.visitAnonymousClass(aClass)
isInAnonymousClass = false
}
override fun visitReturnStatement(statement: PsiReturnStatement) {
if (!isInAnonymousClass && statement.getReturnValue()?.nullability() == Nullability.Nullable) {
if (statement.getReturnValue()?.nullability() == Nullability.Nullable) {
nullability = Nullability.Nullable
}
}
override fun visitMethod(method: PsiMethod) {
// do not go inside any other method (e.g. in anonymous class)
}
})
}
@@ -129,7 +126,7 @@ class TypeConverter(val settings: ConverterSettings) {
private fun PsiExpression.nullability(): Nullability {
return when (this) {
is PsiLiteralExpression -> if (getValue() != null) Nullability.NotNull else Nullability.Nullable
is PsiLiteralExpression -> if (getType() != PsiType.NULL) Nullability.NotNull else Nullability.Nullable
is PsiNewExpression -> Nullability.NotNull
@@ -159,7 +156,7 @@ class TypeConverter(val settings: ConverterSettings) {
val operationType = parent.getOperationTokenType()
if (operationType == JavaTokenType.EQEQ || operationType == JavaTokenType.NE) {
val otherOperand = if (usage == parent.getLOperand()) parent.getROperand() else parent.getLOperand()
return otherOperand?.nullability() == Nullability.Nullable
return otherOperand is PsiLiteralExpression && otherOperand.getType() == PsiType.NULL
}
}
return false
@@ -16,7 +16,7 @@
package org.jetbrains.jet.j2k.ast
open class EnumConstant(
class EnumConstant(
identifier: Identifier,
members: MemberComments,
modifiers: Set<Modifier>,
+10 -12
View File
@@ -29,7 +29,15 @@ open class Field(
val writingAccesses: Int
) : Member(comments, modifiers) {
fun modifiersToKotlin(): String {
override fun toKotlin(): String {
val declaration = commentsToKotlin() + modifiersToKotlin() + (if (isVal) "val " else "var ") + identifier.toKotlin() + " : " + `type`.toKotlin()
return if (initializer.isEmpty)
declaration + (if (isVal && !isStatic() && writingAccesses != 0) "" else " = " + getDefaultInitializer(this))
else
declaration + " = " + initializer.toKotlin()
}
private fun modifiersToKotlin(): String {
val modifierList = ArrayList<Modifier>()
if (modifiers.contains(Modifier.ABSTRACT)) {
modifierList.add(Modifier.ABSTRACT)
@@ -37,16 +45,6 @@ open class Field(
modifiers.accessModifier()?.let { modifierList.add(it) }
return modifierList.toKotlin() + (if (isVal) "val " else "var ")
}
override fun toKotlin(): String {
val declaration = commentsToKotlin() + modifiersToKotlin() + identifier.toKotlin() + " : " + `type`.toKotlin()
if (initializer.isEmpty) {
return declaration + (if (isVal && !isStatic() && writingAccesses != 0) "" else " = " + getDefaultInitializer(this))
}
else {
return declaration + " = " + initializer.toKotlin()
}
return modifierList.toKotlin()
}
}
+1 -1
View File
@@ -63,7 +63,7 @@ private fun filterImport(name: String, ref: PsiJavaCodeReferenceElement): String
if (!JavaToKotlinClassMap.getInstance().mapPlatformClass(FqName(name)).isEmpty()) return null
val target = ref.resolve()
if (target != null && target is KotlinLightClassForPackage) {
if (target is KotlinLightClassForPackage) {
return quoteKeywords(target.getFqName().parent().toString()) + ".*"
}
@@ -33,6 +33,7 @@ import org.jetbrains.jet.lang.psi.psiUtil.getParentByType
import org.jetbrains.jet.lang.psi.psiUtil.isExtensionDeclaration
import org.jetbrains.jet.lang.psi.JetPropertyAccessor
import com.intellij.psi.impl.light.LightField
import org.jetbrains.jet.lang.resolve.java.JvmAbi
class ExpressionVisitor(private val converter: Converter,
private val typeConverter: TypeConverter,
@@ -159,15 +160,12 @@ class ExpressionVisitor(private val converter: Converter,
val parameterCount = target.getParameterList().getParameters().size
if (parameterCount == arguments.size) {
val propertyName = Identifier(property.getName()!!, false)
var isExtension = false
val isExtension = property.isExtensionDeclaration()
val propertyAccess = if (isTopLevel) {
if (property.isExtensionDeclaration()) {
isExtension = true
if (isExtension)
QualifiedExpression(converter.convertExpression(arguments.firstOrNull()), propertyName)
}
else {
else
propertyName
}
}
else {
QualifiedExpression(converter.convertExpression(methodExpr.getQualifierExpression()), propertyName)
@@ -312,7 +310,7 @@ class ExpressionVisitor(private val converter: Converter,
identifier = Identifier("size", isNullable)
}
else if (qualifier != null) {
if (referencedName == "object$" || referencedName == "instance$") {
if (referencedName == JvmAbi.CLASS_OBJECT_FIELD || referencedName == JvmAbi.INSTANCE_FIELD) {
val target = expression.getReference()?.resolve()
if (target is LightField) { //TODO: should be KotlinLightField with check of origin here, see KT-5188
result = converter.convertExpression(qualifier)
@@ -1245,6 +1245,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
doTest("j2k/tests/testData/ast/function/overrideObject.java");
}
@TestMetadata("overrideObject2.java")
public void testOverrideObject2() throws Exception {
doTest("j2k/tests/testData/ast/function/overrideObject2.java");
}
@TestMetadata("ownGenericParam.java")
public void testOwnGenericParam() throws Exception {
doTest("j2k/tests/testData/ast/function/ownGenericParam.java");
@@ -2176,6 +2181,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("j2k/tests/testData/ast/settings"), Pattern.compile("^(.+)\\.java$"), true);
}
@TestMetadata("forceLocalVariableImmutability.java")
public void testForceLocalVariableImmutability() throws Exception {
doTest("j2k/tests/testData/ast/settings/forceLocalVariableImmutability.java");
}
@TestMetadata("openByDefault.java")
public void testOpenByDefault() throws Exception {
doTest("j2k/tests/testData/ast/settings/openByDefault.java");
+1 -1
View File
@@ -4,7 +4,7 @@ public open class KotlinClass {
public var property: String = ""
class object {
public fun statucFun(p: Int): Int = p
public fun staticFun(p: Int): Int = p
public var staticVar: Int = 1
public var staticProperty: Int
get() = 1
@@ -0,0 +1,24 @@
//file
class Base {}
class X extends Base {
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object o) {
return super.equals(o);
}
@Override
public String toString() {
return super.toString();
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
@@ -0,0 +1,19 @@
class Base()
class X() : Base() {
override fun hashCode(): Int {
return super.hashCode()
}
override fun equals(other: Any?): Boolean {
return super.equals(o)
}
override fun toString(): String {
return super.toString()
}
protected fun clone(): Any {
return super.clone()
}
}
@@ -0,0 +1,14 @@
//file
class Base {
@Override
public boolean equals(Object o) {
return super.equals(o);
}
}
class X extends Base {
@Override
public boolean equals(Object o) {
return super.equals(o);
}
}
@@ -0,0 +1,11 @@
class Base() {
override fun equals(other: Any?): Boolean {
return super.equals(o)
}
}
class X() : Base() {
override fun equals(o: Any?): Boolean {
return super.equals(o)
}
}
@@ -5,6 +5,6 @@ class C {
int foo() {
KotlinClass.staticVar = KotlinClass.staticVar * 2;
KotlinClass.object$.setStaticProperty(KotlinClass.object$.getStaticVar() + KotlinClass.object$.getStaticProperty());
return KotlinClass.object$.statucFun(1);
return KotlinClass.object$.staticFun(1);
}
}
@@ -4,6 +4,6 @@ class C() {
fun foo(): Int {
KotlinClass.staticVar = KotlinClass.staticVar * 2
KotlinClass.staticProperty = KotlinClass.staticVar + KotlinClass.staticProperty
return KotlinClass.statucFun(1)
return KotlinClass.staticFun(1)
}
}
@@ -3,7 +3,7 @@ import kotlinApi.KotlinApiPackage;
class C {
int foo() {
KotlinApiPackage.setGlobalValue1(0)
KotlinApiPackage.setGlobalValue1(0);
return KotlinApiPackage.getGlobalValue1();
}
}
@@ -3,7 +3,7 @@ import kotlinApi.KotlinApiPackage;
class C {
int foo() {
KotlinApiPackage.setGlobalValue2(0)
KotlinApiPackage.setGlobalValue2(0);
return KotlinApiPackage.getGlobalValue2();
}
}
@@ -4,6 +4,6 @@ import kotlinApi.*
class C {
void foo(KotlinClass k) {
System.out.println(k.getProperty());
k.setProperty("a")
k.setProperty("a");
}
}
@@ -3,6 +3,6 @@ class C {
private String s = "";
void foo() {
s = null
s = null;
}
}
@@ -3,6 +3,6 @@ class C {
private String s = null;
void foo() {
s = "x"
s = "x";
}
}
@@ -3,6 +3,6 @@ class C {
private void foo(String s){}
void bar() {
foo(null)
foo(null);
}
}
@@ -3,6 +3,6 @@ class C {
private void foo(String s){}
void bar(boolean b) {
foo(b ? "a" : null)
foo(b ? "a" : null);
}
}
@@ -6,7 +6,7 @@ interface I {
class C {
void foo(I i) {
if (i.getString() == null) {
println("null")
println("null");
}
}
}
@@ -2,10 +2,10 @@
class C {
String foo(boolean b) {
if (b) {
return "abc"
return "abc";
}
else {
return null
return null;
}
}
}
@@ -1,9 +1,6 @@
import java.lang.Override;
import java.lang.String;
//file
interface Getter {
String get()
String get();
}
class C {
@@ -1,5 +1,3 @@
import java.lang.Override
trait Getter {
public fun get(): String
}
@@ -0,0 +1,10 @@
//file
class C {
String foo() {
class Local {
String foo() { return null; }
};
new Local().foo();
return "";
}
}
@@ -1,6 +1,6 @@
//file
class C {
String foo(boolean b) {
return b ? "abc" : null
return b ? "abc" : null;
}
}
@@ -1,6 +1,6 @@
//method
void foo(String s) {
if (s != null) {
zoo(s)
zoo(s);
}
}
@@ -3,6 +3,6 @@
void foo(boolean b) {
String s = "abc";
if (b) {
s = null
s = null;
}
}
@@ -1,8 +1,8 @@
//method
// !specifyLocalVariableTypeByDefault: true
void foo() {
String s = bar()
String s = bar();
if (s != null) {
zoo(s)
zoo(s);
}
}
@@ -3,6 +3,6 @@
void foo(boolean b) {
String s = null;
if (b) {
s = "abc"
s = "abc";
}
}
@@ -0,0 +1,7 @@
//method
// !forceLocalVariableImmutability: false
public void foo() {
int i = 1;
String s = "";
s += "a";
}
@@ -0,0 +1,6 @@
// !forceLocalVariableImmutability: false
public fun foo() {
var i = 1
var s = ""
s += "a"
}