[java-highlighting] PsiTypesUtil.isValidAnnotationMethodType

Part of IDEA-365344 Create a new Java error highlighter with minimal dependencies (PSI only)

GitOrigin-RevId: 20f7189c30ab52a667b810fb5cde6395d196301a
This commit is contained in:
Tagir Valeev
2025-01-09 12:30:34 +00:00
committed by intellij-monorepo-bot
parent 0e2be796b7
commit ee123058f6
4 changed files with 31 additions and 60 deletions
@@ -96,7 +96,7 @@ final class AnnotationChecker {
}
void checkValidAnnotationType(@Nullable PsiType type, @NotNull PsiTypeElement typeElement) {
if (type == null || !isValidAnnotationMethodType(type)) {
if (type == null || !PsiTypesUtil.isValidAnnotationMethodType(type)) {
myVisitor.report(JavaErrorKinds.ANNOTATION_METHOD_INVALID_TYPE.create(typeElement, type));
}
}
@@ -517,26 +517,4 @@ final class AnnotationChecker {
}
}
}
private static boolean isValidAnnotationMethodType(@NotNull PsiType type) {
if (type instanceof PsiArrayType arrayType) {
if (arrayType.getArrayDimensions() != 1) return false;
type = arrayType.getComponentType();
}
if (type instanceof PsiPrimitiveType) {
return !PsiTypes.voidType().equals(type) && !PsiTypes.nullType().equals(type);
}
if (type instanceof PsiClassType classType) {
if (classType.getParameters().length > 0) {
return PsiTypesUtil.classNameEquals(classType, CommonClassNames.JAVA_LANG_CLASS);
}
if (classType.equalsToText(CommonClassNames.JAVA_LANG_CLASS) || classType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
return true;
}
PsiClass aClass = classType.resolve();
return aClass != null && (aClass.isAnnotationType() || aClass.isEnum());
}
return false;
}
}
@@ -18,7 +18,6 @@ import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.PsiImmediateClassType;
import com.intellij.psi.util.PsiTypesUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -252,38 +251,4 @@ public final class AnnotationsHighlightUtil {
return null;
}
public static class AnnotationReturnTypeVisitor extends PsiTypeVisitor<Boolean> {
public static final AnnotationReturnTypeVisitor INSTANCE = new AnnotationReturnTypeVisitor();
@Override
public Boolean visitType(@NotNull PsiType type) {
return Boolean.FALSE;
}
@Override
public Boolean visitPrimitiveType(@NotNull PsiPrimitiveType primitiveType) {
return PsiTypes.voidType().equals(primitiveType) || PsiTypes.nullType().equals(primitiveType) ? Boolean.FALSE : Boolean.TRUE;
}
@Override
public Boolean visitArrayType(@NotNull PsiArrayType arrayType) {
if (arrayType.getArrayDimensions() != 1) return Boolean.FALSE;
PsiType componentType = arrayType.getComponentType();
return componentType.accept(this);
}
@Override
public Boolean visitClassType(@NotNull PsiClassType classType) {
if (classType.getParameters().length > 0) {
return PsiTypesUtil.classNameEquals(classType, CommonClassNames.JAVA_LANG_CLASS);
}
PsiClass aClass = classType.resolve();
if (aClass != null && (aClass.isAnnotationType() || aClass.isEnum())) {
return Boolean.TRUE;
}
return classType.equalsToText(CommonClassNames.JAVA_LANG_CLASS) || classType.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
}
}
@@ -5,7 +5,6 @@ import com.intellij.codeInsight.ExpectedTypeInfo;
import com.intellij.codeInsight.ExpectedTypesProvider;
import com.intellij.codeInsight.TailTypes;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.analysis.AnnotationsHighlightUtil;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.java.JavaBundle;
@@ -14,6 +13,7 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
@@ -126,7 +126,7 @@ public class CreateAnnotationMethodFromUsageFix extends CreateFromUsageBaseFix {
type = currentType.createArrayType();
}
}
if (type != null && type.accept(AnnotationsHighlightUtil.AnnotationReturnTypeVisitor.INSTANCE).booleanValue()) {
if (type != null && PsiTypesUtil.isValidAnnotationMethodType(type)) {
return type;
}
return null;
@@ -756,6 +756,34 @@ public final class PsiTypesUtil {
return type;
}
/**
* @param type type to check
* @return true if a given type is a valid return type for an annotation method
*/
public static boolean isValidAnnotationMethodType(@NotNull PsiType type) {
if (type instanceof PsiArrayType) {
PsiArrayType arrayType = (PsiArrayType)type;
if (arrayType.getArrayDimensions() != 1) return false;
type = arrayType.getComponentType();
}
if (type instanceof PsiPrimitiveType) {
return !PsiTypes.voidType().equals(type) && !PsiTypes.nullType().equals(type);
}
if (type instanceof PsiClassType) {
PsiClassType classType = (PsiClassType)type;
if (classType.getParameters().length > 0) {
return classNameEquals(classType, CommonClassNames.JAVA_LANG_CLASS);
}
if (classType.equalsToText(CommonClassNames.JAVA_LANG_CLASS) || classType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
return true;
}
PsiClass aClass = classType.resolve();
return aClass != null && (aClass.isAnnotationType() || aClass.isEnum());
}
return false;
}
public static class TypeParameterSearcher extends PsiTypeVisitor<Boolean> {
private final Set<PsiTypeParameter> myTypeParams = new HashSet<>();