Merge pull request #206 from ponomandr/j2k
Small improvements in Java to Kotlin converter
This commit is contained in:
@@ -361,7 +361,7 @@ public class Converter {
|
|||||||
return new Field(
|
return new Field(
|
||||||
new IdentifierImpl(field.getName()), // TODO
|
new IdentifierImpl(field.getName()), // TODO
|
||||||
modifiers,
|
modifiers,
|
||||||
typeToType(field.getType()),
|
typeToType(field.getType(), ConverterUtil.isAnnotatedAsNotNull(field.getModifierList())),
|
||||||
createSureCallOnlyForChain(field.getInitializer(), field.getType()), // TODO: add modifiers
|
createSureCallOnlyForChain(field.getInitializer(), field.getType()), // TODO: add modifiers
|
||||||
countWritingAccesses(field, psiClass)
|
countWritingAccesses(field, psiClass)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class CallChainExpression extends Expression {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isNullable() {
|
public boolean isNullable() {
|
||||||
return myIdentifier.isNullable();
|
return myExpression.isNullable() || myIdentifier.isNullable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import com.intellij.psi.tree.IElementType;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.j2k.Converter;
|
import org.jetbrains.jet.j2k.Converter;
|
||||||
|
import org.jetbrains.jet.j2k.ConverterUtil;
|
||||||
import org.jetbrains.jet.j2k.ast.*;
|
import org.jetbrains.jet.j2k.ast.*;
|
||||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
|
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
|
||||||
|
|
||||||
@@ -345,7 +346,8 @@ public class ExpressionVisitor extends StatementVisitor {
|
|||||||
final boolean insideSecondaryConstructor = isInsideSecondaryConstructor(expression);
|
final boolean insideSecondaryConstructor = isInsideSecondaryConstructor(expression);
|
||||||
final boolean hasReceiver = isFieldReference && insideSecondaryConstructor;
|
final boolean hasReceiver = isFieldReference && insideSecondaryConstructor;
|
||||||
final boolean isThis = isThisExpression(expression);
|
final boolean isThis = isThisExpression(expression);
|
||||||
final boolean isNullable = getConverter().typeToType(expression.getType()).isNullable();
|
final boolean notNull = isResolvedToNotNull(expression);
|
||||||
|
final boolean isNullable = getConverter().typeToType(expression.getType(), notNull).isNullable();
|
||||||
final String className = getClassNameWithConstructor(expression);
|
final String className = getClassNameWithConstructor(expression);
|
||||||
|
|
||||||
Expression identifier = new IdentifierImpl(expression.getReferenceName(), isNullable);
|
Expression identifier = new IdentifierImpl(expression.getReferenceName(), isNullable);
|
||||||
@@ -364,6 +366,17 @@ public class ExpressionVisitor extends StatementVisitor {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isResolvedToNotNull(PsiReference expression) {
|
||||||
|
PsiElement target = expression.resolve();
|
||||||
|
if (target instanceof PsiEnumConstant) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (target instanceof PsiModifierListOwner) {
|
||||||
|
return ConverterUtil.isAnnotatedAsNotNull(((PsiModifierListOwner) target).getModifierList());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static String getClassNameWithConstructor(@NotNull PsiReferenceExpression expression) {
|
private static String getClassNameWithConstructor(@NotNull PsiReferenceExpression expression) {
|
||||||
PsiElement context = expression.getContext();
|
PsiElement context = expression.getContext();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package test
|
package test
|
||||||
public open class Test(str : String) {
|
public open class Test(str : String) {
|
||||||
var myStr : String? = "String2"
|
var myStr : String = "String2"
|
||||||
public open fun sout(str : String) : Unit {
|
public open fun sout(str : String) : Unit {
|
||||||
System.out?.println(str)
|
System.out?.println(str)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
void execute() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Bar {
|
||||||
|
@NotNull
|
||||||
|
Foo fooNotNull = Foo();
|
||||||
|
Foo fooNullable = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
public void test(@NotNull Bar barNotNull, Bar barNullable) {
|
||||||
|
barNotNull.fooNotNull.execute();
|
||||||
|
barNotNull.fooNullable.execute();
|
||||||
|
barNullable.fooNotNull.execute();
|
||||||
|
barNullable.fooNullable.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package test
|
||||||
|
open class Foo() {
|
||||||
|
open fun execute() : Unit {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
open class Bar() {
|
||||||
|
var fooNotNull : Foo = Foo()
|
||||||
|
var fooNullable : Foo? = null
|
||||||
|
}
|
||||||
|
open class Test() {
|
||||||
|
public open fun test(barNotNull : Bar, barNullable : Bar?) : Unit {
|
||||||
|
barNotNull.fooNotNull.execute()
|
||||||
|
barNotNull.fooNullable?.execute()
|
||||||
|
barNullable?.fooNotNull?.execute()
|
||||||
|
barNullable?.fooNullable?.execute()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
enum E {
|
||||||
|
FOO;
|
||||||
|
|
||||||
|
void foo() {
|
||||||
|
FOO.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
enum class E {
|
||||||
|
FOO
|
||||||
|
fun foo() : Unit {
|
||||||
|
FOO.toString()
|
||||||
|
}
|
||||||
|
public fun name() : String { return "" }
|
||||||
|
public fun order() : Int { return 0 }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user