nullable safe calls supported (for methods and instances)

This commit is contained in:
Sergey Ignatov
2011-11-07 15:58:32 +04:00
parent 8f1727882a
commit 510fb25e80
10 changed files with 108 additions and 8 deletions
@@ -8,10 +8,17 @@ import org.jetbrains.annotations.NotNull;
public class MethodCallExpression extends Expression {
private final Expression myMethodCall;
private final Element myParamList;
private boolean myIsResultNullable;
public MethodCallExpression(Expression methodCall, Element paramList) {
public MethodCallExpression(Expression methodCall, Element paramList, boolean nullable) {
myMethodCall = methodCall;
myParamList = paramList;
myIsResultNullable = nullable;
}
@Override
public boolean isNullable() {
return myIsResultNullable;
}
@NotNull
@@ -12,6 +12,11 @@ public class PrimitiveType extends Type {
myType = type;
}
@Override
public boolean isNullable() {
return false;
}
@NotNull
@Override
public String toKotlin() {
+6 -1
View File
@@ -19,7 +19,7 @@ public abstract class Type extends Element {
myNullable = nullable;
}
boolean isNullable() {
public boolean isNullable() {
return myNullable;
}
@@ -36,5 +36,10 @@ public abstract class Type extends Element {
public String toKotlin() {
return "UNRESOLVED_TYPE";
}
@Override
public boolean isNullable() {
return false;
}
}
}
@@ -165,7 +165,8 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
myResult = // TODO: not resolved
new MethodCallExpression(
expressionToExpression(expression.getMethodExpression()),
elementToElement(expression.getArgumentList())
elementToElement(expression.getArgumentList()),
typeToType(expression.getType()).isNullable()
);
}
@@ -225,11 +226,11 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
super.visitReferenceExpression(expression);
final IdentifierImpl identifier = expression.getType() != null ? // TODO: if type exists so id is nullable
new IdentifierImpl(expression.getReferenceName()) :
new IdentifierImpl(expression.getReferenceName(), false);
myResult = new CallChain(expressionToExpression(expression.getQualifierExpression()), identifier);
boolean isNullable = typeToType(expression.getType()).isNullable();
myResult = new CallChain(
expressionToExpression(expression.getQualifierExpression()),
new IdentifierImpl(expression.getReferenceName(), isNullable) // TODO: if type exists so id is nullable
);
}
@Override
@@ -0,0 +1,12 @@
class Library {
static void call() {}
static String getString() { return ""; }
}
class User {
void main() {
Library.call();
Library.getString().isEmpty();
}
}
@@ -0,0 +1,17 @@
namespace {
open class Library {
class object {
fun call() : Unit {
}
fun getString() : String? {
return ""
}
}
}
open class User {
fun main() : Unit {
Library.call()
Library.getString()?.isEmpty()
}
}
}
@@ -0,0 +1,16 @@
class Library {
void call() {}
String getString() { return ""; }
}
class User {
void main() {
Library lib = new Library();
lib.call();
lib.getString().isEmpty();
new Library().call();
new Library().getString().isEmpty();
}
}
@@ -0,0 +1,18 @@
namespace {
open class Library {
fun call() : Unit {
}
fun getString() : String? {
return ""
}
}
open class User {
fun main() : Unit {
var lib : Library? = Library()
lib?.call()
lib?.getString()?.isEmpty()
Library().call()
Library().getString()?.isEmpty()
}
}
}
@@ -0,0 +1,9 @@
class Library {
final public String myString;
}
class User {
void main() {
Library.myString.isEmpty();
}
}
@@ -0,0 +1,10 @@
namespace {
open class Library {
public val myString : String?
}
open class User {
fun main() : Unit {
Library.myString?.isEmpty()
}
}
}