removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / AnnotationBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2005, 2008 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 net.sourceforge.phpdt.core.dom;
14
15 import net.sourceforge.phpdt.core.IAnnotatable;
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
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.ReferenceBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.TagBits;
22 import net.sourceforge.phpdt.internal.compiler.util.*;
23
24 /**
25  * Internal class
26  */
27 class AnnotationBinding implements IAnnotationBinding {
28         static final AnnotationBinding[] NoAnnotations = new AnnotationBinding[0];
29         private net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding binding;
30         private BindingResolver bindingResolver;
31         private String key;
32
33         AnnotationBinding(net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding annotation, BindingResolver resolver) {
34                 if (annotation == null)
35                         throw new IllegalStateException();
36                 binding = annotation;
37                 bindingResolver = resolver;
38         }
39         
40         public IAnnotationBinding[] getAnnotations() {
41                 return NoAnnotations;
42         }
43
44         public ITypeBinding getAnnotationType() {
45                 ITypeBinding typeBinding = this.bindingResolver.getTypeBinding(this.binding.getAnnotationType());
46                 if (typeBinding == null)
47                         return null;
48                 return typeBinding;
49         }
50         
51         public IMemberValuePairBinding[] getDeclaredMemberValuePairs() {
52                 ReferenceBinding typeBinding = this.binding.getAnnotationType();
53                 if (typeBinding == null || ((typeBinding.tagBits & TagBits.HasMissingType) != 0)) {
54                         return MemberValuePairBinding.NoPair;
55                 }
56                 ElementValuePair[] internalPairs = this.binding.getElementValuePairs();
57                 int length = internalPairs.length;
58                 IMemberValuePairBinding[] pairs = length == 0 ? MemberValuePairBinding.NoPair : new MemberValuePairBinding[length];
59                 int counter = 0;
60                 for (int i = 0; i < length; i++) {
61                         ElementValuePair valuePair = internalPairs[i];
62                         if (valuePair.binding == null) continue;
63                         pairs[counter++] = this.bindingResolver.getMemberValuePairBinding(valuePair);
64                 }
65                 if (counter == 0) return MemberValuePairBinding.NoPair;
66                 if (counter != length) {
67                         // resize
68                         System.arraycopy(pairs, 0, (pairs = new MemberValuePairBinding[counter]), 0, counter);
69                 }
70                 return pairs;
71         }
72
73         public IMemberValuePairBinding[] getAllMemberValuePairs() {
74                 IMemberValuePairBinding[] pairs = getDeclaredMemberValuePairs();
75                 ReferenceBinding typeBinding = this.binding.getAnnotationType();
76                 if (typeBinding == null || ((typeBinding.tagBits & TagBits.HasMissingType) != 0)) return pairs;
77                 MethodBinding[] methods = typeBinding.availableMethods(); // resilience
78                 int methodLength = methods == null ? 0 : methods.length;
79                 if (methodLength == 0) return pairs;
80
81                 int declaredLength = pairs.length;
82                 if (declaredLength == methodLength)
83                         return pairs;
84
85                 HashtableOfObject table = new HashtableOfObject(declaredLength);
86                 for (int i = 0; i < declaredLength; i++) {
87                         char[] internalName = ((MemberValuePairBinding) pairs[i]).internalName();
88                         if (internalName == null) continue;
89                         table.put(internalName, pairs[i]);
90                 }
91
92                 // handle case of more methods than declared members
93                 IMemberValuePairBinding[] allPairs = new  IMemberValuePairBinding[methodLength];
94                 for (int i = 0; i < methodLength; i++) {
95                         Object pair = table.get(methods[i].selector);
96                         allPairs[i] = pair == null ? new DefaultValuePairBinding(methods[i], this.bindingResolver) : (IMemberValuePairBinding) pair;
97                 }
98                 return allPairs;
99         }
100         
101         public IJavaElement getJavaElement() {
102                 if (!(this.bindingResolver instanceof DefaultBindingResolver)) return null;
103                 ASTNode node = (ASTNode) ((DefaultBindingResolver) this.bindingResolver).bindingsToAstNodes.get(this);
104                 if (!(node instanceof Annotation)) return null;
105                 ASTNode parent = node.getParent();
106                 IJavaElement parentElement = null;
107                 switch (parent.getNodeType()) {
108                 case ASTNode.PACKAGE_DECLARATION:
109                         IJavaElement cu = ((CompilationUnit) parent.getParent()).getJavaElement();
110                         if (cu instanceof ICompilationUnit) {
111                                 String pkgName = ((PackageDeclaration) parent).getName().getFullyQualifiedName();
112                                 parentElement =  ((ICompilationUnit) cu).getPackageDeclaration(pkgName);
113                         }
114                         break;
115                 case ASTNode.TYPE_DECLARATION:
116                         parentElement = ((TypeDeclaration) parent).resolveBinding().getJavaElement();
117                         break;
118                 case ASTNode.FIELD_DECLARATION:
119                         VariableDeclarationFragment fragment = (VariableDeclarationFragment) ((FieldDeclaration) parent).fragments().get(0);
120                         parentElement = fragment.resolveBinding().getJavaElement();
121                         break;
122                 case ASTNode.METHOD_DECLARATION:
123                         parentElement = ((MethodDeclaration) parent).resolveBinding().getJavaElement();
124                         break;
125                 case ASTNode.VARIABLE_DECLARATION_STATEMENT:
126                         fragment = (VariableDeclarationFragment) ((VariableDeclarationStatement) parent).fragments().get(0);
127                         parentElement = fragment.resolveBinding().getJavaElement();
128                         break;
129                 default:
130                         return null;
131                 }
132                 if (! (parentElement instanceof IAnnotatable)) return null;
133                 return ((IAnnotatable) parentElement).getAnnotation(getName());
134         }
135
136         public String getKey() {
137                 if (this.key == null) {
138                         String recipientKey = getRecipientKey();
139                         this.key = new String(this.binding.computeUniqueKey(recipientKey.toCharArray()));
140                 }
141                 return this.key;
142         }
143         
144         private String getRecipientKey() {
145                 if (!(this.bindingResolver instanceof DefaultBindingResolver)) return ""; //$NON-NLS-1$
146                 DefaultBindingResolver resolver = (DefaultBindingResolver) this.bindingResolver;
147                 ASTNode node = (ASTNode) resolver.bindingsToAstNodes.get(this);
148                 if (node == null) {
149                         // Can happen if annotation bindings have been resolved before having parsed the declaration
150                         return ""; //$NON-NLS-1$
151                 }
152                 ASTNode recipient = node.getParent();
153                 switch (recipient.getNodeType()) {
154                 case ASTNode.PACKAGE_DECLARATION:
155                         String pkgName = ((PackageDeclaration) recipient).getName().getFullyQualifiedName();
156                         return pkgName.replace('.', '/');
157                 case ASTNode.TYPE_DECLARATION:
158                         return ((TypeDeclaration) recipient).resolveBinding().getKey();
159                 case ASTNode.FIELD_DECLARATION:
160                         VariableDeclarationFragment fragment = (VariableDeclarationFragment) ((FieldDeclaration) recipient).fragments().get(0);
161                         return fragment.resolveBinding().getKey();
162                 case ASTNode.METHOD_DECLARATION:
163                         return ((MethodDeclaration) recipient).resolveBinding().getKey();
164                 case ASTNode.VARIABLE_DECLARATION_STATEMENT:
165                         fragment = (VariableDeclarationFragment) ((VariableDeclarationStatement) recipient).fragments().get(0);
166                         return fragment.resolveBinding().getKey();
167                 default:
168                         return ""; //$NON-NLS-1$
169                 }
170         }
171
172         public int getKind() {
173                 return IBinding.ANNOTATION;
174         }
175
176         public int getModifiers() {
177                 return Modifier.NONE;
178         }
179
180         public String getName() {
181                 ITypeBinding annotationType = getAnnotationType();
182                 if (annotationType == null) {
183                         return new String(this.binding.getAnnotationType().sourceName());
184                 } else {
185                         return annotationType.getName();
186                 }
187         }
188         
189         public boolean isDeprecated() {
190                 ReferenceBinding typeBinding = this.binding.getAnnotationType();
191                 if (typeBinding == null) return false;
192                 return typeBinding.isDeprecated();
193         }
194         
195         public boolean isEqualTo(IBinding otherBinding) {
196                 if (this == otherBinding)
197                         return true;
198                 if (otherBinding.getKind() != IBinding.ANNOTATION)
199                         return false;
200                 IAnnotationBinding other = (IAnnotationBinding) otherBinding;
201                 if (!getAnnotationType().isEqualTo(other.getAnnotationType()))
202                         return false;
203                 IMemberValuePairBinding[] memberValuePairs = getDeclaredMemberValuePairs();
204                 IMemberValuePairBinding[] otherMemberValuePairs = other.getDeclaredMemberValuePairs();
205                 if (memberValuePairs.length != otherMemberValuePairs.length)
206                         return false;
207                 for (int i = 0, length = memberValuePairs.length; i < length; i++) {
208                         if (!memberValuePairs[i].isEqualTo(otherMemberValuePairs[i]))
209                                 return false;
210                 }
211                 return true;
212         }
213
214         /*
215          * (non-Javadoc)
216          * @see org.eclipse.jdt.core.dom.IBinding#isRecovered()
217          */
218         public boolean isRecovered() {
219         ReferenceBinding annotationType = binding.getAnnotationType();
220         return annotationType == null || (annotationType.tagBits & TagBits.HasMissingType) != 0;        }
221
222         public boolean isSynthetic() {
223                 return false;
224         }
225
226         public String toString() {
227                 ITypeBinding type = getAnnotationType();
228                 final StringBuffer buffer = new StringBuffer();
229                 buffer.append('@');
230                 if (type != null)
231                         buffer.append(type.getName());
232                 buffer.append('(');
233                 IMemberValuePairBinding[] pairs = getDeclaredMemberValuePairs();
234                 for (int i = 0, len = pairs.length; i < len; i++) {
235                         if (i != 0)
236                                 buffer.append(", "); //$NON-NLS-1$
237                         buffer.append(pairs[i].toString());
238                 }
239                 buffer.append(')');
240                 return buffer.toString();
241         }
242         
243 }