[java-analysis] Checker's DefaultQualifier should not apply to type parameters and type parameter uses

Fixes IDEA-288014 @DefaultQualifier should not apply to parameterized type uses

GitOrigin-RevId: 2b9bd0bc4bbe594092d3b120ef8cceac718abf5d
This commit is contained in:
Tagir Valeev
2022-02-09 12:23:06 +00:00
committed by intellij-monorepo-bot
parent b69d1625f1
commit f1bde775ff
2 changed files with 27 additions and 0 deletions
@@ -29,10 +29,21 @@ class CheckerFrameworkSupport implements AnnotationPackageSupport {
@NotNull PsiElement context,
PsiAnnotation.TargetType @NotNull [] types,
boolean superPackage) {
if (context instanceof PsiTypeParameter) {
// DefaultQualifier is not applicable to type parameter declarations
return null;
}
if (anno.hasQualifiedName(DEFAULT_QUALIFIER)) {
PsiAnnotationMemberValue value = anno.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
if (value instanceof PsiClassObjectAccessExpression &&
hasAppropriateTarget(types, anno.findAttributeValue("locations"))) {
PsiType type = context instanceof PsiMethod ? ((PsiMethod)context).getReturnType() :
context instanceof PsiVariable ? ((PsiVariable)context).getType() :
null;
if (type instanceof PsiClassType && ((PsiClassType)type).resolve() instanceof PsiTypeParameter) {
// DefaultQualifier is not applicable to type parameter uses
return null;
}
PsiClass valueClass = PsiUtil.resolveClassInClassTypeOnly(((PsiClassObjectAccessExpression)value).getOperand().getType());
if (valueClass != null) {
NullableNotNullManager instance = NullableNotNullManager.getInstance(value.getProject());
@@ -134,4 +134,20 @@ class Test {
assert NullableNotNullManager.isNullable(clazz.methods[1])
}
void "test type parameter use"() {
def clazz = myFixture.addClass """
import org.checkerframework.checker.nullness.qual.*;
import org.checkerframework.framework.qual.*;
@DefaultQualifier(NonNull.class)
interface Test<X> {
String test(X x);
X test(String x);
}"""
assert NullableNotNullManager.isNotNull(clazz.methods[0])
assert !NullableNotNullManager.isNotNull(clazz.methods[1])
assert !NullableNotNullManager.isNotNull(clazz.methods[0].parameterList.parameters[0])
assert NullableNotNullManager.isNotNull(clazz.methods[1].parameterList.parameters[0])
}
}