Check not null annotations for parameters in IdeaJdkAnnotationsReflectedTest

This commit is contained in:
Natalia.Ukhorskaya
2013-05-29 15:30:03 +04:00
parent 92bc46ab8f
commit fc3a413fba
@@ -87,14 +87,14 @@ public class IdeaJdkAnnotationsReflectedTest extends KotlinTestWithEnvironment {
public void visitMethod(PsiMethod method) { public void visitMethod(PsiMethod method) {
super.visitMethod(method); super.visitMethod(method);
if (method.getReturnType() != null) { // disabled for constructors if (method.getReturnType() != null) { // disabled for constructors
check(method, method); checkAndReport(method);
} }
} }
@Override @Override
public void visitField(PsiField field) { public void visitField(PsiField field) {
super.visitField(field); super.visitField(field);
check(field, field); checkAndReport(field);
} }
@Override @Override
@@ -103,22 +103,39 @@ public class IdeaJdkAnnotationsReflectedTest extends KotlinTestWithEnvironment {
PsiMethod method = PsiTreeUtil.getParentOfType(parameter, PsiMethod.class); PsiMethod method = PsiTreeUtil.getParentOfType(parameter, PsiMethod.class);
assert method != null; assert method != null;
if (method.getReturnType() != null) { // disabled for constructors if (method.getReturnType() != null) { // disabled for constructors
check(parameter, method); if (!check(parameter, method, AnnotationsKind.KOTLIN_SIGNATURE) &&
!check(parameter, parameter, AnnotationsKind.NOT_NULL)) {
declarationsWithMissingAnnotations.add(parameter);
}
} }
} }
private void check(@NotNull PsiModifierListOwner ideaOwner, @NotNull PsiModifierListOwner kotlinOwner) { private void checkAndReport(@NotNull PsiModifierListOwner annotationOwner) {
if (hasAnnotation(ideaFakeAnnotationsManager, ideaOwner, AnnotationUtil.NOT_NULL)) { if (!check(annotationOwner, annotationOwner, AnnotationsKind.ANY)) {
boolean kotlinHasNotNull = hasAnnotation(kotlinFakeAnnotationsManager, kotlinOwner, AnnotationUtil.NOT_NULL); declarationsWithMissingAnnotations.add(annotationOwner);
boolean kotlinHasKotlinSignature = hasAnnotation(kotlinFakeAnnotationsManager, kotlinOwner, }
JvmStdlibNames.KOTLIN_SIGNATURE.getFqName().asString()); }
if (kotlinOwner == ideaOwner && kotlinHasNotNull || kotlinHasKotlinSignature) {
// good private boolean check(
} @NotNull PsiModifierListOwner ideaOwner,
else { @NotNull PsiModifierListOwner kotlinOwner,
declarationsWithMissingAnnotations.add(kotlinOwner); @NotNull AnnotationsKind annotationsKind
)
{
return !hasAnnotationInIdea(ideaOwner) || hasAnnotationInKotlin(kotlinOwner, annotationsKind);
}
private boolean hasAnnotationInIdea(@NotNull PsiModifierListOwner owner) {
return hasAnnotation(ideaFakeAnnotationsManager, owner, AnnotationUtil.NOT_NULL);
}
private boolean hasAnnotationInKotlin(@NotNull PsiModifierListOwner owner, @NotNull AnnotationsKind annotationsKind) {
for (String name : annotationsKind.annotationNames) {
if (hasAnnotation(kotlinFakeAnnotationsManager, owner, name)) {
return true;
} }
} }
return false;
} }
private boolean hasAnnotation( private boolean hasAnnotation(
@@ -134,9 +151,21 @@ public class IdeaJdkAnnotationsReflectedTest extends KotlinTestWithEnvironment {
if (!declarationsWithMissingAnnotations.isEmpty()) { if (!declarationsWithMissingAnnotations.isEmpty()) {
StringBuilder builder = new StringBuilder("Annotations missing for JDK items:\n"); StringBuilder builder = new StringBuilder("Annotations missing for JDK items:\n");
for (PsiModifierListOwner declaration : declarationsWithMissingAnnotations) { for (PsiModifierListOwner declaration : declarationsWithMissingAnnotations) {
builder.append(PsiFormatUtil.getExternalName(declaration)).append("\n"); builder.append(PsiFormatUtil.getExternalName(declaration) + " " + declaration.getText()).append("\n");
} }
fail(builder.toString()); fail(builder.toString());
} }
} }
private enum AnnotationsKind {
KOTLIN_SIGNATURE(JvmStdlibNames.KOTLIN_SIGNATURE.getFqName().asString()),
NOT_NULL(AnnotationUtil.NOT_NULL),
ANY(AnnotationUtil.NOT_NULL, JvmStdlibNames.KOTLIN_SIGNATURE.getFqName().asString());
public final String[] annotationNames;
private AnnotationsKind(@NotNull String... annotationNames) {
this.annotationNames = annotationNames;
}
}
} }