1 11 package org.eclipse.jdt.internal.compiler.apt.model; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.List ; 16 17 import javax.lang.model.element.AnnotationMirror; 18 import javax.lang.model.element.AnnotationValue; 19 import javax.lang.model.element.AnnotationValueVisitor; 20 import javax.lang.model.element.VariableElement; 21 import javax.lang.model.type.TypeMirror; 22 23 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl; 24 import org.eclipse.jdt.internal.compiler.impl.Constant; 25 import org.eclipse.jdt.internal.compiler.impl.DoubleConstant; 26 import org.eclipse.jdt.internal.compiler.impl.FloatConstant; 27 import org.eclipse.jdt.internal.compiler.impl.LongConstant; 28 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding; 29 import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding; 30 import org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding; 31 import org.eclipse.jdt.internal.compiler.lookup.ElementValuePair; 32 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; 33 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; 34 import org.eclipse.jdt.internal.compiler.lookup.TypeIds; 35 import org.eclipse.jdt.internal.compiler.problem.ShouldNotImplement; 36 37 public class AnnotationValueImpl implements AnnotationValue, TypeIds { 38 39 42 private static final int T_AnnotationMirror = -1; 43 private static final int T_EnumConstant = -2; 44 private static final int T_ClassObject = -3; 45 private static final int T_ArrayType = -4; 46 47 private final BaseProcessingEnvImpl _env; 48 49 55 private final Object _value; 56 57 61 private final int _kind; 62 63 85 public AnnotationValueImpl(BaseProcessingEnvImpl env, Object value, TypeBinding type) { 86 _env = env; 87 int kind[] = new int[1]; 88 if (type == null) { 89 _value = convertToMirrorType(value, type, kind); 90 _kind = kind[0]; 91 } else if (type.isArrayType()) { 92 List <AnnotationValue> convertedValues = null; 93 TypeBinding valueType = ((ArrayBinding)type).elementsType(); 94 if (value instanceof Object []) { 95 Object [] values = (Object [])value; 96 convertedValues = new ArrayList <AnnotationValue>(values.length); 97 for (Object oneValue : values) { 98 convertedValues.add(new AnnotationValueImpl(_env, oneValue, valueType)); 99 } 100 } else { 101 convertedValues = new ArrayList <AnnotationValue>(1); 102 convertedValues.add(new AnnotationValueImpl(_env, value, valueType)); 103 } 104 _value = Collections.unmodifiableList(convertedValues); 105 _kind = T_ArrayType; 106 } else { 107 _value = convertToMirrorType(value, type, kind); 108 _kind = kind[0]; 109 } 110 } 111 112 124 private Object convertToMirrorType(Object value, TypeBinding type, int kind[]) { 125 if (type == null) { 126 kind[0] = TypeIds.T_JavaLangString; 127 return "<error>"; } else if (type instanceof BaseTypeBinding || type.id == TypeIds.T_JavaLangString) { 129 if (value == null) { 130 if (type instanceof BaseTypeBinding 131 || type.id == TypeIds.T_JavaLangString) { 132 kind[0] = TypeIds.T_JavaLangString; 134 return "<error>"; } else if (type.isAnnotationType()) { 136 kind[0] = T_AnnotationMirror; 137 return _env.getFactory().newAnnotationMirror(null); 138 } 139 } else if (value instanceof Constant) { 140 if (type instanceof BaseTypeBinding) { 141 kind[0] = ((BaseTypeBinding)type).id; 142 } 143 else if (type.id == TypeIds.T_JavaLangString) { 144 kind[0] = ((Constant)value).typeID(); 145 } else { 146 kind[0] = TypeIds.T_JavaLangString; 148 return "<error>"; } 150 switch (kind[0]) { 151 case T_boolean: 152 return ((Constant)value).booleanValue(); 153 case T_byte: 154 return ((Constant)value).byteValue(); 155 case T_char: 156 return ((Constant)value).charValue(); 157 case T_double: 158 return ((Constant)value).doubleValue(); 159 case T_float: 160 return ((Constant)value).floatValue(); 161 case T_int: 162 try { 163 if (value instanceof LongConstant 164 || value instanceof DoubleConstant 165 || value instanceof FloatConstant) { 166 kind[0] = TypeIds.T_JavaLangString; 168 return "<error>"; } 170 return ((Constant)value).intValue(); 171 } catch (ShouldNotImplement e) { 172 kind[0] = TypeIds.T_JavaLangString; 173 return "<error>"; } 175 case T_JavaLangString: 176 return ((Constant)value).stringValue(); 177 case T_long: 178 return ((Constant)value).longValue(); 179 case T_short: 180 return ((Constant)value).shortValue(); 181 } 182 } 183 } else if (type.isEnum()) { 184 if (value instanceof FieldBinding) { 185 kind[0] = T_EnumConstant; 186 return (VariableElement) _env.getFactory().newElement((FieldBinding) value); 187 } else { 188 kind[0] = TypeIds.T_JavaLangString; 189 return "<error>"; } 191 } else if (type.isAnnotationType()) { 192 if (value instanceof AnnotationBinding) { 193 kind[0] = T_AnnotationMirror; 194 return _env.getFactory().newAnnotationMirror((AnnotationBinding) value); 195 } 196 } else if (value instanceof TypeBinding) { 197 kind[0] = T_ClassObject; 198 return _env.getFactory().newTypeMirror((TypeBinding) value); 199 } 200 kind[0] = TypeIds.T_JavaLangString; 202 return "<error>"; } 204 205 @SuppressWarnings ("unchecked") @Override 207 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { 208 switch (_kind) { 209 case TypeIds.T_boolean: 210 return v.visitBoolean((Boolean )_value, p); 211 case TypeIds.T_byte: 212 return v.visitByte((Byte )_value, p); 213 case TypeIds.T_char: 214 return v.visitChar((Character )_value, p); 215 case TypeIds.T_double: 216 return v.visitDouble((Double )_value, p); 217 case TypeIds.T_float: 218 return v.visitFloat((Float )_value, p); 219 case TypeIds.T_int: 220 return v.visitInt((Integer )_value, p); 221 case TypeIds.T_JavaLangString: 222 return v.visitString((String )_value, p); 223 case TypeIds.T_long: 224 return v.visitLong((Long )_value, p); 225 case TypeIds.T_short: 226 return v.visitShort((Short )_value, p); 227 case T_EnumConstant: 228 return v.visitEnumConstant((VariableElement)_value, p); 229 case T_ClassObject: 230 return v.visitType((TypeMirror)_value, p); 231 case T_AnnotationMirror: 232 return v.visitAnnotation((AnnotationMirror)_value, p); 233 case T_ArrayType: 234 return v.visitArray((List <AnnotationValue>)_value, p); 235 default: 236 return null; 237 } 238 } 239 240 @Override 241 public Object getValue() { 242 return _value; 243 } 244 245 @Override 246 public boolean equals(Object obj) { 247 if (obj instanceof AnnotationValueImpl) { 248 return this._value.equals(((AnnotationValueImpl) obj)._value); 249 } 250 return false; 251 } 252 253 @Override 254 public int hashCode() { 255 return this._value.hashCode() + this._kind; 256 } 257 258 @Override 259 public String toString() { 260 if (null == _value) { 261 return "null"; } 263 return _value.toString(); 264 } 265 } 266 | Popular Tags |