1 13 package org.eclipse.jdt.core.dom; 14 15 import org.eclipse.jdt.core.IJavaElement; 16 import org.eclipse.jdt.internal.compiler.lookup.ElementValuePair; 17 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; 18 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; 19 import org.eclipse.jdt.internal.compiler.util.*; 20 21 24 class AnnotationBinding implements IAnnotationBinding { 25 static final AnnotationBinding[] NoAnnotations = new AnnotationBinding[0]; 26 private org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding internalAnnotation; 27 private BindingResolver bindingResolver; 28 29 AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding annotation, BindingResolver resolver) { 30 if (annotation == null) 31 throw new IllegalStateException (); 32 internalAnnotation = annotation; 33 bindingResolver = resolver; 34 } 35 36 public IAnnotationBinding[] getAnnotations() { 37 return NoAnnotations; 38 } 39 40 public ITypeBinding getAnnotationType() { 41 ITypeBinding binding = this.bindingResolver.getTypeBinding(this.internalAnnotation.getAnnotationType()); 42 if (binding == null || !binding.isAnnotation()) 43 return null; 44 return binding; 45 } 46 47 public IMemberValuePairBinding[] getDeclaredMemberValuePairs() { 48 ElementValuePair[] internalPairs = this.internalAnnotation.getElementValuePairs(); 49 int length = internalPairs.length; 50 IMemberValuePairBinding[] pairs = length == 0 ? MemberValuePairBinding.NoPair : new MemberValuePairBinding[length]; 51 for (int i = 0; i < length; i++) 52 pairs[i] = this.bindingResolver.getMemberValuePairBinding(internalPairs[i]); 53 return pairs; 54 } 55 56 public IMemberValuePairBinding[] getAllMemberValuePairs() { 57 IMemberValuePairBinding[] pairs = getDeclaredMemberValuePairs(); 58 ReferenceBinding typeBinding = this.internalAnnotation.getAnnotationType(); 59 if (typeBinding == null) return pairs; 60 MethodBinding[] methods = typeBinding.availableMethods(); int methodLength = methods == null ? 0 : methods.length; 62 if (methodLength == 0) return pairs; 63 64 int declaredLength = pairs.length; 65 if (declaredLength == methodLength) 66 return pairs; 67 68 HashtableOfObject table = new HashtableOfObject(declaredLength); 69 for (int i = 0; i < declaredLength; i++) 70 table.put(((MemberValuePairBinding) pairs[i]).internalName(), pairs[i]); 71 72 IMemberValuePairBinding[] allPairs = new IMemberValuePairBinding[methodLength]; 74 for (int i = 0; i < methodLength; i++) { 75 Object pair = table.get(methods[i].selector); 76 allPairs[i] = pair == null ? new DefaultValuePairBinding(methods[i], this.bindingResolver) : (IMemberValuePairBinding) pair; 77 } 78 return allPairs; 79 } 80 81 public IJavaElement getJavaElement() { 82 ITypeBinding annotationType = getAnnotationType(); 83 if (annotationType == null) 84 return null; 85 return annotationType.getJavaElement(); 86 } 87 88 public String getKey() { 89 return null; 91 } 92 93 public int getKind() { 94 return IBinding.ANNOTATION; 95 } 96 97 public int getModifiers() { 98 return Modifier.NONE; 99 } 100 101 public String getName() { 102 ITypeBinding annotationType = getAnnotationType(); 103 if (annotationType == null) { 104 return new String (this.internalAnnotation.getAnnotationType().sourceName()); 105 } else { 106 return annotationType.getName(); 107 } 108 } 109 110 public boolean isDeprecated() { 111 ReferenceBinding typeBinding = this.internalAnnotation.getAnnotationType(); 112 if (typeBinding == null) return false; 113 return typeBinding.isDeprecated(); 114 } 115 116 public boolean isEqualTo(IBinding binding) { 117 if (this == binding) 118 return true; 119 if (binding.getKind() != IBinding.ANNOTATION) 120 return false; 121 IAnnotationBinding other = (IAnnotationBinding) binding; 122 if (!getAnnotationType().isEqualTo(other.getAnnotationType())) 123 return false; 124 IMemberValuePairBinding[] memberValuePairs = getDeclaredMemberValuePairs(); 125 IMemberValuePairBinding[] otherMemberValuePairs = other.getDeclaredMemberValuePairs(); 126 if (memberValuePairs.length != otherMemberValuePairs.length) 127 return false; 128 for (int i = 0, length = memberValuePairs.length; i < length; i++) { 129 if (!memberValuePairs[i].isEqualTo(otherMemberValuePairs[i])) 130 return false; 131 } 132 return true; 133 } 134 135 139 public boolean isRecovered() { 140 return false; 141 } 142 143 public boolean isSynthetic() { 144 return false; 145 } 146 147 public String toString() { 148 ITypeBinding type = getAnnotationType(); 149 final StringBuffer buffer = new StringBuffer (); 150 buffer.append('@'); 151 if (type != null) 152 buffer.append(type.getName()); 153 buffer.append('('); 154 IMemberValuePairBinding[] pairs = getDeclaredMemberValuePairs(); 155 for (int i = 0, len = pairs.length; i < len; i++) { 156 if (i != 0) 157 buffer.append(", "); buffer.append(pairs[i].toString()); 159 } 160 buffer.append(')'); 161 return buffer.toString(); 162 } 163 164 } 165 | Popular Tags |