diff --git a/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/AnnotationChecker.java b/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/AnnotationChecker.java index 60627e578f61..59b71340cf78 100644 --- a/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/AnnotationChecker.java +++ b/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/AnnotationChecker.java @@ -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; - } } diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java index d4048360cef7..6b3f9b984bab 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java @@ -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 { - 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); - } - } } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java index 0f04cf6ae25c..14afbb3864e3 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java @@ -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; diff --git a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java index 259f12445856..8556219a9049 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java @@ -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 { private final Set myTypeParams = new HashSet<>();