[java-inspections] IDEA-376220 Fix JavaApiUsageInspection not respecting preview features

Previously:
Assume we use language level 24, but JDK 25.
And that we used some *preview* API from JDK 25.
The quick-fix from JavaApiUsageInspection suggested bumping language level to 25.

Now:
It is fixed, i.e. the quick-fix suggests bumping language level to 25 preview.


Merge-request: IJ-MR-172531
Merged-by: Bartek Pacia <bartek.pacia@jetbrains.com>

GitOrigin-RevId: 4966e043c75a3b7ff5c19debe8e600579aad47c3
This commit is contained in:
Bartek Pacia
2025-08-26 13:31:30 +00:00
committed by intellij-monorepo-bot
parent b5cc3b7714
commit 954aed5dda
8 changed files with 102 additions and 16 deletions
@@ -86,11 +86,7 @@ public final class JavaPreviewFeatureUtil {
if (element instanceof PsiJavaCodeReferenceElement refElement) {
PsiElement resolved = refElement.resolve();
if (resolved instanceof PsiModifierListOwner owner) {
PsiAnnotation annotation = getPreviewFeatureAnnotationInternal(owner);
JavaFeature feature = fromPreviewFeatureAnnotation(annotation);
if (feature == null) return null;
if (isParticipating(refElement, owner)) return null;
return new PreviewFeatureUsage(feature, refElement, owner, annotation);
return getPreviewFeatureUsage(refElement, owner);
}
}
else if (element instanceof PsiRequiresStatement requiresStatement) {
@@ -122,6 +118,18 @@ public final class JavaPreviewFeatureUtil {
return null;
}
/**
* @see #getPreviewFeatureUsage(PsiElement)
*/
public static @Nullable PreviewFeatureUsage getPreviewFeatureUsage(@NotNull PsiJavaCodeReferenceElement refElement,
@NotNull PsiModifierListOwner owner) {
PsiAnnotation annotation = getPreviewFeatureAnnotationInternal(owner);
JavaFeature feature = fromPreviewFeatureAnnotation(annotation);
if (feature == null) return null;
if (isParticipating(refElement, owner)) return null;
return new PreviewFeatureUsage(feature, refElement, owner, annotation);
}
/**
* Participating source code means that such code can access preview feature api in the same package without warnings.
*