summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com/intellij/codeInsight/ConditionCheckManager.java
blob: f7edb1e3b250f73b8375fb00dc0ab5f2961207d4 (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
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * 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.intellij.codeInsight;

import com.intellij.openapi.components.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:johnnyclark@gmail.com">Johnny Clark</a>
 *         Creation Date: 8/3/12
 */
@State(
  name = "ConditionCheckManager",
  storages = {
    @Storage(id = "dir", file = StoragePathMacros.PROJECT_CONFIG_DIR + "/checker.xml", scheme = StorageScheme.DIRECTORY_BASED),
    @Storage(file = StoragePathMacros.PROJECT_FILE)
  }
)
public class ConditionCheckManager implements PersistentStateComponent<ConditionCheckManager.State> {
  @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) private State state;
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.ConditionCheckManager");

  private List<ConditionChecker> myIsNullCheckMethods = new ArrayList<ConditionChecker>();
  private List<ConditionChecker> myIsNotNullCheckMethods = new ArrayList<ConditionChecker>();

  private List<ConditionChecker> myAssertIsNullMethods = new ArrayList<ConditionChecker>();
  private List<ConditionChecker> myAssertIsNotNullMethods = new ArrayList<ConditionChecker>();

  private List<ConditionChecker> myAssertTrueMethods = new ArrayList<ConditionChecker>();
  private List<ConditionChecker> myAssertFalseMethods = new ArrayList<ConditionChecker>();

  public static ConditionCheckManager getInstance(Project project) {
    return ServiceManager.getService(project, ConditionCheckManager.class);
  }

  public void setIsNullCheckMethods(List<ConditionChecker> methodConditionChecks) {
    myIsNullCheckMethods.clear();
    myIsNullCheckMethods.addAll(methodConditionChecks);
  }

  public void setIsNotNullCheckMethods(List<ConditionChecker> methodConditionChecks) {
    myIsNotNullCheckMethods.clear();
    myIsNotNullCheckMethods.addAll(methodConditionChecks);
  }

  public void setAssertIsNullMethods(List<ConditionChecker> methodConditionChecks) {
    myAssertIsNullMethods.clear();
    myAssertIsNullMethods.addAll(methodConditionChecks);
  }

  public void setAssertIsNotNullMethods(List<ConditionChecker> methodConditionChecks) {
    myAssertIsNotNullMethods.clear();
    myAssertIsNotNullMethods.addAll(methodConditionChecks);
  }

  public void setAssertTrueMethods(List<ConditionChecker> psiMethodWrappers) {
    myAssertTrueMethods.clear();
    myAssertTrueMethods.addAll(psiMethodWrappers);
  }

  public void setAssertFalseMethods(List<ConditionChecker> psiMethodWrappers) {
    myAssertFalseMethods.clear();
    myAssertFalseMethods.addAll(psiMethodWrappers);
  }

  public List<ConditionChecker> getIsNullCheckMethods() {
    return myIsNullCheckMethods;
  }

  public List<ConditionChecker> getIsNotNullCheckMethods() {
    return myIsNotNullCheckMethods;
  }

  public List<ConditionChecker> getAssertIsNullMethods() {
    return myAssertIsNullMethods;
  }

  public List<ConditionChecker> getAssertIsNotNullMethods() {
    return myAssertIsNotNullMethods;
  }

  public List<ConditionChecker> getAssertFalseMethods() {
    return myAssertFalseMethods;
  }

  public List<ConditionChecker> getAssertTrueMethods() {
    return myAssertTrueMethods;
  }

  public static class State {
    public List<String> myIsNullCheckMethods = new ArrayList<String>();
    public List<String> myIsNotNullCheckMethods = new ArrayList<String>();
    public List<String> myAssertIsNullMethods = new ArrayList<String>();
    public List<String> myAssertIsNotNullMethods = new ArrayList<String>();
    public List<String> myAssertTrueMethods = new ArrayList<String>();
    public List<String> myAssertFalseMethods = new ArrayList<String>();
  }

  @Override
  public State getState() {
    State state = new State();

    loadMethodChecksToState(state.myIsNullCheckMethods, myIsNullCheckMethods);
    loadMethodChecksToState(state.myIsNotNullCheckMethods, myIsNotNullCheckMethods);
    loadMethodChecksToState(state.myAssertIsNullMethods, myAssertIsNullMethods);
    loadMethodChecksToState(state.myAssertIsNotNullMethods, myAssertIsNotNullMethods);
    loadMethodChecksToState(state.myAssertTrueMethods, myAssertTrueMethods);
    loadMethodChecksToState(state.myAssertFalseMethods, myAssertFalseMethods);

    return state;
  }

  private static void loadMethodChecksToState(List<String> listToLoadTo, List<ConditionChecker> listToLoadFrom) {
    for (ConditionChecker checker : listToLoadFrom) {
      listToLoadTo.add(checker.toString());
    }
  }

  @Override
  public void loadState(State state) {
    this.state = state;
    loadMethods(myIsNullCheckMethods, state.myIsNullCheckMethods, ConditionChecker.Type.IS_NULL_METHOD);
    loadMethods(myIsNotNullCheckMethods, state.myIsNotNullCheckMethods, ConditionChecker.Type.IS_NOT_NULL_METHOD);
    loadMethods(myAssertIsNullMethods, state.myAssertIsNullMethods, ConditionChecker.Type.ASSERT_IS_NULL_METHOD);
    loadMethods(myAssertIsNotNullMethods, state.myAssertIsNotNullMethods, ConditionChecker.Type.ASSERT_IS_NOT_NULL_METHOD);
    loadMethods(myAssertTrueMethods, state.myAssertTrueMethods, ConditionChecker.Type.ASSERT_TRUE_METHOD);
    loadMethods(myAssertFalseMethods, state.myAssertFalseMethods, ConditionChecker.Type.ASSERT_FALSE_METHOD);
  }

  private static void loadMethods(List<ConditionChecker> listToLoadTo, List<String> listToLoadFrom, ConditionChecker.Type type){
    listToLoadTo.clear();
    for (String setting : listToLoadFrom) {
      try {
        listToLoadTo.add(new ConditionChecker.FromConfigBuilder(setting, type).build());
      } catch (Exception e) {
        LOG.error("Problem occurred while attempting to load Condition Check from configuration file. " + e.getMessage());
      }
    }
  }

  @Nullable
  public static ConditionChecker findConditionChecker(@NotNull PsiMethod method) {
    ConditionCheckManager instance = getInstance(method.getProject());
    ConditionChecker checker = methodMatches(method, instance.getIsNullCheckMethods());
    if (checker == null) checker = methodMatches(method, instance.getIsNotNullCheckMethods());
    if (checker == null) checker = methodMatches(method, instance.getAssertIsNullMethods());
    if (checker == null) checker = methodMatches(method, instance.getAssertIsNotNullMethods());
    if (checker == null) checker = methodMatches(method, instance.getAssertTrueMethods());
    if (checker == null) checker = methodMatches(method, instance.getAssertFalseMethods());
    return checker;
  }

  private static ConditionChecker methodMatches(PsiMethod psiMethod, List<ConditionChecker> checkers) {
    for (ConditionChecker checker : checkers) {
      if (checker.matchesPsiMethod(psiMethod)) {
        return checker;
      }
    }
    return null;
  }

}