summaryrefslogtreecommitdiff
path: root/plugins/InspectionGadgets/src/com/siyeh/ig/migration/IfStatementBranch.java
blob: 6d5513ac4d3f1a9f114f196105d38b0884497404 (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
/*
 * Copyright 2003-2011 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.migration;

import com.intellij.psi.*;

import java.util.*;

class IfStatementBranch {

  private final Set<String> topLevelVariables = new HashSet<String>(3);
  private final LinkedList<String> comments = new LinkedList<String>();
  private final LinkedList<String> statementComments = new LinkedList<String>();
  private final List<PsiExpression> conditions = new ArrayList<PsiExpression>(3);
  private final PsiStatement statement;
  private final boolean elseBranch;

  public IfStatementBranch(PsiStatement branch, boolean elseBranch) {
    statement = branch;
    this.elseBranch = elseBranch;
    calculateVariablesDeclared(statement);
  }

  public void addComment(String comment) {
    comments.addFirst(comment);
  }

  public void addStatementComment(String comment) {
    statementComments.addFirst(comment);
  }

  public void addCaseExpression(PsiExpression expression) {
    conditions.add(expression);
  }

  public PsiStatement getStatement() {
    return statement;
  }

  public List<PsiExpression> getConditions() {
    return Collections.unmodifiableList(conditions);
  }

  public boolean isElse() {
    return elseBranch;
  }

  public boolean topLevelDeclarationsConflictWith(
    IfStatementBranch testBranch) {
    final Set<String> topLevel = testBranch.topLevelVariables;
    return intersects(topLevelVariables, topLevel);
  }

  private static boolean intersects(Set<String> set1,
                                    Set<String> set2) {
    for (final String s : set1) {
      if (set2.contains(s)) {
        return true;
      }
    }
    return false;
  }

  public List<String> getComments() {
    return comments;
  }

  public List<String> getStatementComments() {
    return statementComments;
  }

  public void calculateVariablesDeclared(PsiStatement statement) {
    if (statement == null) {
      return;
    }
    if (statement instanceof PsiDeclarationStatement) {
      final PsiDeclarationStatement declarationStatement =
        (PsiDeclarationStatement)statement;
      final PsiElement[] elements =
        declarationStatement.getDeclaredElements();
      for (PsiElement element : elements) {
        final PsiVariable variable = (PsiVariable)element;
        final String varName = variable.getName();
        topLevelVariables.add(varName);
      }
    }
    else if (statement instanceof PsiBlockStatement) {
      final PsiBlockStatement block = (PsiBlockStatement)statement;
      final PsiCodeBlock codeBlock = block.getCodeBlock();
      final PsiStatement[] statements = codeBlock.getStatements();
      for (PsiStatement statement1 : statements) {
        calculateVariablesDeclared(statement1);
      }
    }
  }
}