mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-223097 Provide an inspection which reports overriding package-private methods located in different module
GitOrigin-RevId: 2708657ea214765cf92f259a6de83beb6275ad5e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
544ddcc12b
commit
9c33f8acc2
+13
@@ -0,0 +1,13 @@
|
||||
package xxx;
|
||||
|
||||
public abstract class PackagePrivateAbstractMethod {
|
||||
|
||||
abstract void foo();
|
||||
|
||||
void bar() {}
|
||||
|
||||
protected void baz() {}
|
||||
|
||||
public void qux() {}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package xxx;
|
||||
|
||||
class OverridePackagePrivateMethod : PackagePrivateAbstractMethod() {
|
||||
|
||||
override fun <warning descr="Method foo() overrides a package-private method from class xxx.PackagePrivateAbstractMethod which is declared in a different module 'dep'">foo</warning>() {}
|
||||
|
||||
override fun <warning descr="Method bar() overrides a package-private method from class xxx.PackagePrivateAbstractMethod which is declared in a different module 'dep'">bar</warning>() {}
|
||||
|
||||
override protected fun baz() {}
|
||||
|
||||
override fun qux() {}
|
||||
}
|
||||
+4
@@ -18,5 +18,9 @@ class KtSuspiciousPackagePrivateAccessInspectionTest : SuspiciousPackagePrivateA
|
||||
doTestWithDependency()
|
||||
}
|
||||
|
||||
fun testOverridePackagePrivateMethod() {
|
||||
doTestWithDependency()
|
||||
}
|
||||
|
||||
override fun getBasePath() = "${JvmAnalysisKtTestsUtil.TEST_DATA_PROJECT_RELATIVE_BASE_PATH}/codeInspection/suspiciousPackagePrivateAccess"
|
||||
}
|
||||
+29
@@ -90,6 +90,11 @@ public class SuspiciousPackagePrivateAccessInspection extends AbstractBaseUastLo
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processMethodOverriding(@NotNull UMethod method, @NotNull PsiMethod targetElement) {
|
||||
checkOverridePackageLocal(method, targetElement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processConstructorInvocation(@NotNull UElement sourceNode,
|
||||
@NotNull PsiClass instantiatedClass,
|
||||
@@ -147,6 +152,30 @@ public class SuspiciousPackagePrivateAccessInspection extends AbstractBaseUastLo
|
||||
}
|
||||
}
|
||||
|
||||
private void checkOverridePackageLocal(@NotNull UMethod sourceNode, @NotNull PsiJvmMember targetElement) {
|
||||
PsiMethod sourcePsi = sourceNode.getPsi();
|
||||
if (targetElement.hasModifier(JvmModifier.PACKAGE_LOCAL)) {
|
||||
Module targetModule = ModuleUtilCore.findModuleForPsiElement(targetElement);
|
||||
Module sourceModule = ModuleUtilCore.findModuleForPsiElement(sourcePsi);
|
||||
if (isPackageLocalAccessSuspicious(sourceModule, targetModule)) {
|
||||
List<IntentionAction> fixes =
|
||||
JvmElementActionFactories.createModifierActions(targetElement, MemberRequestsKt.modifierRequest(JvmModifier.PUBLIC, true));
|
||||
String elementDescription =
|
||||
StringUtil.removeHtmlTags(StringUtil.capitalize(RefactoringUIUtil.getDescription(targetElement, false)));
|
||||
final String classDescription =
|
||||
StringUtil.removeHtmlTags(RefactoringUIUtil.getDescription(targetElement.getParent(), false));
|
||||
LocalQuickFix[] quickFixes =
|
||||
IntentionWrapper.wrapToQuickFixes(fixes.toArray(IntentionAction.EMPTY_ARRAY), targetElement.getContainingFile());
|
||||
String problem = elementDescription + " overrides a package-private method from " + classDescription +
|
||||
" which is declared in a different module '" + targetModule.getName() + "'";
|
||||
myProblemsHolder.registerProblem(sourcePsi.getNameIdentifier(), problem,
|
||||
ArrayUtil.append(quickFixes,
|
||||
new MarkModulesAsLoadedTogetherFix(sourceModule.getName(),
|
||||
targetModule.getName())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPackageLocalAccessSuspicious(Module sourceModule, Module targetModule) {
|
||||
if (targetModule == null || sourceModule == null || targetModule.equals(sourceModule)) {
|
||||
return false;
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package xxx;
|
||||
|
||||
public abstract class PackagePrivateAbstractMethod {
|
||||
|
||||
abstract void foo();
|
||||
|
||||
void bar() {}
|
||||
|
||||
protected void baz() {}
|
||||
|
||||
public void qux() {}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package xxx;
|
||||
|
||||
class OverridePackagePrivateMethod extends PackagePrivateAbstractMethod {
|
||||
|
||||
@Override
|
||||
public void <warning descr="Method foo() overrides a package-private method from class xxx.PackagePrivateAbstractMethod which is declared in a different module 'dep'">foo</warning>() {}
|
||||
|
||||
@Override
|
||||
public void <warning descr="Method bar() overrides a package-private method from class xxx.PackagePrivateAbstractMethod which is declared in a different module 'dep'">bar</warning>() {}
|
||||
|
||||
@Override
|
||||
public void baz() {}
|
||||
|
||||
@Override
|
||||
public void qux() {}
|
||||
|
||||
}
|
||||
+4
@@ -19,4 +19,8 @@ public class SuspiciousPackagePrivateAccessInspectionTest extends SuspiciousPack
|
||||
public void testAccessingPackagePrivateInSignatures() {
|
||||
doTestWithDependency();
|
||||
}
|
||||
|
||||
public void testOverridePackagePrivateMethod() {
|
||||
doTestWithDependency();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user