ignore @NotNull inferred from @ParametersAreNonnullByDefault when there's hardcoded contract (e.g. Guava Preconditions.checkNotNull)

This commit is contained in:
peter
2014-11-20 15:54:01 +01:00
parent dca0ad0d56
commit 110abf16a4
3 changed files with 44 additions and 3 deletions
@@ -51,7 +51,7 @@ public class InferredAnnotationsManagerImpl extends InferredAnnotationsManager {
}
}
if (!ignoreBytecodeInference(listOwner, annotationFQN)) {
if (!ignoreInference(listOwner, annotationFQN)) {
PsiAnnotation fromBytecode = ProjectBytecodeAnalysis.getInstance(myProject).findInferredAnnotation(listOwner, annotationFQN);
if (fromBytecode != null) {
return fromBytecode;
@@ -71,7 +71,8 @@ public class InferredAnnotationsManagerImpl extends InferredAnnotationsManager {
return contracts.isEmpty() ? null : createContractAnnotation(contracts, HardcodedContracts.isHardcodedPure(method));
}
private static boolean ignoreBytecodeInference(PsiModifierListOwner owner, @Nullable String annotationFQN) {
@Override
public boolean ignoreInference(@NotNull PsiModifierListOwner owner, @Nullable String annotationFQN) {
if (ORG_JETBRAINS_ANNOTATIONS_CONTRACT.equals(annotationFQN) && hasHardcodedContracts(owner)) {
return true;
}
@@ -125,7 +126,7 @@ public class InferredAnnotationsManagerImpl extends InferredAnnotationsManager {
List<PsiAnnotation> result = ContainerUtil.newArrayList();
PsiAnnotation[] fromBytecode = ProjectBytecodeAnalysis.getInstance(myProject).findInferredAnnotations(listOwner);
for (PsiAnnotation annotation : fromBytecode) {
if (!ignoreBytecodeInference(listOwner, annotation.getQualifiedName())) {
if (!ignoreInference(listOwner, annotation.getQualifiedName())) {
if (!ORG_JETBRAINS_ANNOTATIONS_CONTRACT.equals(annotation.getQualifiedName()) || canHaveContract(listOwner)) {
result.add(annotation);
}
@@ -23,6 +23,15 @@ import com.intellij.psi.PsiModifierListOwner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Returns annotations inferred by bytecode our source code, for example contracts and nullity.
*
* @see com.intellij.codeInsight.NullableNotNullManager
* @see org.jetbrains.annotations.Contract
* @see org.jetbrains.annotations.Nullable
* @see org.jetbrains.annotations.NotNull
* @see com.intellij.codeInsight.AnnotationUtil
*/
public abstract class InferredAnnotationsManager {
private static final NotNullLazyKey<InferredAnnotationsManager, Project> INSTANCE_KEY = ServiceManager.createLazyKey(InferredAnnotationsManager.class);
@@ -30,11 +39,37 @@ public abstract class InferredAnnotationsManager {
return INSTANCE_KEY.getValue(project);
}
/**
* @return if exists, an inferred annotation by given qualified name on a given PSI element. Several invocations may return several
* different instances of {@link com.intellij.psi.PsiAnnotation}, which are not guaranteed to be equal.
*/
@Nullable
public abstract PsiAnnotation findInferredAnnotation(@NotNull PsiModifierListOwner listOwner, @NotNull String annotationFQN);
/**
* There is a number of well-known methods where automatic inference fails (for example, {@link java.util.Objects#requireNonNull(Object)}.
* For such methods, contracts are hardcoded, and for their parameters inferred @NotNull are suppressed.<p/>
*
* In addition, package-default annotations like @ParametersAreNonnullByDefault are not honored for parameters where
* {@link org.jetbrains.annotations.NotNull} inference is ignored.
*
* @return whether inference is to be suppressed the given annotation on the given method or parameter
*/
public abstract boolean ignoreInference(@NotNull PsiModifierListOwner owner, @Nullable String annotationFQN);
/**
* When annotation name is known, prefer {@link #findInferredAnnotation(com.intellij.psi.PsiModifierListOwner, String)} as
* potentially faster.
*
* @return all inferred annotations for the given element
*/
@NotNull
public abstract PsiAnnotation[] findInferredAnnotations(@NotNull PsiModifierListOwner listOwner);
/**
* @return whether the given annotation was inferred by this service.
*
* @see com.intellij.codeInsight.AnnotationUtil#isInferredAnnotation(com.intellij.psi.PsiAnnotation)
*/
public abstract boolean isInferredAnnotation(@NotNull PsiAnnotation annotation);
}
@@ -184,6 +184,11 @@ public class NullableNotNullManager implements PersistentStateComponent<Element>
if (AnnotationUtil.isAnnotated(owner, nullable ? Arrays.asList(DEFAULT_NOT_NULLS) : Arrays.asList(DEFAULT_NULLABLES), checkBases, false)) {
return null;
}
if (!nullable && InferredAnnotationsManager.getInstance(owner.getProject()).ignoreInference(owner, AnnotationUtil.NOT_NULL)) {
return null;
}
return findNullabilityDefaultInHierarchy(owner, nullable);
}