KickJava   Java API By Example, From Geeks To Geeks.

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


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 ResolvedAnnotation to AnnotationBinding
12  *******************************************************************************/

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 /**
22  * Internal class
23  */

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 JavaDoc();
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(); // resilience
61
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         // handle case of more methods than declared members
73
IMemberValuePairBinding[] allPairs = new IMemberValuePairBinding[methodLength];
74         for (int i = 0; i < methodLength; i++) {
75             Object JavaDoc 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 JavaDoc getKey() {
89         // TODO when implementing, update spec in IBinding
90
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 JavaDoc getName() {
102         ITypeBinding annotationType = getAnnotationType();
103         if (annotationType == null) {
104             return new String JavaDoc(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     /*
136      * (non-Javadoc)
137      * @see org.eclipse.jdt.core.dom.IBinding#isRecovered()
138      */

139     public boolean isRecovered() {
140         return false;
141     }
142
143     public boolean isSynthetic() {
144         return false;
145     }
146
147     public String JavaDoc toString() {
148         ITypeBinding type = getAnnotationType();
149         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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(", "); //$NON-NLS-1$
158
buffer.append(pairs[i].toString());
159         }
160         buffer.append(')');
161         return buffer.toString();
162     }
163     
164 }
165
Popular Tags