Cleanup: NotNull/Nullable

GitOrigin-RevId: b8e892f32ea84c2115973155dba7127b892cc36e
This commit is contained in:
Egor Zhdan
2019-04-04 18:16:43 +03:00
committed by intellij-monorepo-bot
parent ce8e10e6c4
commit 39d2d77155
12734 changed files with 160314 additions and 488001 deletions

View File

@@ -54,9 +54,7 @@ public abstract class NullableNotNullManager {
"org.checkerframework.checker.nullness.compatqual.NonNullType",
"com.android.annotations.NonNull",
};
private static final List<String> DEFAULT_ALL = Arrays.asList(
ArrayUtil.append(ArrayUtil.mergeArrays(DEFAULT_NULLABLES, DEFAULT_NOT_NULLS),
"org.checkerframework.checker.nullness.qual.MonotonicNonNull"));
private static final List<String> DEFAULT_ALL = Arrays.asList(ArrayUtil.mergeArrays(DEFAULT_NULLABLES, DEFAULT_NOT_NULLS));
protected NullableNotNullManager(Project project) {
myProject = project;
@@ -80,10 +78,6 @@ public abstract class NullableNotNullManager {
@NotNull
public abstract String getDefaultNullable();
/**
* Returns an annotation which marks given element as Nullable, if any. Usage of this method is discouraged.
* Use {@link #findEffectiveNullabilityInfo(PsiModifierListOwner)} instead.
*/
@Nullable
public PsiAnnotation getNullableAnnotation(@NotNull PsiModifierListOwner owner, boolean checkBases) {
return findNullityAnnotationWithDefault(owner, checkBases, true);
@@ -94,10 +88,6 @@ public abstract class NullableNotNullManager {
@NotNull
public abstract String getDefaultNotNull();
/**
* Returns an annotation which marks given element as NotNull, if any. Usage of this method is discouraged.
* Use {@link #findEffectiveNullabilityInfo(PsiModifierListOwner)} instead.
*/
@Nullable
public PsiAnnotation getNotNullAnnotation(@NotNull PsiModifierListOwner owner, boolean checkBases) {
return findNullityAnnotationWithDefault(owner, checkBases, false);
@@ -261,18 +251,22 @@ public abstract class NullableNotNullManager {
@Nullable
private NullabilityAnnotationInfo doFindEffectiveNullabilityAnnotation(@NotNull PsiModifierListOwner owner) {
List<String> nullables = getNullablesWithNickNames();
Set<String> annotationNames = getAllNullabilityAnnotationsWithNickNames();
Set<String> extraAnnotations = new HashSet<>(DEFAULT_ALL);
extraAnnotations.addAll(annotationNames);
extraAnnotations.removeAll(annotationNames);
if (!extraAnnotations.isEmpty()) {
annotationNames = new HashSet<>(annotationNames);
annotationNames.addAll(extraAnnotations);
}
PsiAnnotation annotation = findPlainAnnotation(owner, true, extraAnnotations);
PsiAnnotation annotation = findPlainAnnotation(owner, true, annotationNames);
if (annotation != null) {
if (!annotationNames.contains(annotation.getQualifiedName())) {
if (extraAnnotations.contains(annotation.getQualifiedName())) {
// Deliberately excluded known standard annotation still has precedence over default class-level or package-level annotation:
// return null in this case
return null;
}
List<String> nullables = getNullablesWithNickNames();
return new NullabilityAnnotationInfo(annotation,
nullables.contains(annotation.getQualifiedName()) ? Nullability.NULLABLE : Nullability.NOT_NULL,
false);
@@ -282,7 +276,7 @@ public abstract class NullableNotNullManager {
List<PsiParameter> superParameters = getSuperAnnotationOwners((PsiParameter)owner);
if (!superParameters.isEmpty()) {
for (PsiParameter parameter: superParameters) {
PsiAnnotation plain = findPlainAnnotation(parameter, false, extraAnnotations);
PsiAnnotation plain = findPlainAnnotation(parameter, false, annotationNames);
// Plain not null annotation is not inherited
if (plain != null) return null;
NullabilityAnnotationInfo defaultInfo = findNullityDefaultInHierarchy(parameter);
@@ -301,7 +295,7 @@ public abstract class NullableNotNullManager {
return null;
}
private PsiAnnotation takeAnnotationFromSuperParameters(@NotNull PsiParameter owner, @NotNull List<? extends PsiParameter> superOwners) {
private PsiAnnotation takeAnnotationFromSuperParameters(@NotNull PsiParameter owner, @NotNull List<PsiParameter> superOwners) {
return RecursionManager.doPreventingRecursion(owner, true, () -> {
for (PsiParameter superOwner : superOwners) {
PsiAnnotation anno = findNullityAnnotationWithDefault(superOwner, false, false);
@@ -316,17 +310,6 @@ public abstract class NullableNotNullManager {
return findPlainAnnotation(owner, checkBases, qNames);
}
/**
* @return an annotation (if any) with the given nullability semantics on the given declaration or its type. In case of conflicts,
* type annotations are preferred.
*/
@Nullable
public PsiAnnotation findExplicitNullabilityAnnotation(@NotNull PsiModifierListOwner owner, @NotNull Nullability nullability) {
if (nullability == Nullability.UNKNOWN) return null;
List<String> names = nullability == Nullability.NULLABLE ? getNullablesWithNickNames() : getNotNullsWithNickNames();
return findPlainAnnotation(owner, false, new HashSet<>(names));
}
@Nullable
private static PsiAnnotation findPlainAnnotation(@NotNull PsiModifierListOwner owner,
boolean checkBases,
@@ -407,7 +390,7 @@ public abstract class NullableNotNullManager {
PsiElement element = owner.getParent();
while (element != null) {
if (element instanceof PsiModifierListOwner) {
NullabilityAnnotationInfo result = getNullityDefault((PsiModifierListOwner)element, placeTargetTypes, owner, false);
NullabilityAnnotationInfo result = getNullityDefault((PsiModifierListOwner)element, placeTargetTypes, false);
if (result != null) {
return result;
}
@@ -415,8 +398,8 @@ public abstract class NullableNotNullManager {
if (element instanceof PsiClassOwner) {
String packageName = ((PsiClassOwner)element).getPackageName();
return findNullityDefaultOnPackage(placeTargetTypes, JavaPsiFacade.getInstance(element.getProject()).findPackage(packageName),
owner);
return findNullityDefaultOnPackage(placeTargetTypes,
JavaPsiFacade.getInstance(element.getProject()).findPackage(packageName));
}
element = element.getContext();
@@ -425,12 +408,10 @@ public abstract class NullableNotNullManager {
}
@Nullable
private NullabilityAnnotationInfo findNullityDefaultOnPackage(@NotNull PsiAnnotation.TargetType[] placeTargetTypes,
@Nullable PsiPackage psiPackage,
PsiModifierListOwner owner) {
private NullabilityAnnotationInfo findNullityDefaultOnPackage(@NotNull PsiAnnotation.TargetType[] placeTargetTypes, @Nullable PsiPackage psiPackage) {
boolean superPackage = false;
while (psiPackage != null) {
NullabilityAnnotationInfo onPkg = getNullityDefault(psiPackage, placeTargetTypes, owner, superPackage);
NullabilityAnnotationInfo onPkg = getNullityDefault(psiPackage, placeTargetTypes, superPackage);
if (onPkg != null) return onPkg;
superPackage = true;
psiPackage = psiPackage.getParentPackage();
@@ -439,9 +420,28 @@ public abstract class NullableNotNullManager {
}
@Nullable
abstract NullabilityAnnotationInfo getNullityDefault(@NotNull PsiModifierListOwner container,
@NotNull PsiAnnotation.TargetType[] placeTargetTypes,
PsiModifierListOwner owner, boolean superPackage);
private NullabilityAnnotationInfo getNullityDefault(@NotNull PsiModifierListOwner container,
@NotNull PsiAnnotation.TargetType[] placeTargetTypes,
boolean superPackage) {
PsiModifierList modifierList = container.getModifierList();
if (modifierList == null) return null;
for (PsiAnnotation annotation : modifierList.getAnnotations()) {
NullabilityAnnotationInfo result = checkNullityDefault(annotation, placeTargetTypes, superPackage);
if (result != null) {
return result;
}
}
return null;
}
@Nullable
private NullabilityAnnotationInfo checkNullityDefault(@NotNull PsiAnnotation annotation, @NotNull PsiAnnotation.TargetType[] placeTargetTypes, boolean superPackage) {
NullabilityAnnotationInfo jsr = superPackage ? null : isJsr305Default(annotation, placeTargetTypes);
return jsr != null ? jsr : CheckerFrameworkNullityUtil.isCheckerDefault(annotation, placeTargetTypes);
}
@Nullable
protected abstract NullabilityAnnotationInfo isJsr305Default(@NotNull PsiAnnotation annotation, @NotNull PsiAnnotation.TargetType[] placeTargetTypes);
@NotNull
public abstract List<String> getNullables();