2 * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.siyeh
.ig
.bugs
;
18 import com
.intellij
.psi
.*;
19 import com
.siyeh
.HardcodedMethodConstants
;
20 import com
.siyeh
.InspectionGadgetsBundle
;
21 import com
.siyeh
.ig
.BaseInspection
;
22 import com
.siyeh
.ig
.BaseInspectionVisitor
;
23 import com
.siyeh
.ig
.psiutils
.ExceptionUtils
;
24 import com
.siyeh
.ig
.psiutils
.IteratorUtils
;
25 import com
.siyeh
.ig
.psiutils
.MethodUtils
;
26 import org
.jetbrains
.annotations
.NotNull
;
30 public class IteratorNextDoesNotThrowNoSuchElementExceptionInspection
31 extends BaseInspection
{
34 public String
getID(){
35 return "IteratorNextCanNotThrowNoSuchElementException";
39 public String
getDisplayName(){
40 return InspectionGadgetsBundle
.message(
41 "iterator.next.does.not.throw.nosuchelementexception.display.name");
45 public String
buildErrorString(Object
... infos
){
46 return InspectionGadgetsBundle
.message(
47 "iterator.next.does.not.throw.nosuchelementexception.problem.descriptor");
50 public BaseInspectionVisitor
buildVisitor(){
51 return new IteratorNextDoesNotThrowNoSuchElementExceptionVisitor();
54 private static class IteratorNextDoesNotThrowNoSuchElementExceptionVisitor
55 extends BaseInspectionVisitor
{
57 @Override public void visitMethod(@NotNull PsiMethod method
){
58 // note: no call to super
59 if (!MethodUtils
.methodMatches(method
, "java.util.Iterator", null,
60 HardcodedMethodConstants
.NEXT
)) {
63 final Set
<PsiType
> exceptions
=
64 ExceptionUtils
.calculateExceptionsThrown(method
);
65 for (final PsiType exception
: exceptions
) {
66 if (exception
.equalsToText(
67 "java.util.NoSuchElementException")) {
71 if(IteratorUtils
.containsCallToIteratorNext(method
, null, false)){
74 final CalledMethodsVisitor visitor
= new CalledMethodsVisitor();
75 method
.accept(visitor
);
76 if (visitor
.isNoSuchElementExceptionThrown()) {
79 registerMethodError(method
);
83 private static class CalledMethodsVisitor
84 extends JavaRecursiveElementVisitor
{
86 private boolean noSuchElementExceptionThrown
= false;
88 @Override public void visitMethodCallExpression(
89 PsiMethodCallExpression expression
) {
90 if (noSuchElementExceptionThrown
) {
93 super.visitMethodCallExpression(expression
);
94 final PsiReferenceExpression methodExpression
=
95 expression
.getMethodExpression();
96 final PsiElement method
= methodExpression
.resolve();
100 final Set
<PsiType
> exceptions
=
101 ExceptionUtils
.calculateExceptionsThrown(method
);
102 for (final PsiType exception
: exceptions
) {
103 if (exception
.equalsToText(
104 "java.util.NoSuchElementException")) {
105 noSuchElementExceptionThrown
= true;
110 public boolean isNoSuchElementExceptionThrown() {
111 return noSuchElementExceptionThrown
;