IDEA-86646 (inspection "size()==0 is replaceable with isEmpty()" triggers for project level 1.5)

This commit is contained in:
Bas Leijdekkers
2012-08-07 16:06:58 +02:00
parent 785f5fa031
commit 1f5f3abb90
3 changed files with 34 additions and 9 deletions
@@ -1458,6 +1458,8 @@ interface.never.implemented.option=Ignore interfaces which only declare constant
size.replaceable.by.isempty.display.name='size() == 0' replaceable with 'isEmpty()'
size.replaceable.by.isempty.quickfix=Replace with 'isEmpty()'
size.replaceable.by.isempty.negation.ignore.option=Ignore expressions which would be replaced with '!isEmpty()'
ignored.classes.table=Ignored classes
choose.class.type.to.ignore=Choose class type to ignore
loop.condition.not.updated.inside.loop.display.name=Loop variable not updated inside loop
loop.condition.not.updated.inside.loop.problem.descriptor='#ref' is not updated inside loop #loc
utility.class.without.private.constructor.option=Ignore classes with only a main method
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2011 Bas Leijdekkers
* Copyright 2006-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,11 +16,15 @@
package com.siyeh.ig.style;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.codeInspection.ui.ListTable;
import com.intellij.codeInspection.ui.ListWrappingTableModel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.OrderedSet;
import com.intellij.util.ui.CheckBox;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
@@ -28,36 +32,47 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ComparisonUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.ui.UiUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
public class SizeReplaceableByIsEmptyInspection extends BaseInspection {
@SuppressWarnings({"PublicField"})
public boolean ignoreNegations = false;
@SuppressWarnings("PublicField")
public OrderedSet<String> ignoredTypes = new OrderedSet();
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"size.replaceable.by.isempty.display.name");
return InspectionGadgetsBundle.message("size.replaceable.by.isempty.display.name");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"expression.can.be.replaced.problem.descriptor", infos[0]);
return InspectionGadgetsBundle.message("expression.can.be.replaced.problem.descriptor", infos[0]);
}
@Override
@Nullable
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message(
final JComponent panel = new JPanel(new BorderLayout());
final ListTable table =
new ListTable(new ListWrappingTableModel(ignoredTypes, InspectionGadgetsBundle.message("ignored.classes.table")));
JPanel tablePanel =
UiUtils.createAddRemoveTreeClassChooserPanel(table, InspectionGadgetsBundle.message("choose.class.type.to.ignore"));
final CheckBox checkBox = new CheckBox(InspectionGadgetsBundle.message(
"size.replaceable.by.isempty.negation.ignore.option"), this, "ignoreNegations");
panel.add(tablePanel, BorderLayout.CENTER);
panel.add(checkBox, BorderLayout.SOUTH);
return panel;
}
@Override
@@ -71,8 +86,7 @@ public class SizeReplaceableByIsEmptyInspection extends BaseInspection {
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"size.replaceable.by.isempty.quickfix");
return InspectionGadgetsBundle.message("size.replaceable.by.isempty.quickfix");
}
@Override
@@ -195,6 +209,11 @@ public class SizeReplaceableByIsEmptyInspection extends BaseInspection {
if (aClass == null) {
return null;
}
for (String ignoredType : ignoredTypes) {
if (InheritanceUtil.isInheritor(aClass, ignoredType)) {
return null;
}
}
final PsiMethod[] methods = aClass.findMethodsByName("isEmpty", true);
for (PsiMethod method : methods) {
final PsiParameterList parameterList = method.getParameterList();
@@ -4,6 +4,10 @@ This inspection reports any <b>.size()</b> or <b>.length()</b>
comparisons with a <b>0</b> literal which can be replaced with a call to <b>.isEmpty()</b>.
<!-- tooltip end -->
<p>
Use the table below to add classes for which any <b>.size()</b> or <b>.length()</b> comparisons should not be replaced.
<p>
Use the checkbox below to ignore any expressions which would be replaced with '!isEmpty()'.
<p>
<small>Powered by InspectionGadgets</small>
</body>
</html>