[java] IDEA-379795 Add hasAnnotations() to PsiAnnotationOwner, PsiModifierListOwner and TypeAnnotationProvider

GitOrigin-RevId: dbe2d9508f45fb5bbbff26c1ba81792f5120c690
This commit is contained in:
Tagir Valeev
2025-09-26 11:09:02 +00:00
committed by intellij-monorepo-bot
parent ecfabbcaed
commit ac74ac484e
33 changed files with 105 additions and 41 deletions
@@ -54,4 +54,11 @@ public interface PsiAnnotationOwner {
*/
@NotNull
PsiAnnotation addAnnotation(@NotNull @NonNls String qualifiedName);
/**
* @return true if there is at least one annotation attached to the element.
*/
default boolean hasAnnotations() {
return getAnnotations().length > 0;
}
}
@@ -36,6 +36,14 @@ public interface PsiModifierListOwner extends PsiElement {
return PsiJvmConversionHelper.getListAnnotations(this);
}
/**
* @return true if the element has annotations, false otherwise.
*/
default boolean hasAnnotations() {
PsiModifierList list = getModifierList();
return list != null && list.hasAnnotations();
}
default @Nullable PsiAnnotation getAnnotation(@NotNull String fqn) {
return PsiJvmConversionHelper.getListAnnotation(this, fqn);
}
@@ -333,6 +333,11 @@ public abstract class PsiType implements PsiAnnotationOwner, Cloneable, JvmType
return myAnnotationProvider.getAnnotations();
}
@Override
public boolean hasAnnotations() {
return myAnnotationProvider.hasAnnotations();
}
@Override
public PsiAnnotation findAnnotation(@NotNull String qualifiedName) {
for (PsiAnnotation annotation : getAnnotations()) {
@@ -50,6 +50,11 @@ public interface PsiTypeParameter extends PsiClass, PsiAnnotationOwner, JvmTypeP
return PsiClass.super.getAnnotations();
}
@Override
default boolean hasAnnotations() {
return PsiClass.super.hasAnnotations();
}
@Override
default boolean hasAnnotation(@NotNull @NonNls String fqn) {
return PsiClass.super.hasAnnotation(fqn);
@@ -21,6 +21,11 @@ public interface TypeAnnotationProvider {
return PsiAnnotation.EMPTY_ARRAY;
}
@Override
public boolean hasAnnotations() {
return false;
}
@Override
public String toString() {
return "EMPTY";
@@ -29,6 +34,13 @@ public interface TypeAnnotationProvider {
@NotNull PsiAnnotation @NotNull [] getAnnotations();
/**
* @return true if this provider has annotations.
*/
default boolean hasAnnotations() {
return getAnnotations().length > 0;
}
/**
* @param owner owner for annotations in this provider
* @return a provider whose annotations are updated to return the supplied owner.
@@ -52,6 +64,12 @@ public interface TypeAnnotationProvider {
return myAnnotations;
}
@Override
public boolean hasAnnotations() {
// Array is always non-empty
return true;
}
public static @NotNull TypeAnnotationProvider create(@NotNull PsiAnnotation @NotNull [] annotations) {
if (annotations.length == 0) return EMPTY;
for (PsiAnnotation annotation : annotations) {
@@ -387,7 +387,7 @@ public final class PsiTypesUtil {
return type.accept(new PsiTypeVisitor<Boolean>() {
@Override
public Boolean visitType(@NotNull PsiType type) {
return type.getAnnotations().length > 0;
return type.hasAnnotations();
}
@Override