mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SuspiciousPackagePrivateAccessInspection: fully Uast-based retrieval of the context class for given node
GitOrigin-RevId: 4d5707b7322883d2735f7e636de6491e8b31b303
This commit is contained in:
committed by
intellij-monorepo-bot
parent
5243efa59d
commit
6e357707cb
+4
-8
@@ -6,8 +6,10 @@ import com.intellij.codeInsight.daemon.impl.analysis.HighlightMessageUtil
|
||||
import com.intellij.codeInspection.apiUsage.ApiUsageProcessor
|
||||
import com.intellij.codeInspection.apiUsage.ApiUsageUastVisitor
|
||||
import com.intellij.openapi.roots.ProjectFileIndex
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.PsiModifierListOwner
|
||||
import com.intellij.psi.util.PsiUtilCore
|
||||
import org.jetbrains.annotations.ApiStatus
|
||||
import org.jetbrains.uast.*
|
||||
@@ -32,12 +34,6 @@ class NonExtendableApiUsageInspection : LocalInspectionTool() {
|
||||
return virtualFile != null && ProjectFileIndex.getInstance(element.project).isInLibraryClasses(virtualFile)
|
||||
}
|
||||
|
||||
private fun isPsiAncestor(ancestor: UElement, child: UElement): Boolean {
|
||||
val ancestorPsi = ancestor.sourcePsi ?: return false
|
||||
val childPsi = child.sourcePsi ?: return false
|
||||
return PsiTreeUtil.isAncestor(ancestorPsi, childPsi, false)
|
||||
}
|
||||
|
||||
private fun isSuperClassReferenceInSubclassDeclaration(sourceNode: UElement, subclassDeclaration: UClass) =
|
||||
subclassDeclaration.uastSuperTypes.any { isPsiAncestor(it, sourceNode) }
|
||||
|
||||
|
||||
+1
-19
@@ -17,7 +17,6 @@ import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.profile.codeInspection.InspectionProfileManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.resolve.JavaResolveUtil;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUIUtil;
|
||||
@@ -195,14 +194,9 @@ public class SuspiciousPackagePrivateAccessInspection extends AbstractBaseUastLo
|
||||
@Nullable
|
||||
private static PsiClass getContextClass(@NotNull UElement sourceNode, boolean forClassReference) {
|
||||
PsiElement sourcePsi = sourceNode.getSourcePsi();
|
||||
if (sourcePsi == null) return null;
|
||||
PsiClass javaContextClass = JavaResolveUtil.getContextClass(sourcePsi);
|
||||
if (javaContextClass != null) {
|
||||
return javaContextClass;
|
||||
}
|
||||
UClass sourceClass = UastUtils.findContaining(sourcePsi, UClass.class);
|
||||
if (sourceClass == null) return null;
|
||||
if (forClassReference && isInKotlinSuperTypeList(sourcePsi)) {
|
||||
if (forClassReference && sourceClass.getUastSuperTypes().stream().anyMatch(it -> UastUtils.isPsiAncestor(it, sourceNode))) {
|
||||
UElement parent = sourceClass.getUastParent();
|
||||
if (parent == null) return null;
|
||||
UClass parentClass = UastUtils.findContaining(parent.getSourcePsi(), UClass.class);
|
||||
@@ -211,18 +205,6 @@ public class SuspiciousPackagePrivateAccessInspection extends AbstractBaseUastLo
|
||||
return sourceClass.getJavaPsi();
|
||||
}
|
||||
|
||||
private static boolean isInKotlinSuperTypeList(PsiElement psiElement) {
|
||||
PsiElement place = psiElement;
|
||||
while (place != null) {
|
||||
String className = place.getClass().getName();
|
||||
if (className.equals("org.jetbrains.kotlin.psi.KtSuperTypeList")) {
|
||||
return true;
|
||||
}
|
||||
place = place.getContext();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Tag("modules-set")
|
||||
public static class ModulesSet {
|
||||
@XCollection(elementName = "module", valueAttributeName = "name")
|
||||
|
||||
@@ -124,6 +124,12 @@ fun <T : UElement> PsiElement?.findContaining(clazz: Class<T>): T? {
|
||||
return null
|
||||
}
|
||||
|
||||
fun isPsiAncestor(ancestor: UElement, child: UElement): Boolean {
|
||||
val ancestorPsi = ancestor.sourcePsi ?: return false
|
||||
val childPsi = child.sourcePsi ?: return false
|
||||
return PsiTreeUtil.isAncestor(ancestorPsi, childPsi, false)
|
||||
}
|
||||
|
||||
fun UElement.isUastChildOf(probablyParent: UElement?, strict: Boolean = false): Boolean {
|
||||
tailrec fun isChildOf(current: UElement?, probablyParent: UElement): Boolean {
|
||||
return when (current) {
|
||||
|
||||
Reference in New Issue
Block a user