IDEADEV-31824 (Incorrect "manual array copy" warning)
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / methodmetrics / CyclomaticComplexityVisitor.java
blob34bfa893976e8b691ea5d90f6e4476f5cd1f11bb
1 /*
2 * Copyright 2003-2005 Dave Griffith
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.methodmetrics;
18 import com.intellij.psi.*;
19 import org.jetbrains.annotations.NotNull;
21 class CyclomaticComplexityVisitor extends JavaRecursiveElementVisitor {
22 private int m_complexity = 1;
24 @Override public void visitAnonymousClass(@NotNull PsiAnonymousClass aClass) {
25 // to call to super, to keep this from drilling down
28 @Override public void visitForStatement(@NotNull PsiForStatement statement) {
29 super.visitForStatement(statement);
30 m_complexity++;
33 @Override public void visitForeachStatement(@NotNull PsiForeachStatement statement) {
34 super.visitForeachStatement(statement);
35 m_complexity++;
38 @Override public void visitIfStatement(@NotNull PsiIfStatement statement) {
39 super.visitIfStatement(statement);
40 m_complexity++;
43 @Override public void visitDoWhileStatement(@NotNull PsiDoWhileStatement statement) {
44 super.visitDoWhileStatement(statement);
45 m_complexity++;
48 @Override public void visitConditionalExpression(PsiConditionalExpression expression) {
49 super.visitConditionalExpression(expression);
50 m_complexity++;
53 @Override public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) {
54 super.visitSwitchStatement(statement);
55 final PsiCodeBlock body = statement.getBody();
56 if (body == null) {
57 return;
59 final PsiStatement[] statements = body.getStatements();
60 boolean pendingLabel = false;
61 for(final PsiStatement child : statements){
62 if(child instanceof PsiSwitchLabelStatement){
63 if(!pendingLabel){
64 m_complexity++;
66 pendingLabel = true;
67 } else{
68 pendingLabel = false;
73 @Override public void visitWhileStatement(@NotNull PsiWhileStatement statement) {
74 super.visitWhileStatement(statement);
75 m_complexity++;
78 public int getComplexity() {
79 return m_complexity;