J2K: Detect @NotNull references in call chains

This commit is contained in:
Andrey Ponomarev
2013-02-21 01:46:09 +01:00
parent 514c7b553f
commit e4df4059d1
4 changed files with 51 additions and 2 deletions
@@ -39,7 +39,7 @@ public class CallChainExpression extends Expression {
@Override
public boolean isNullable() {
return myIdentifier.isNullable();
return myExpression.isNullable() || myIdentifier.isNullable();
}
@NotNull
@@ -21,6 +21,7 @@ import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.j2k.Converter;
import org.jetbrains.jet.j2k.ConverterUtil;
import org.jetbrains.jet.j2k.ast.*;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
@@ -345,7 +346,8 @@ public class ExpressionVisitor extends StatementVisitor {
final boolean insideSecondaryConstructor = isInsideSecondaryConstructor(expression);
final boolean hasReceiver = isFieldReference && insideSecondaryConstructor;
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);
Expression identifier = new IdentifierImpl(expression.getReferenceName(), isNullable);
@@ -364,6 +366,14 @@ public class ExpressionVisitor extends StatementVisitor {
);
}
private static boolean isResolvedToNotNull(PsiReference expression) {
PsiElement target = expression.resolve();
if (target instanceof PsiModifierListOwner) {
return ConverterUtil.isAnnotatedAsNotNull(((PsiModifierListOwner) target).getModifierList());
}
return false;
}
@NotNull
private static String getClassNameWithConstructor(@NotNull PsiReferenceExpression expression) {
PsiElement context = expression.getContext();
@@ -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()
}
}