[java-psi] IDEA-377693 Support unbounded wildcards

New exceptions are added with a discussion opened in JSpecify tracker:
https://github.com/jspecify/jspecify/issues/771

GitOrigin-RevId: ebcd3cfb99fc26e8b18c4c4ab25b10ed74ca8c11
This commit is contained in:
Tagir Valeev
2025-09-30 08:46:32 +00:00
committed by intellij-monorepo-bot
parent 0afc6f7842
commit f6b49100b4
9 changed files with 74 additions and 18 deletions
@@ -34,6 +34,12 @@ interface ContextNullabilityInfo {
public @NotNull ContextNullabilityInfo filtering(@NotNull Predicate<@NotNull PsiElement> contextFilter) {
return this;
}
@Override
public @NotNull ContextNullabilityInfo withNullabilityInContext(@NotNull Predicate<@NotNull PsiElement> contextFilter,
@NotNull Nullability nullability) {
return this;
}
};
/**
@@ -68,6 +74,15 @@ interface ContextNullabilityInfo {
default @NotNull ContextNullabilityInfo filtering(@NotNull Predicate<@NotNull PsiElement> contextFilter) {
return context -> contextFilter.test(context) ? forContext(context) : null;
}
default @NotNull ContextNullabilityInfo withNullabilityInContext(@NotNull Predicate<@NotNull PsiElement> contextFilter,
@NotNull Nullability nullability) {
return context -> {
NullabilityAnnotationInfo info = forContext(context);
if (info == null) return null;
return contextFilter.test(context) ? new NullabilityAnnotationInfo(info.getAnnotation(), nullability, info.isContainer()) : info;
};
}
/**
* @return a new {@code ContextNullabilityInfo} that filters out the cast contexts.
@@ -81,7 +81,9 @@ public final class TypeNullability {
if (this.nullability() == Nullability.NULLABLE && this.source() instanceof NullabilitySource.ExtendsBound) {
return nullability;
}
if (nullability.nullability() == Nullability.NOT_NULL && this.source() instanceof NullabilitySource.ExtendsBound) {
if ((nullability.nullability() == Nullability.NOT_NULL ||
nullability.nullability() == Nullability.UNKNOWN && !nullability.source().equals(NullabilitySource.Standard.NONE))
&& this.source() instanceof NullabilitySource.ExtendsBound) {
return nullability;
}
if (this.source() == NullabilitySource.Standard.NONE) {
@@ -58,6 +58,18 @@ public interface PsiTypeElement extends PsiElement, PsiAnnotationOwner {
return false;
}
/**
* Returns true if the type element represents an unbounded wildcard type like {@code ?} (possibly annotated).
* Strictly speaking, {@code isUnboundedWildcard() == getType() instanceof PsiWildcardType wt && !wt.isBounded()}.
* This method can be helpful for nullability annotation processing to avoid computing the type,
* as the type may in turn depend on the nullability.
*
* @return true if the type element represents an unbounded wildcard.
*/
default boolean isUnboundedWildcard() {
return getText().endsWith("?");
}
/**
* @return false if annotations cannot be added to this type element
* For example, the JVM language that doesn't support type-use annotations;
@@ -74,11 +74,13 @@ public final class PsiWildcardType extends PsiType.Stub implements JvmWildcardTy
@Override
public @NotNull PsiWildcardType withNullability(@NotNull TypeNullability nullability) {
if (nullability == getNullability()) return this;
return new PsiWildcardType(myManager, myIsExtending, myBound, getAnnotationProvider(), nullability);
}
@Override
public @NotNull PsiType annotate(@NotNull TypeAnnotationProvider provider) {
if (getAnnotationProvider() == provider) return this;
return new PsiWildcardType(myManager, myIsExtending, myBound, provider);
}