KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > MemberValuePairBinding


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 org.eclipse.jdt.core.dom;
15
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.internal.compiler.impl.Constant;
18 import org.eclipse.jdt.internal.compiler.lookup.ElementValuePair;
19 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
20 import org.eclipse.jdt.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 JavaDoc NoValue = new Object JavaDoc();
28     private static final Object JavaDoc[] EmptyArray = new Object JavaDoc[0];
29
30     private ElementValuePair internalPair;
31     protected Object JavaDoc value = null;
32     protected BindingResolver bindingResolver;
33
34     static void appendValue(Object JavaDoc value, StringBuffer JavaDoc buffer) {
35         if (value instanceof Object JavaDoc[]) {
36             Object JavaDoc[] values = (Object JavaDoc[]) 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 JavaDoc buildDOMValue(final Object JavaDoc 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 JavaDoc(constant.byteValue());
63                 case TypeIds.T_char:
64                     return new Character JavaDoc(constant.charValue());
65                 case TypeIds.T_double:
66                     return new Double JavaDoc(constant.doubleValue());
67                 case TypeIds.T_float:
68                     return new Float JavaDoc(constant.floatValue());
69                 case TypeIds.T_int:
70                     return new Integer JavaDoc(constant.intValue());
71                 case TypeIds.T_long:
72                     return new Long JavaDoc(constant.longValue());
73                 case TypeIds.T_short:
74                     return new Short JavaDoc(constant.shortValue());
75                 case TypeIds.T_JavaLangString:
76                     return constant.stringValue();
77             }
78         } else if (internalObject instanceof org.eclipse.jdt.internal.compiler.lookup.TypeBinding) {
79             return resolver.getTypeBinding((org.eclipse.jdt.internal.compiler.lookup.TypeBinding) internalObject);
80         } else if (internalObject instanceof org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) {
81             return resolver.getAnnotationInstance((org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) internalObject);
82         } else if (internalObject instanceof org.eclipse.jdt.internal.compiler.lookup.FieldBinding) {
83             return resolver.getVariableBinding((org.eclipse.jdt.internal.compiler.lookup.FieldBinding) internalObject);
84         } else if (internalObject instanceof Object JavaDoc[]) {
85             Object JavaDoc[] elements = (Object JavaDoc[]) internalObject;
86             int length = elements.length;
87             Object JavaDoc[] values = length == 0 ? EmptyArray : new Object JavaDoc[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 JavaDoc 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 JavaDoc getName() {
126         if (this.internalPair == null)
127             return null;
128         final char[] membername = this.internalPair.getName();
129         return membername == null ? null : new String JavaDoc(membername);
130     }
131
132     public Object JavaDoc 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 JavaDoc value2 = getValue();
150         Object JavaDoc 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 JavaDoc otherValue = other.getValue();
176         Object JavaDoc 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 JavaDoc toString() {
202         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
203         buffer.append(getName());
204         buffer.append(" = "); //$NON-NLS-1$
205
appendValue(getValue(), buffer);
206         return buffer.toString();
207     }
208 }
209
Popular Tags