summaryrefslogtreecommitdiff
path: root/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ThrowableInstanceNeverThrownInspection.java
blob: 3604e3face5eb21c9b7dcd197f7369cc383f70ec (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
/*
 * Copyright 2007-2011 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.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.Query;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

public class ThrowableInstanceNeverThrownInspection extends BaseInspection {

  @Override
  @Nls
  @NotNull
  public String getDisplayName() {
    return InspectionGadgetsBundle.message(
      "throwable.instance.never.thrown.display.name");
  }

  @Override
  @NotNull
  protected String buildErrorString(Object... infos) {
    final PsiExpression expression = (PsiExpression)infos[0];
    final String type =
      TypeUtils.expressionHasTypeOrSubtype(expression,
                                           CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION,
                                           CommonClassNames.JAVA_LANG_EXCEPTION,
                                           CommonClassNames.JAVA_LANG_ERROR);
    if (CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION.equals(type)) {
      return InspectionGadgetsBundle.message(
        "throwable.instance.never.thrown.runtime.exception.problem.descriptor");
    }
    else if (CommonClassNames.JAVA_LANG_EXCEPTION.equals(type)) {
      return InspectionGadgetsBundle.message(
        "throwable.instance.never.thrown.checked.exception.problem.descriptor");
    }
    else if (CommonClassNames.JAVA_LANG_ERROR.equals(type)) {
      return InspectionGadgetsBundle.message(
        "throwable.instance.never.thrown.error.problem.descriptor");
    }
    else {
      return InspectionGadgetsBundle.message(
        "throwable.instance.never.thrown.problem.descriptor");
    }
  }

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

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

  private static class ExceptionInstanceNeverThrownVisitor
    extends BaseInspectionVisitor {

    @Override
    public void visitNewExpression(PsiNewExpression expression) {
      super.visitNewExpression(expression);
      if (!TypeUtils.expressionHasTypeOrSubtype(expression,
                                                CommonClassNames.JAVA_LANG_THROWABLE)) {
        return;
      }
      final PsiElement parent = getParent(expression.getParent());
      if (parent instanceof PsiThrowStatement ||
          parent instanceof PsiReturnStatement) {
        return;
      }
      if (PsiTreeUtil.getParentOfType(parent, PsiCallExpression.class) !=
          null) {
        return;
      }
      final PsiElement typedParent =
        PsiTreeUtil.getParentOfType(expression,
                                    PsiAssignmentExpression.class,
                                    PsiVariable.class);
      final PsiLocalVariable variable;
      if (typedParent instanceof PsiAssignmentExpression) {
        final PsiAssignmentExpression assignmentExpression =
          (PsiAssignmentExpression)typedParent;
        final PsiExpression rhs = assignmentExpression.getRExpression();
        if (!PsiTreeUtil.isAncestor(rhs, expression, false)) {
          return;
        }
        final PsiExpression lhs = assignmentExpression.getLExpression();
        if (!(lhs instanceof PsiReferenceExpression)) {
          return;
        }
        final PsiReferenceExpression referenceExpression =
          (PsiReferenceExpression)lhs;
        final PsiElement target = referenceExpression.resolve();
        if (!(target instanceof PsiLocalVariable)) {
          return;
        }
        variable = (PsiLocalVariable)target;
      }
      else if (typedParent instanceof PsiVariable) {
        if (!(typedParent instanceof PsiLocalVariable)) {
          return;
        }
        variable = (PsiLocalVariable)typedParent;
      }
      else {
        variable = null;
      }
      if (variable != null) {
        final Query<PsiReference> query =
          ReferencesSearch.search(variable,
                                  variable.getUseScope());
        for (PsiReference reference : query) {
          final PsiElement usage = reference.getElement();
          PsiElement usageParent = usage.getParent();
          while (usageParent instanceof PsiParenthesizedExpression) {
            usageParent = usageParent.getParent();
          }
          if (usageParent instanceof PsiThrowStatement ||
              usageParent instanceof PsiReturnStatement) {
            return;
          }
          if (PsiTreeUtil.getParentOfType(usageParent,
                                          PsiCallExpression.class) != null) {
            return;
          }
        }
      }
      registerError(expression, expression);
    }

    public static PsiElement getParent(PsiElement element) {
      PsiElement parent = element;
      while (parent instanceof PsiParenthesizedExpression ||
             parent instanceof PsiConditionalExpression ||
             parent instanceof PsiTypeCastExpression) {
        parent = parent.getParent();
      }
      final PsiElement skipped = skipInitCause(parent);
      if (skipped != null) {
        return getParent(skipped);
      }
      return parent;
    }

    private static PsiElement skipInitCause(PsiElement parent) {
      if (!(parent instanceof PsiReferenceExpression)) {
        return null;
      }
      final PsiElement grandParent = parent.getParent();
      if (!(grandParent instanceof PsiMethodCallExpression)) {
        return null;
      }
      final PsiMethodCallExpression methodCallExpression =
        (PsiMethodCallExpression)grandParent;
      final PsiReferenceExpression methodExpression =
        methodCallExpression.getMethodExpression();
      @NonNls final String methodName =
        methodExpression.getReferenceName();
      if (!"initCause".equals(methodName)) {
        return null;
      }
      final PsiMethod method =
        methodCallExpression.resolveMethod();
      if (method == null) {
        return null;
      }
      final PsiParameterList parameterList =
        method.getParameterList();
      if (parameterList.getParametersCount() != 1) {
        return null;
      }
      final PsiParameter[] parameters =
        parameterList.getParameters();
      final PsiType type = parameters[0].getType();
      if (!type.equalsToText(CommonClassNames.JAVA_LANG_THROWABLE)) {
        return null;
      }
      return getParent(methodCallExpression.getParent());
    }
  }
}