0c15c4d3fe556ae7351174c9e98d550b12e3f7cb
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / MemberValuePairBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *    tyeung@bea.com - initial API and implementation
10  *    IBM Corporation - implemented methods from IBinding
11  *    IBM Corporation - renamed from ResolvedMemberValuePair to MemberValuePairBinding
12  *    jgarms@bea.com - Fix for IllegalStateException
13  *******************************************************************************/
14 package net.sourceforge.phpdt.core.dom;
15
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
18 import net.sourceforge.phpdt.internal.compiler.lookup.ElementValuePair;
19 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.TypeIds;
21
22 /**
23  * Internal class.
24  */
25 class MemberValuePairBinding implements IMemberValuePairBinding {
26         static final MemberValuePairBinding[] NoPair = new MemberValuePairBinding[0];
27         private static final Object NoValue = new Object();
28         private static final Object[] EmptyArray = new Object[0];
29
30         private ElementValuePair internalPair;
31         protected Object value = null;
32         protected BindingResolver bindingResolver;
33
34         static void appendValue(Object value, StringBuffer buffer) {
35                 if (value instanceof Object[]) {
36                         Object[] values = (Object[]) value;
37                         buffer.append('{');
38                         for (int i = 0, l = values.length; i < l; i++) {
39                                 if (i != 0)
40                                         buffer.append(", "); //$NON-NLS-1$
41                                 appendValue(values[i], buffer);
42                         }
43                         buffer.append('}');
44                 } else if (value instanceof ITypeBinding) {
45                         buffer.append(((ITypeBinding) value).getName());
46                         buffer.append(".class"); //$NON-NLS-1$
47                 } else {
48                         buffer.append(value);
49                 }
50         }
51
52         static Object buildDOMValue(final Object internalObject, BindingResolver resolver) {
53                 if (internalObject == null)
54                         return null;
55
56                 if (internalObject instanceof Constant) {
57                         Constant constant = (Constant) internalObject;
58                         switch (constant.typeID()) {
59                                 case TypeIds.T_boolean:
60                                         return Boolean.valueOf(constant.booleanValue());
61                                 case TypeIds.T_byte:
62                                         return new Byte(constant.byteValue());
63                                 case TypeIds.T_char:
64                                         return new Character(constant.charValue());
65                                 case TypeIds.T_double:
66                                         return new Double(constant.doubleValue());
67                                 case TypeIds.T_float:
68                                         return new Float(constant.floatValue());
69                                 case TypeIds.T_int:
70                                         return new Integer(constant.intValue());
71                                 case TypeIds.T_long:
72                                         return new Long(constant.longValue());
73                                 case TypeIds.T_short:
74                                         return new Short(constant.shortValue());
75                                 case TypeIds.T_JavaLangString:
76                                         return constant.stringValue();
77                         }
78                 } else if (internalObject instanceof net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) {
79                         return resolver.getTypeBinding((net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) internalObject);
80                 } else if (internalObject instanceof net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding) {
81                         return resolver.getAnnotationInstance((net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding) internalObject);
82                 } else if (internalObject instanceof net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding) {
83                         return resolver.getVariableBinding((net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding) internalObject);
84                 } else if (internalObject instanceof Object[]) {
85                         Object[] elements = (Object[]) internalObject;
86                         int length = elements.length;
87                         Object[] values = length == 0 ? EmptyArray : new Object[length];
88                         for (int i = 0; i < length; i++)
89                                 values[i] = buildDOMValue(elements[i], resolver);
90                         return values;
91                 }
92                 return null;
93         }
94
95         MemberValuePairBinding(ElementValuePair pair, BindingResolver resolver) {
96                 this.internalPair = pair;
97                 this.bindingResolver = resolver;
98         }
99
100         public IAnnotationBinding[] getAnnotations() {
101                 return AnnotationBinding.NoAnnotations;
102         }
103
104         public IJavaElement getJavaElement() {
105                 return null;
106         }
107
108         public String getKey() {
109                 // TODO when implementing, update spec in IBinding
110                 return null;
111         }
112
113         public int getKind() {
114                 return IBinding.MEMBER_VALUE_PAIR;
115         }
116
117         public IMethodBinding getMethodBinding() {
118                 return this.bindingResolver.getMethodBinding(this.internalPair.getMethodBinding());
119         }
120
121         public int getModifiers() {
122                 return Modifier.NONE;
123         }
124
125         public String getName() {
126                 if (this.internalPair == null)
127                         return null;
128                 final char[] membername = this.internalPair.getName();
129                 return membername == null ? null : new String(membername);
130         }
131
132         public Object getValue() {
133                 if (value == null)
134                         init();
135                 return value == NoValue ? null : this.value;
136         }
137
138         private void init() {
139                 this.value = buildDOMValue(this.internalPair.getValue(), this.bindingResolver);
140                 if (this.value == null)
141                         this.value = NoValue;
142         }
143
144         char[] internalName() {
145                 return this.internalPair == null ? null : this.internalPair.getName();
146         }
147
148         public boolean isDefault() {
149                 Object value2 = getValue();
150                 Object defaultValue = getMethodBinding().getDefaultValue();
151                 if (value2 instanceof IBinding) {
152                         if (defaultValue instanceof IBinding) {
153                                 return ((IBinding) value2).isEqualTo((IBinding) defaultValue);
154                         }
155                         return false;
156                 }
157                 if (defaultValue == null) return false;
158                 return defaultValue.equals(value2);
159         }
160
161         public boolean isDeprecated() {
162                 MethodBinding methodBinding = this.internalPair.getMethodBinding();
163                 return methodBinding == null ? false : methodBinding.isDeprecated();
164         }
165
166         public boolean isEqualTo(IBinding binding) {
167                 if (this == binding)
168                         return true;
169                 if (binding.getKind() != IBinding.MEMBER_VALUE_PAIR)
170                         return false;
171                 IMemberValuePairBinding other = (IMemberValuePairBinding) binding;
172                 if (!getMethodBinding().isEqualTo(other.getMethodBinding())) {
173                         return false;
174                 }
175                 Object otherValue = other.getValue();
176                 Object currentValue = getValue();
177                 if (currentValue == null) {
178                         return otherValue == null;
179                 }
180                 if (currentValue instanceof IBinding) {
181                         if (otherValue instanceof IBinding) {
182                                 return ((IBinding) currentValue).isEqualTo((IBinding) otherValue);
183                         }
184                         return false;
185                 }
186                 return currentValue.equals(otherValue);
187         }
188
189         /*
190          * (non-Javadoc)
191          * @see org.eclipse.jdt.core.dom.IBinding#isRecovered()
192          */
193         public boolean isRecovered() {
194                 return false;
195         }
196
197         public boolean isSynthetic() {
198                 return false;
199         }
200
201         public String toString() {
202                 StringBuffer buffer = new StringBuffer();
203                 buffer.append(getName());
204                 buffer.append(" = "); //$NON-NLS-1$
205                 appendValue(getValue(), buffer);
206                 return buffer.toString();
207         }
208 }