InferredAnnotationsManagerImpl: hardcoded not-nullness of collection factory methods

(there are too many overloads, so adding them into annotations.xml is impractical)
This commit is contained in:
Tagir Valeev
2017-02-28 16:43:40 +07:00
parent 01248ad92e
commit ff4a8a824a
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,15 +58,19 @@ public class InferredAnnotationsManagerImpl extends InferredAnnotationsManager {
return fromBytecode;
}
if (listOwner instanceof PsiMethodImpl) {
if (ORG_JETBRAINS_ANNOTATIONS_CONTRACT.equals(annotationFQN)) {
return getInferredContractAnnotation((PsiMethodImpl)listOwner);
if ((AnnotationUtil.NOT_NULL.equals(annotationFQN) || AnnotationUtil.NULLABLE.equals(annotationFQN))) {
PsiAnnotation anno = null;
if (listOwner instanceof PsiMethodImpl) {
anno = getInferredNullityAnnotation((PsiMethodImpl)listOwner);
}
if (listOwner instanceof PsiParameter) {
anno = getInferredNullityAnnotation((PsiParameter)listOwner);
}
return anno == null ? null : annotationFQN.equals(anno.getQualifiedName()) ? anno : null;
}
if ((AnnotationUtil.NOT_NULL.equals(annotationFQN) || AnnotationUtil.NULLABLE.equals(annotationFQN))) {
PsiAnnotation anno = getInferredNullityAnnotation((PsiMethodImpl)listOwner);
return anno == null ? null : annotationFQN.equals(anno.getQualifiedName()) ? anno : null;
}
if (listOwner instanceof PsiMethodImpl && ORG_JETBRAINS_ANNOTATIONS_CONTRACT.equals(annotationFQN)) {
return getInferredContractAnnotation((PsiMethodImpl)listOwner);
}
return null;
@@ -128,6 +132,25 @@ public class InferredAnnotationsManagerImpl extends InferredAnnotationsManager {
return null;
}
private PsiAnnotation getInferredNullityAnnotation(PsiParameter parameter) {
PsiElement scope = parameter.getDeclarationScope();
if (scope instanceof PsiMethod) {
PsiMethod method = (PsiMethod)scope;
if (method.getName().equals("of")) {
PsiClass containingClass = method.getContainingClass();
if (containingClass != null) {
String className = containingClass.getQualifiedName();
if (CommonClassNames.JAVA_UTIL_LIST.equals(className) ||
CommonClassNames.JAVA_UTIL_SET.equals(className) ||
CommonClassNames.JAVA_UTIL_MAP.equals(className)) {
return ProjectBytecodeAnalysis.getInstance(myProject).getNotNullAnnotation();
}
}
}
}
return null;
}
@Nullable
private PsiAnnotation createContractAnnotation(List<MethodContract> contracts, boolean pure) {
return createContractAnnotation(myProject, pure, StringUtil.join(contracts, "; "));