KT-641 Support @NotNull @Nullable annotations
This commit is contained in:
@@ -18,6 +18,13 @@ import java.util.*;
|
|||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public class Converter {
|
public class Converter {
|
||||||
|
private final static Set<String> NOT_NULL_ANNOTATIONS = new HashSet<String>() {
|
||||||
|
{
|
||||||
|
add("org.jetbrains.annotations.NotNull");
|
||||||
|
add("com.sun.istack.internal.NotNull");
|
||||||
|
add("javax.annotation.Nonnull");
|
||||||
|
}
|
||||||
|
} ;
|
||||||
private static Set<String> ourClassIdentifiers = new HashSet<String>();
|
private static Set<String> ourClassIdentifiers = new HashSet<String>();
|
||||||
|
|
||||||
public static void setClassIdentifiers(Set<String> identifiers) {
|
public static void setClassIdentifiers(Set<String> identifiers) {
|
||||||
@@ -288,7 +295,7 @@ public class Converter {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private static Function methodToFunction(@NotNull PsiMethod method, boolean notEmpty) {
|
private static Function methodToFunction(@NotNull PsiMethod method, boolean notEmpty) {
|
||||||
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
|
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 Block body = blockToBlock(method.getBody(), notEmpty);
|
||||||
final Element params = elementToElement(method.getParameterList());
|
final Element params = elementToElement(method.getParameterList());
|
||||||
final List<Element> typeParameters = elementsToElementList(method.getTypeParameters());
|
final List<Element> typeParameters = elementsToElementList(method.getTypeParameters());
|
||||||
@@ -408,6 +415,13 @@ public class Converter {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Type typeToType(PsiType type, boolean notNull) {
|
||||||
|
Type result = typeToType(type);
|
||||||
|
if (notNull)
|
||||||
|
result.convertToNotNull();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static List<Type> typesToNotNullableTypeList(@NotNull PsiType[] types) {
|
private static List<Type> typesToNotNullableTypeList(@NotNull PsiType[] types) {
|
||||||
List<Type> result = new LinkedList<Type>(typesToTypeList(types));
|
List<Type> result = new LinkedList<Type>(typesToTypeList(types));
|
||||||
@@ -441,11 +455,23 @@ public class Converter {
|
|||||||
public static Parameter parameterToParameter(@NotNull PsiParameter parameter) {
|
public static Parameter parameterToParameter(@NotNull PsiParameter parameter) {
|
||||||
return new Parameter(
|
return new Parameter(
|
||||||
new IdentifierImpl(parameter.getName()), // TODO: remove
|
new IdentifierImpl(parameter.getName()), // TODO: remove
|
||||||
typeToType(parameter.getType()),
|
typeToType(parameter.getType(), isNotNull(parameter.getModifierList())),
|
||||||
isReadOnly(parameter)
|
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) {
|
private static boolean isReadOnly(PsiParameter parameter) {
|
||||||
for (PsiReference r : (ReferencesSearch.search(parameter))) {
|
for (PsiReference r : (ReferencesSearch.search(parameter))) {
|
||||||
if (r instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression) r)) {
|
if (r instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression) r)) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.jetbrains.jet.j2k.visitors;
|
|||||||
|
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.j2k.Converter;
|
||||||
import org.jetbrains.jet.j2k.ast.*;
|
import org.jetbrains.jet.j2k.ast.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -26,7 +27,7 @@ public class ElementVisitor extends JavaElementVisitor {
|
|||||||
myResult = new LocalVariable(
|
myResult = new LocalVariable(
|
||||||
new IdentifierImpl(variable.getName()), // TODO
|
new IdentifierImpl(variable.getName()), // TODO
|
||||||
modifiersListToModifiersSet(variable.getModifierList()),
|
modifiersListToModifiersSet(variable.getModifierList()),
|
||||||
typeToType(variable.getType()),
|
typeToType(variable.getType(), Converter.isNotNull(variable.getModifierList())),
|
||||||
expressionToExpression(variable.getInitializer())
|
expressionToExpression(variable.getInitializer())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user