mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-82642 (Inspection on java file takes 20+ seconds)
This commit is contained in:
+34
-45
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2005-2011 Bas Leijdekkers
|
||||
* Copyright 2005-2012 Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,7 +16,10 @@
|
||||
package com.siyeh.ig.abstraction;
|
||||
|
||||
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.PsiSearchHelper;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.Processor;
|
||||
@@ -28,7 +31,7 @@ import com.siyeh.ig.psiutils.ClassUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.*;
|
||||
|
||||
public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
|
||||
@@ -44,8 +47,7 @@ public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"method.only.used.from.inner.class.display.name");
|
||||
return InspectionGadgetsBundle.message("method.only.used.from.inner.class.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,13 +57,9 @@ public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
final String name = element.getName();
|
||||
if (infos.length > 1) {
|
||||
if (Boolean.TRUE.equals(infos[1])) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"method.only.used.from.inner.class.problem.descriptor.anonymous.extending",
|
||||
name);
|
||||
return InspectionGadgetsBundle.message("method.only.used.from.inner.class.problem.descriptor.anonymous.extending", name);
|
||||
}
|
||||
return InspectionGadgetsBundle.message(
|
||||
"method.only.used.from.inner.class.problem.descriptor.anonymous.implementing",
|
||||
name);
|
||||
return InspectionGadgetsBundle.message("method.only.used.from.inner.class.problem.descriptor.anonymous.implementing", name);
|
||||
}
|
||||
return InspectionGadgetsBundle.message(
|
||||
"method.only.used.from.inner.class.problem.descriptor", name);
|
||||
@@ -70,17 +68,12 @@ public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
@Override
|
||||
@Nullable
|
||||
public JComponent createOptionsPanel() {
|
||||
final MultipleCheckboxOptionsPanel panel =
|
||||
new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message(
|
||||
"method.only.used.from.inner.class.ignore.option"),
|
||||
final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message("method.only.used.from.inner.class.ignore.option"),
|
||||
"ignoreMethodsAccessedFromAnonymousClass");
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message(
|
||||
"ignore.static.methods.accessed.from.a.non.static.inner.class"),
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message("ignore.static.methods.accessed.from.a.non.static.inner.class"),
|
||||
"ignoreStaticMethodsFromNonStaticInnerClass");
|
||||
panel.addCheckbox(
|
||||
InspectionGadgetsBundle.message("only.report.static.methods"),
|
||||
"onlyReportStaticMethods");
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message("only.report.static.methods"), "onlyReportStaticMethods");
|
||||
return panel;
|
||||
}
|
||||
|
||||
@@ -95,47 +88,39 @@ public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
@Override
|
||||
public void visitMethod(PsiMethod method) {
|
||||
super.visitMethod(method);
|
||||
if (!method.hasModifierProperty(PsiModifier.PRIVATE) ||
|
||||
method.isConstructor()) {
|
||||
if (!method.hasModifierProperty(PsiModifier.PRIVATE) || method.isConstructor()) {
|
||||
return;
|
||||
}
|
||||
if (onlyReportStaticMethods &&
|
||||
!method.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
if (onlyReportStaticMethods && !method.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
return;
|
||||
}
|
||||
if (method.getNameIdentifier() == null) {
|
||||
return;
|
||||
}
|
||||
final MethodReferenceFinder processor =
|
||||
new MethodReferenceFinder(method);
|
||||
final MethodReferenceFinder processor = new MethodReferenceFinder(method);
|
||||
if (!processor.isOnlyAccessedFromInnerClass()) {
|
||||
return;
|
||||
}
|
||||
final PsiClass containingClass = processor.getContainingClass();
|
||||
if (ignoreStaticMethodsFromNonStaticInnerClass &&
|
||||
method.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
if (ignoreStaticMethodsFromNonStaticInnerClass && method.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
final PsiElement parent = containingClass.getParent();
|
||||
if (parent instanceof PsiClass &&
|
||||
!containingClass.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
if (parent instanceof PsiClass && !containingClass.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (containingClass instanceof PsiAnonymousClass) {
|
||||
final PsiClass[] interfaces =
|
||||
containingClass.getInterfaces();
|
||||
final PsiClass[] interfaces = containingClass.getInterfaces();
|
||||
final PsiClass superClass;
|
||||
if (interfaces.length == 1) {
|
||||
superClass = interfaces[0];
|
||||
registerMethodError(method, superClass,
|
||||
Boolean.valueOf(false));
|
||||
registerMethodError(method, superClass, Boolean.valueOf(false));
|
||||
}
|
||||
else {
|
||||
superClass = containingClass.getSuperClass();
|
||||
if (superClass == null) {
|
||||
return;
|
||||
}
|
||||
registerMethodError(method, superClass,
|
||||
Boolean.valueOf(true));
|
||||
registerMethodError(method, superClass, Boolean.valueOf(true));
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -160,22 +145,18 @@ public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
@Override
|
||||
public boolean process(PsiReference reference) {
|
||||
final PsiElement element = reference.getElement();
|
||||
final PsiMethod containingMethod =
|
||||
PsiTreeUtil.getParentOfType(element, PsiMethod.class);
|
||||
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
|
||||
if (method.equals(containingMethod)) {
|
||||
return true;
|
||||
}
|
||||
final PsiClass containingClass =
|
||||
ClassUtils.getContainingClass(element);
|
||||
final PsiClass containingClass = ClassUtils.getContainingClass(element);
|
||||
if (containingClass == null) {
|
||||
onlyAccessedFromInnerClass = false;
|
||||
return false;
|
||||
}
|
||||
if (containingClass instanceof PsiAnonymousClass) {
|
||||
final PsiAnonymousClass anonymousClass =
|
||||
(PsiAnonymousClass)containingClass;
|
||||
final PsiExpressionList argumentList =
|
||||
anonymousClass.getArgumentList();
|
||||
final PsiAnonymousClass anonymousClass = (PsiAnonymousClass)containingClass;
|
||||
final PsiExpressionList argumentList = anonymousClass.getArgumentList();
|
||||
if (PsiTreeUtil.isAncestor(argumentList, element, true)) {
|
||||
onlyAccessedFromInnerClass = false;
|
||||
return false;
|
||||
@@ -191,8 +172,7 @@ public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!PsiTreeUtil.isAncestor(methodClass, containingClass,
|
||||
true)) {
|
||||
else if (!PsiTreeUtil.isAncestor(methodClass, containingClass, true)) {
|
||||
onlyAccessedFromInnerClass = false;
|
||||
return false;
|
||||
}
|
||||
@@ -202,6 +182,15 @@ public class MethodOnlyUsedFromInnerClassInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
public boolean isOnlyAccessedFromInnerClass() {
|
||||
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(method.getProject());
|
||||
final ProgressManager progressManager = ProgressManager.getInstance();
|
||||
final ProgressIndicator progressIndicator = progressManager.getProgressIndicator();
|
||||
final PsiSearchHelper.SearchCostResult searchCost =
|
||||
searchHelper.isCheapEnoughToSearch(method.getName(), method.getResolveScope(), null, progressIndicator);
|
||||
if (searchCost == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES ||
|
||||
searchCost == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) {
|
||||
return onlyAccessedFromInnerClass;
|
||||
}
|
||||
final Query<PsiReference> query = ReferencesSearch.search(method);
|
||||
query.forEach(this);
|
||||
return onlyAccessedFromInnerClass;
|
||||
|
||||
Reference in New Issue
Block a user