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
 
   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;
 
  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;
 
  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];
 
  30         private ElementValuePair internalPair;
 
  31         protected Object value = null;
 
  32         protected BindingResolver bindingResolver;
 
  34         static void appendValue(Object value, StringBuffer buffer) {
 
  35                 if (value instanceof Object[]) {
 
  36                         Object[] values = (Object[]) value;
 
  38                         for (int i = 0, l = values.length; i < l; i++) {
 
  40                                         buffer.append(", "); //$NON-NLS-1$
 
  41                                 appendValue(values[i], buffer);
 
  44                 } else if (value instanceof ITypeBinding) {
 
  45                         buffer.append(((ITypeBinding) value).getName());
 
  46                         buffer.append(".class"); //$NON-NLS-1$
 
  52         static Object buildDOMValue(final Object internalObject, BindingResolver resolver) {
 
  53                 if (internalObject == null)
 
  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());
 
  62                                         return new Byte(constant.byteValue());
 
  64                                         return new Character(constant.charValue());
 
  65                                 case TypeIds.T_double:
 
  66                                         return new Double(constant.doubleValue());
 
  68                                         return new Float(constant.floatValue());
 
  70                                         return new Integer(constant.intValue());
 
  72                                         return new Long(constant.longValue());
 
  74                                         return new Short(constant.shortValue());
 
  75                                 case TypeIds.T_JavaLangString:
 
  76                                         return constant.stringValue();
 
  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);
 
  95         MemberValuePairBinding(ElementValuePair pair, BindingResolver resolver) {
 
  96                 this.internalPair = pair;
 
  97                 this.bindingResolver = resolver;
 
 100         public IAnnotationBinding[] getAnnotations() {
 
 101                 return AnnotationBinding.NoAnnotations;
 
 104         public IJavaElement getJavaElement() {
 
 108         public String getKey() {
 
 109                 // TODO when implementing, update spec in IBinding
 
 113         public int getKind() {
 
 114                 return IBinding.MEMBER_VALUE_PAIR;
 
 117         public IMethodBinding getMethodBinding() {
 
 118                 return this.bindingResolver.getMethodBinding(this.internalPair.getMethodBinding());
 
 121         public int getModifiers() {
 
 122                 return Modifier.NONE;
 
 125         public String getName() {
 
 126                 if (this.internalPair == null)
 
 128                 final char[] membername = this.internalPair.getName();
 
 129                 return membername == null ? null : new String(membername);
 
 132         public Object getValue() {
 
 135                 return value == NoValue ? null : this.value;
 
 138         private void init() {
 
 139                 this.value = buildDOMValue(this.internalPair.getValue(), this.bindingResolver);
 
 140                 if (this.value == null)
 
 141                         this.value = NoValue;
 
 144         char[] internalName() {
 
 145                 return this.internalPair == null ? null : this.internalPair.getName();
 
 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);
 
 157                 if (defaultValue == null) return false;
 
 158                 return defaultValue.equals(value2);
 
 161         public boolean isDeprecated() {
 
 162                 MethodBinding methodBinding = this.internalPair.getMethodBinding();
 
 163                 return methodBinding == null ? false : methodBinding.isDeprecated();
 
 166         public boolean isEqualTo(IBinding binding) {
 
 169                 if (binding.getKind() != IBinding.MEMBER_VALUE_PAIR)
 
 171                 IMemberValuePairBinding other = (IMemberValuePairBinding) binding;
 
 172                 if (!getMethodBinding().isEqualTo(other.getMethodBinding())) {
 
 175                 Object otherValue = other.getValue();
 
 176                 Object currentValue = getValue();
 
 177                 if (currentValue == null) {
 
 178                         return otherValue == null;
 
 180                 if (currentValue instanceof IBinding) {
 
 181                         if (otherValue instanceof IBinding) {
 
 182                                 return ((IBinding) currentValue).isEqualTo((IBinding) otherValue);
 
 186                 return currentValue.equals(otherValue);
 
 191          * @see org.eclipse.jdt.core.dom.IBinding#isRecovered()
 
 193         public boolean isRecovered() {
 
 197         public boolean isSynthetic() {
 
 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();