summaryrefslogtreecommitdiff
path: root/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/HData.java
blob: 7c938347ccb9f88be6537e1346a526021c5afcd8 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright 2000-2014 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.codeInspection.bytecodeAnalysis;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

/**
 * Small size key, constructed by hashing method signature.
 * @see com.intellij.codeInspection.bytecodeAnalysis.BytecodeAnalysisConverter for details of construction.
 */
final class HKey {
  @NotNull
  final byte[] key;
  final int dirKey;
  final boolean stable;

  HKey(@NotNull byte[] key, int dirKey, boolean stable) {
    this.key = key;
    this.dirKey = dirKey;
    this.stable = stable;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    HKey hKey = (HKey)o;
    if (dirKey != hKey.dirKey) return false;
    if (stable != hKey.stable) return false;
    if (!Arrays.equals(key, hKey.key)) return false;
    return true;
  }

  @Override
  public int hashCode() {
    int result = Arrays.hashCode(key);
    result = 31 * result + dirKey;
    result = 31 * result + (stable ? 1 : 0);
    return result;
  }

  HKey negate() {
    return new HKey(key, dirKey, !stable);
  }

  HKey mkStable() {
    return stable ? this : new HKey(key, dirKey, true);
  }

  HKey mkUnstable() {
    return stable ? new HKey(key, dirKey, false) : this;
  }

  public HKey mkBase() {
    return dirKey == 0 ? this : new HKey(key, 0, stable);
  }

  HKey updateDirection(int newDirKey) {
    return new HKey(key, newDirKey, stable);
  }
}

final class HComponent {
  @NotNull Value value;
  @NotNull final HKey[] ids;

  HComponent(@NotNull Value value, @NotNull HKey[] ids) {
    this.value = value;
    this.ids = ids;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    HComponent that = (HComponent)o;

    if (!Arrays.equals(ids, that.ids)) return false;
    if (value != that.value) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = value.hashCode();
    result = 31 * result + Arrays.hashCode(ids);
    return result;
  }

  public boolean remove(@NotNull HKey id) {
    return HUtils.remove(ids, id);
  }

  public boolean isEmpty() {
    return HUtils.isEmpty(ids);
  }

  @NotNull
  public HComponent copy() {
    return new HComponent(value, ids.clone());
  }
}

class HUtils {

  static boolean isEmpty(HKey[] ids) {
    for (HKey id : ids) {
      if (id != null) return false;
    }
    return true;
  }

  static boolean remove(HKey[] ids, @NotNull HKey id) {
    boolean removed = false;
    for (int i = 0; i < ids.length; i++) {
      if (id.equals(ids[i])) {
        ids[i] = null;
        removed = true;
      }
    }
    return removed;
  }
}

final class HEquation {
  @NotNull final HKey key;
  @NotNull final HResult result;

  HEquation(@NotNull HKey key, @NotNull HResult result) {
    this.key = key;
    this.result = result;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    HEquation hEquation = (HEquation)o;
    if (!key.equals(hEquation.key)) return false;
    if (!result.equals(hEquation.result)) return false;
    return true;
  }

  @Override
  public int hashCode() {
    int result1 = key.hashCode();
    result1 = 31 * result1 + result.hashCode();
    return result1;
  }
}
class Bytes {
  @NotNull
  final byte[] bytes;
  Bytes(@NotNull byte[] bytes) {
    this.bytes = bytes;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Bytes bytes1 = (Bytes)o;

    if (!Arrays.equals(bytes, bytes1.bytes)) return false;

    return true;
  }

  @Override
  public int hashCode() {
    return Arrays.hashCode(bytes);
  }
}

class HEquations {
  @NotNull final List<DirectionResultPair> results;
  final boolean stable;

  HEquations(@NotNull List<DirectionResultPair> results, boolean stable) {
    this.results = results;
    this.stable = stable;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    HEquations that = (HEquations)o;

    if (stable != that.stable) return false;
    if (!results.equals(that.results)) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = results.hashCode();
    result = 31 * result + (stable ? 1 : 0);
    return result;
  }
}

class DirectionResultPair {
  final int directionKey;
  @NotNull
  final HResult hResult;

  DirectionResultPair(int directionKey, @NotNull HResult hResult) {
    this.directionKey = directionKey;
    this.hResult = hResult;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    DirectionResultPair that = (DirectionResultPair)o;

    if (directionKey != that.directionKey) return false;
    if (!hResult.equals(that.hResult)) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = directionKey;
    result = 31 * result + hResult.hashCode();
    return result;
  }
}

interface HResult {}
final class HFinal implements HResult {
  @NotNull final Value value;

  HFinal(@NotNull Value value) {
    this.value = value;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    HFinal hFinal = (HFinal)o;

    if (value != hFinal.value) return false;

    return true;
  }

  @Override
  public int hashCode() {
    return value.ordinal();
  }
}

final class HPending implements HResult {
  @NotNull final HComponent[] delta;

  HPending(@NotNull HComponent[] delta) {
    this.delta = delta;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    HPending hPending = (HPending)o;
    if (!Arrays.equals(delta, hPending.delta)) return false;
    return true;
  }

  @Override
  public int hashCode() {
    return Arrays.hashCode(delta);
  }

  @NotNull
  HPending copy() {
    HComponent[] delta1 = new HComponent[delta.length];
    for (int i = 0; i < delta.length; i++) {
      delta1[i] = delta[i].copy();

    }
    return new HPending(delta1);
  }
}