2 * Copyright 2000-2009 JetBrains s.r.o.
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
.intellij
.codeInsight
.guess
.impl
;
18 import com
.intellij
.codeInsight
.CodeInsightUtil
;
19 import com
.intellij
.codeInspection
.dataFlow
.DfaMemoryStateImpl
;
20 import com
.intellij
.codeInspection
.dataFlow
.value
.DfaValue
;
21 import com
.intellij
.codeInspection
.dataFlow
.value
.DfaValueFactory
;
22 import com
.intellij
.psi
.PsiExpression
;
23 import com
.intellij
.psi
.PsiType
;
24 import com
.intellij
.util
.containers
.HashMap
;
25 import gnu
.trove
.THashMap
;
26 import gnu
.trove
.TObjectHashingStrategy
;
33 public class ExpressionTypeMemoryState
extends DfaMemoryStateImpl
{
34 public static final TObjectHashingStrategy
<PsiExpression
> EXPRESSION_HASHING_STRATEGY
= new TObjectHashingStrategy
<PsiExpression
>() {
35 public int computeHashCode(PsiExpression object
) {
36 return object
.getNode().getElementType().hashCode();
39 public boolean equals(PsiExpression o1
, PsiExpression o2
) {
40 return CodeInsightUtil
.areExpressionsEquivalent(o1
, o2
);
43 private final Map
<PsiExpression
, PsiType
> myStates
= new THashMap
<PsiExpression
, PsiType
>(EXPRESSION_HASHING_STRATEGY
);
45 public ExpressionTypeMemoryState(final DfaValueFactory factory
) {
50 protected DfaMemoryStateImpl
createNew() {
51 return new ExpressionTypeMemoryState(getFactory());
55 public DfaMemoryStateImpl
createCopy() {
56 final ExpressionTypeMemoryState copy
= (ExpressionTypeMemoryState
)super.createCopy();
57 copy
.myStates
.putAll(myStates
);
62 public boolean applyCondition(DfaValue dfaCond
) {
63 if (dfaCond
instanceof DfaInstanceofValue
) {
64 final DfaInstanceofValue value
= (DfaInstanceofValue
)dfaCond
;
65 if (!value
.isNegated()) {
66 setExpressionType(value
.getExpression(), value
.getCastType());
70 return super.applyCondition(dfaCond
);
73 public Map
<PsiExpression
, PsiType
> getStates() {
78 public boolean equals(Object o
) {
79 if (this == o
) return true;
80 if (o
== null || getClass() != o
.getClass()) return false;
81 if (!super.equals(o
)) return false;
83 ExpressionTypeMemoryState that
= (ExpressionTypeMemoryState
)o
;
85 if (!myStates
.equals(that
.myStates
)) return false;
91 public int hashCode() {
92 int result
= super.hashCode();
93 result
= 31 * result
+ myStates
.hashCode();
98 public String
toString() {
99 return super.toString() + " states=[" + new HashMap
<PsiExpression
, PsiType
>(myStates
) + "]";
102 public void setExpressionType(PsiExpression expression
, PsiType type
) {
103 myStates
.put(expression
, type
);