diff --git a/src/org/jetbrains/jet/j2k/Converter.java b/src/org/jetbrains/jet/j2k/Converter.java index 6e50656bedd..42a2d844b1f 100644 --- a/src/org/jetbrains/jet/j2k/Converter.java +++ b/src/org/jetbrains/jet/j2k/Converter.java @@ -18,6 +18,13 @@ import java.util.*; * @author ignatov */ public class Converter { + private final static Set NOT_NULL_ANNOTATIONS = new HashSet() { + { + add("org.jetbrains.annotations.NotNull"); + add("com.sun.istack.internal.NotNull"); + add("javax.annotation.Nonnull"); + } + } ; private static Set ourClassIdentifiers = new HashSet(); public static void setClassIdentifiers(Set identifiers) { @@ -288,7 +295,7 @@ public class Converter { @NotNull private static Function methodToFunction(@NotNull PsiMethod method, boolean notEmpty) { final IdentifierImpl identifier = new IdentifierImpl(method.getName()); - final Type returnType = typeToType(method.getReturnType()); + final Type returnType = typeToType(method.getReturnType(), isNotNull(method.getModifierList())); final Block body = blockToBlock(method.getBody(), notEmpty); final Element params = elementToElement(method.getParameterList()); final List typeParameters = elementsToElementList(method.getTypeParameters()); @@ -408,6 +415,13 @@ public class Converter { return result; } + public static Type typeToType(PsiType type, boolean notNull) { + Type result = typeToType(type); + if (notNull) + result.convertToNotNull(); + return result; + } + @NotNull private static List typesToNotNullableTypeList(@NotNull PsiType[] types) { List result = new LinkedList(typesToTypeList(types)); @@ -441,11 +455,23 @@ public class Converter { public static Parameter parameterToParameter(@NotNull PsiParameter parameter) { return new Parameter( new IdentifierImpl(parameter.getName()), // TODO: remove - typeToType(parameter.getType()), + typeToType(parameter.getType(), isNotNull(parameter.getModifierList())), isReadOnly(parameter) ); } + public static boolean isNotNull(@Nullable PsiModifierList modifierList) { + if (modifierList != null) { + PsiAnnotation[] annotations = modifierList.getAnnotations(); + for (PsiAnnotation a : annotations) { + String qualifiedName = a.getQualifiedName(); + if (qualifiedName != null && NOT_NULL_ANNOTATIONS.contains(qualifiedName)) + return true; + } + } + return false; + } + private static boolean isReadOnly(PsiParameter parameter) { for (PsiReference r : (ReferencesSearch.search(parameter))) { if (r instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression) r)) { diff --git a/src/org/jetbrains/jet/j2k/visitors/ElementVisitor.java b/src/org/jetbrains/jet/j2k/visitors/ElementVisitor.java index 4c92c1fa183..f18a5f03d43 100644 --- a/src/org/jetbrains/jet/j2k/visitors/ElementVisitor.java +++ b/src/org/jetbrains/jet/j2k/visitors/ElementVisitor.java @@ -2,6 +2,7 @@ package org.jetbrains.jet.j2k.visitors; import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.j2k.Converter; import org.jetbrains.jet.j2k.ast.*; import java.util.List; @@ -26,7 +27,7 @@ public class ElementVisitor extends JavaElementVisitor { myResult = new LocalVariable( new IdentifierImpl(variable.getName()), // TODO modifiersListToModifiersSet(variable.getModifierList()), - typeToType(variable.getType()), + typeToType(variable.getType(), Converter.isNotNull(variable.getModifierList())), expressionToExpression(variable.getInitializer()) ); } diff --git a/testData/ast/annotations/file/jetbrainsNotNull.jav b/testData/ast/annotations/file/jetbrainsNotNull.jav new file mode 100644 index 00000000000..92c511d88a0 --- /dev/null +++ b/testData/ast/annotations/file/jetbrainsNotNull.jav @@ -0,0 +1,29 @@ +package test; + +import org.jetbrains.annotations.NotNull; + +public class Test { + @NotNull String myStr = "String2"; + + public Test(@NotNull String str) { + myStr = str; + } + + public void sout(@NotNull String str) { + System.out.println(str); + } + + @NotNull + public String dummy(@NotNull String str) { + return str; + } + + public void test() { + sout("String"); + @NotNull String test = "String2"; + sout(test); + sout(dummy(test)); + + new Test(test); + } +} \ No newline at end of file diff --git a/testData/ast/annotations/file/jetbrainsNotNull.kt b/testData/ast/annotations/file/jetbrainsNotNull.kt new file mode 100644 index 00000000000..2b0b40a701f --- /dev/null +++ b/testData/ast/annotations/file/jetbrainsNotNull.kt @@ -0,0 +1,21 @@ +namespace test +import org.jetbrains.annotations.NotNull +public open class Test(str : String) { +{ +$myStr = str +} +var myStr : String? = "String2" +open public fun sout(str : String) : Unit { +System.out?.println(str) +} +open public fun dummy(str : String) : String { +return str +} +open public fun test() : Unit { +sout("String") +var test : String = "String2" +sout(test) +sout(dummy(test)) +Test(test) +} +} \ No newline at end of file