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
.codeInspection
.dataFlow
.value
;
18 import com
.intellij
.psi
.PsiVariable
;
19 import com
.intellij
.util
.containers
.HashMap
;
20 import gnu
.trove
.THashMap
;
21 import org
.jetbrains
.annotations
.NonNls
;
22 import org
.jetbrains
.annotations
.NotNull
;
23 import org
.jetbrains
.annotations
.Nullable
;
27 public class DfaBoxedValue
extends DfaValue
{
28 private final DfaValue myWrappedValue
;
30 private DfaBoxedValue(DfaValue valueToWrap
, DfaValueFactory factory
) {
32 myWrappedValue
= valueToWrap
;
36 public String
toString() {
37 return "Boxed "+myWrappedValue
.toString();
40 public DfaValue
getWrappedValue() {
41 return myWrappedValue
;
44 public static class Factory
{
45 private final Map
<Object
, DfaBoxedValue
> cachedValues
= new HashMap
<Object
, DfaBoxedValue
>();
46 private final Map
<Object
, DfaBoxedValue
> cachedNegatedValues
= new HashMap
<Object
, DfaBoxedValue
>();
47 private final DfaValueFactory myFactory
;
49 public Factory(DfaValueFactory factory
) {
54 public DfaValue
createBoxed(DfaValue valueToWrap
) {
55 if (valueToWrap
instanceof DfaUnboxedValue
) return ((DfaUnboxedValue
)valueToWrap
).getVariable();
56 Object o
= valueToWrap
instanceof DfaConstValue
57 ?
((DfaConstValue
)valueToWrap
).getValue()
58 : valueToWrap
instanceof DfaVariableValue ?
((DfaVariableValue
)valueToWrap
).getPsiVariable() : null;
59 if (o
== null) return null;
60 Map
<Object
, DfaBoxedValue
> map
= valueToWrap
instanceof DfaVariableValue
&& ((DfaVariableValue
)valueToWrap
).isNegated() ? cachedNegatedValues
: cachedValues
;
61 DfaBoxedValue boxedValue
= map
.get(o
);
62 if (boxedValue
== null) {
63 boxedValue
= new DfaBoxedValue(valueToWrap
, myFactory
);
64 map
.put(o
, boxedValue
);
69 private final Map
<PsiVariable
, DfaUnboxedValue
> cachedUnboxedValues
= new THashMap
<PsiVariable
, DfaUnboxedValue
>();
70 private final Map
<PsiVariable
, DfaUnboxedValue
> cachedNegatedUnboxedValues
= new THashMap
<PsiVariable
, DfaUnboxedValue
>();
73 public DfaValue
createUnboxed(DfaValue value
) {
74 if (value
instanceof DfaBoxedValue
) {
75 return ((DfaBoxedValue
)value
).getWrappedValue();
77 if (value
instanceof DfaConstValue
) {
78 if (value
== value
.myFactory
.getConstFactory().getNull()) return DfaUnknownValue
.getInstance();
82 if (value
instanceof DfaVariableValue
) {
83 PsiVariable var
= ((DfaVariableValue
)value
).getPsiVariable();
84 Map
<PsiVariable
, DfaUnboxedValue
> map
= ((DfaVariableValue
)value
).isNegated() ? cachedNegatedUnboxedValues
: cachedUnboxedValues
;
85 result
= map
.get(var
);
87 result
= new DfaUnboxedValue((DfaVariableValue
)value
, myFactory
);
88 map
.put(var
, (DfaUnboxedValue
)result
);
92 result
= DfaUnknownValue
.getInstance();