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