summaryrefslogtreecommitdiff
path: root/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/IgnoreResultOfCallInspection.java
blob: c476f9a6af381d8fc53eb26d35681f8baa39c826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.bugs;

import com.intellij.codeInspection.ui.ListTable;
import com.intellij.codeInspection.ui.ListWrappingTableModel;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.ui.CheckBox;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.LibraryUtil;
import com.siyeh.ig.ui.UiUtils;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class IgnoreResultOfCallInspection extends BaseInspection {

  /**
   * @noinspection PublicField
   */
  public boolean m_reportAllNonLibraryCalls = false;

  /**
   * @noinspection PublicField
   */
  @NonNls public String callCheckString = "java.io.InputStream,read," +
                                          "java.io.InputStream,skip," +
                                          "java.lang.StringBuffer,toString," +
                                          "java.lang.StringBuilder,toString," +
                                          "java.lang.String,.*," +
                                          "java.math.BigInteger,.*," +
                                          "java.math.BigDecimal,.*," +
                                          "java.net.InetAddress,.*," +
                                          "java.io.File,.*," +
                                          "java.lang.Object,equals|hashCode";

  final List<String> methodNamePatterns = new ArrayList();
  final List<String> classNames = new ArrayList();
  Map<String, Pattern> patternCache = null;

  public IgnoreResultOfCallInspection() {
    parseString(callCheckString, classNames, methodNamePatterns);
  }

  @Override
  @NotNull
  public String getID() {
    return "ResultOfMethodCallIgnored";
  }

  @Override
  @NotNull
  public String getDisplayName() {
    return InspectionGadgetsBundle.message("result.of.method.call.ignored.display.name");
  }

  @Override
  @NotNull
  public String buildErrorString(Object... infos) {
    final PsiClass containingClass = (PsiClass)infos[0];
    final String className = containingClass.getName();
    return InspectionGadgetsBundle.message("result.of.method.call.ignored.problem.descriptor", className);
  }

  @Override
  public void readSettings(Element element) throws InvalidDataException {
    super.readSettings(element);
    parseString(callCheckString, classNames, methodNamePatterns);
  }

  @Override
  public void writeSettings(Element element) throws WriteExternalException {
    callCheckString = formatString(classNames, methodNamePatterns);
    super.writeSettings(element);
  }

  @Override
  public JComponent createOptionsPanel() {
    final JPanel panel = new JPanel(new BorderLayout());
    final ListTable table = new ListTable(new ListWrappingTableModel(
      Arrays.asList(classNames, methodNamePatterns), InspectionGadgetsBundle.message("result.of.method.call.ignored.class.column.title"),
      InspectionGadgetsBundle.message("result.of.method.call.ignored.method.column.title")));
    final JPanel tablePanel = UiUtils.createAddRemovePanel(table);
    final CheckBox checkBox =
      new CheckBox(InspectionGadgetsBundle.message("result.of.method.call.ignored.non.library.option"), this, "m_reportAllNonLibraryCalls");
    panel.add(tablePanel, BorderLayout.CENTER);
    panel.add(checkBox, BorderLayout.SOUTH);
    return panel;
  }

  @Override
  public boolean isEnabledByDefault() {
    return true;
  }

  @Override
  public BaseInspectionVisitor buildVisitor() {
    return new IgnoreResultOfCallVisitor();
  }

  private class IgnoreResultOfCallVisitor extends BaseInspectionVisitor {

    @Override
    public void visitExpressionStatement(@NotNull PsiExpressionStatement statement) {
      super.visitExpressionStatement(statement);
      final PsiExpression expression = statement.getExpression();
      if (!(expression instanceof PsiMethodCallExpression)) {
        return;
      }
      final PsiMethodCallExpression call = (PsiMethodCallExpression)expression;
      final PsiMethod method = call.resolveMethod();
      if (method == null || method.isConstructor()) {
        return;
      }
      final PsiType returnType = method.getReturnType();
      if (PsiType.VOID.equals(returnType)) {
        return;
      }
      final PsiClass aClass = method.getContainingClass();
      if (aClass == null) {
        return;
      }
      if (PsiUtilCore.hasErrorElementChild(statement)) {
        return;
      }
      if (m_reportAllNonLibraryCalls && !LibraryUtil.classIsInLibrary(aClass)) {
        registerMethodCallError(call, aClass);
        return;
      }
      final PsiReferenceExpression methodExpression = call.getMethodExpression();
      final String methodName = methodExpression.getReferenceName();
      if (methodName == null) {
        return;
      }
      for (int i = 0; i < methodNamePatterns.size(); i++) {
        final String methodNamePattern = methodNamePatterns.get(i);
        if (!methodNamesMatch(methodName, methodNamePattern)) {
          continue;
        }
        final String className = classNames.get(i);
        if (!InheritanceUtil.isInheritor(aClass, className)) {
          continue;
        }
        registerMethodCallError(call, aClass);
        return;
      }
    }

    private boolean methodNamesMatch(String methodName, String methodNamePattern) {
      Pattern pattern;
      if (patternCache != null) {
        pattern = patternCache.get(methodNamePattern);
      }
      else {
        patternCache = new HashMap(methodNamePatterns.size());
        pattern = null;
      }
      if (pattern == null) {
        try {
          pattern = Pattern.compile(methodNamePattern);
          patternCache.put(methodNamePattern, pattern);
        }
        catch (PatternSyntaxException ignore) {
          return false;
        }
        catch (NullPointerException ignore) {
          return false;
        }
      }
      if (pattern == null) {
        return false;
      }
      final Matcher matcher = pattern.matcher(methodName);
      return matcher.matches();
    }
  }
}