KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > DefaultCachedRepository


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 class DefaultCachedRepository implements CachedRepository {
33     
34     private final static Collection JavaDoc EMPTY_COLLECTION = new ArrayList JavaDoc (0);
35     
36     private final Collection JavaDoc classAttributes;
37     private final Map JavaDoc fields = new HashMap JavaDoc ();
38     private final Map JavaDoc methods = new HashMap JavaDoc ();
39     private final Map JavaDoc constructors = new HashMap JavaDoc ();
40     
41     private static class MethodAttributeBundle {
42         private Collection JavaDoc attributes = EMPTY_COLLECTION;
43         private List JavaDoc parameterAttributes = new ArrayList JavaDoc ();
44         private Collection JavaDoc returnAttributes = EMPTY_COLLECTION;
45         
46         public MethodAttributeBundle () {
47         }
48         
49         public Collection JavaDoc getAttributes () {
50             return attributes;
51         }
52         
53         public Collection JavaDoc getReturnAttributes () {
54             return returnAttributes;
55         }
56         
57         public Collection JavaDoc getParameterAttributes (int index) {
58             return (Collection JavaDoc) parameterAttributes.get (index);
59         }
60         
61         public void setAttributes (Collection JavaDoc attributes) {
62             this.attributes = Collections.unmodifiableCollection (attributes);
63         }
64         
65         public void setReturnAttributes (Collection JavaDoc returnAttributes) {
66             this.returnAttributes = Collections.unmodifiableCollection (returnAttributes);
67         }
68         
69         public void addParameterAttributes (Collection JavaDoc parameterAttributes) {
70             this.parameterAttributes.add (Collections.unmodifiableCollection (parameterAttributes));
71         }
72     }
73     
74     public DefaultCachedRepository (Class JavaDoc clazz, AttributeRepositoryClass repo) {
75         
76         // ---- Fix up class attributes
77
Set JavaDoc tempClassAttributes = new HashSet JavaDoc ();
78         
79         tempClassAttributes.addAll (repo.getClassAttributes ());
80         tempClassAttributes.addAll (getInheritableClassAttributes (clazz.getSuperclass ()));
81         Class JavaDoc[] ifs = clazz.getInterfaces ();
82         for (int i = 0; i < ifs.length; i++) {
83             tempClassAttributes.addAll (getInheritableClassAttributes (ifs[i]));
84         }
85         this.classAttributes = Collections.unmodifiableCollection (tempClassAttributes);
86         
87         // ---- Fix up method attributes
88
Method JavaDoc[] methods = clazz.getDeclaredMethods ();
89         for (int i = 0; i < methods.length; i++) {
90             MethodAttributeBundle bundle = new MethodAttributeBundle ();
91             
92             Method JavaDoc m = methods[i];
93             String JavaDoc key = Util.getSignature (m);
94             
95             List JavaDoc attributeBundle = null;
96             if (repo.getMethodAttributes ().containsKey (key)) {
97                 attributeBundle = (List JavaDoc) repo.getMethodAttributes ().get (key);
98             }
99             
100             Set JavaDoc attributes = new HashSet JavaDoc ();
101             if (attributeBundle != null) {
102                 attributes.addAll ((Collection JavaDoc) attributeBundle.get (0));
103             }
104             attributes.addAll (getInheritableMethodAttributes (clazz.getSuperclass (), m.getName (), m.getParameterTypes ()));
105             for (int j = 0; j < ifs.length; j++) {
106                 attributes.addAll (getInheritableMethodAttributes (ifs[j], m.getName (), m.getParameterTypes ()));
107             }
108             
109             if (attributes.size () > 0) {
110                 bundle.setAttributes (attributes);
111             }
112             
113             // ---- Return value attributes
114
attributes = new HashSet JavaDoc ();
115             if (attributeBundle != null) {
116                 attributes.addAll ((Collection JavaDoc) attributeBundle.get (1));
117             }
118             attributes.addAll (getInheritableReturnAttributes (clazz.getSuperclass (), m.getName (), m.getParameterTypes ()));
119             for (int j = 0; j < ifs.length; j++) {
120                 attributes.addAll (getInheritableReturnAttributes (ifs[j], m.getName (), m.getParameterTypes ()));
121             }
122             if (attributes.size () > 0) {
123                 bundle.setReturnAttributes (attributes);
124             }
125             
126             // ---- Parameter attributes
127
int numParameters = m.getParameterTypes().length;
128             for (int k = 0; k < numParameters; k++) {
129                 attributes = new HashSet JavaDoc ();
130                 if (attributeBundle != null) {
131                     attributes.addAll ((Collection JavaDoc) attributeBundle.get (k + 2));
132                 }
133                 attributes.addAll (getInheritableMethodParameterAttributes (clazz.getSuperclass (), m.getName (), m.getParameterTypes (), k));
134                 for (int j = 0; j < ifs.length; j++) {
135                     attributes.addAll (getInheritableMethodParameterAttributes (ifs[j], m.getName (), m.getParameterTypes (), k));
136                 }
137                                 
138                 bundle.addParameterAttributes (attributes);
139             }
140             
141             this.methods.put (m, bundle);
142         }
143         
144         // --- Just copy constructor attributes (they aren't inherited)
145
Constructor JavaDoc[] constructors = clazz.getDeclaredConstructors ();
146         for (int i = 0; i < constructors.length; i++) {
147             Constructor JavaDoc ctor = constructors[i];
148             String JavaDoc key = Util.getSignature (ctor);
149             
150             if (repo.getConstructorAttributes ().containsKey (key)) {
151                 List JavaDoc attributeBundle = null;
152                 attributeBundle = (List JavaDoc) repo.getConstructorAttributes ().get (key);
153                 MethodAttributeBundle bundle = new MethodAttributeBundle ();
154                 
155                 bundle.setAttributes ((Collection JavaDoc) attributeBundle.get (0));
156                 // ---- Parameter attributes
157
int numParameters = ctor.getParameterTypes().length;
158                 for (int k = 0; k < numParameters; k++) {
159                     bundle.addParameterAttributes ((Collection JavaDoc) attributeBundle.get (k + 1));
160                 }
161                 this.constructors.put (ctor, bundle);
162             }
163         }
164         
165         // --- Just copy field attributes (they aren't inherited)
166
Field JavaDoc[] fields = clazz.getDeclaredFields ();
167         for (int i = 0; i < fields.length; i++) {
168             Field JavaDoc f = fields[i];
169             String JavaDoc key = f.getName ();
170             if (repo.getFieldAttributes ().containsKey (key)) {
171                 this.fields.put (f, Collections.unmodifiableCollection ((Collection JavaDoc) repo.getFieldAttributes ().get (key)));
172             }
173         }
174     }
175     
176     private static Collection JavaDoc getInheritableAttributes (Collection JavaDoc attrs) {
177         HashSet JavaDoc result = new HashSet JavaDoc ();
178         
179         Iterator JavaDoc iter = attrs.iterator ();
180         while (iter.hasNext ()) {
181             Object JavaDoc attr = iter.next ();
182             if (Attributes.hasAttributeType (attr.getClass (), Inheritable.class)) {
183                 result.add (attr);
184             }
185         }
186         return result;
187     }
188     
189     private static Collection JavaDoc getInheritableClassAttributes (Class JavaDoc c) {
190         if (c == null) {
191             return new ArrayList JavaDoc (0);
192         }
193         
194         HashSet JavaDoc result = new HashSet JavaDoc ();
195         result.addAll (getInheritableAttributes (Attributes.getAttributes (c)));
196         
197         // Traverse the class hierarchy
198
result.addAll (getInheritableClassAttributes (c.getSuperclass ()));
199         
200         // Traverse the interface hierarchy
201
Class JavaDoc[] ifs = c.getInterfaces ();
202         for (int i = 0; i < ifs.length; i++) {
203             result.addAll (getInheritableClassAttributes (ifs[i]));
204         }
205         
206         return result;
207     }
208     
209     private static Collection JavaDoc getInheritableMethodAttributes (Class JavaDoc c, String JavaDoc methodName, Class JavaDoc[] methodParams) {
210         if (c == null) {
211             return new ArrayList JavaDoc (0);
212         }
213         
214         HashSet JavaDoc result = new HashSet JavaDoc ();
215         
216         try {
217             // Get equivalent method in c
218
Method JavaDoc m = c.getDeclaredMethod (methodName, methodParams);
219             if ((m.getModifiers () & Modifier.PRIVATE) == 0) {
220                 result.addAll (getInheritableAttributes (Attributes.getAttributes (m)));
221             }
222         } catch (NoSuchMethodException JavaDoc nsme) {
223         }
224         
225         // Traverse the class hierarchy
226
result.addAll (getInheritableMethodAttributes (c.getSuperclass (), methodName, methodParams));
227         
228         // Traverse the interface hierarchy
229
Class JavaDoc[] ifs = c.getInterfaces ();
230         for (int i = 0; i < ifs.length; i++) {
231             result.addAll (getInheritableMethodAttributes (ifs[i], methodName, methodParams));
232         }
233         
234         return result;
235     }
236     
237     private static Collection JavaDoc getInheritableMethodParameterAttributes (Class JavaDoc c, String JavaDoc methodName, Class JavaDoc[] methodParams, int parameter) {
238         if (c == null) {
239             return new ArrayList JavaDoc (0);
240         }
241         
242         HashSet JavaDoc result = new HashSet JavaDoc ();
243         
244         try {
245             // Get equivalent method in c
246
Method JavaDoc m = c.getDeclaredMethod (methodName, methodParams);
247             if ((m.getModifiers () & Modifier.PRIVATE) == 0) {
248                 result.addAll (getInheritableAttributes (Attributes.getParameterAttributes (m, parameter)));
249             }
250         } catch (NoSuchMethodException JavaDoc nsme) {
251         }
252         
253         // Traverse the class hierarchy
254
result.addAll (getInheritableMethodParameterAttributes (c.getSuperclass (), methodName, methodParams, parameter));
255         
256         // Traverse the interface hierarchy
257
Class JavaDoc[] ifs = c.getInterfaces ();
258         for (int i = 0; i < ifs.length; i++) {
259             result.addAll (getInheritableMethodParameterAttributes (ifs[i], methodName, methodParams, parameter));
260         }
261         
262         return result;
263     }
264     
265     private static Collection JavaDoc getInheritableReturnAttributes (Class JavaDoc c, String JavaDoc methodName, Class JavaDoc[] methodParams) {
266         if (c == null) {
267             return new ArrayList JavaDoc (0);
268         }
269         
270         HashSet JavaDoc result = new HashSet JavaDoc ();
271         
272         try {
273             // Get equivalent method in c
274
Method JavaDoc m = c.getDeclaredMethod (methodName, methodParams);
275             if ((m.getModifiers () & Modifier.PRIVATE) == 0) {
276                 result.addAll (getInheritableAttributes (Attributes.getReturnAttributes (m)));
277             }
278         } catch (NoSuchMethodException JavaDoc nsme) {
279         }
280         
281         // Traverse the class hierarchy
282
result.addAll (getInheritableReturnAttributes (c.getSuperclass (), methodName, methodParams));
283         
284         // Traverse the interface hierarchy
285
Class JavaDoc[] ifs = c.getInterfaces ();
286         for (int i = 0; i < ifs.length; i++) {
287             result.addAll (getInheritableReturnAttributes (ifs[i], methodName, methodParams));
288         }
289         
290         return result;
291     }
292     
293     public Collection JavaDoc getAttributes () {
294         return classAttributes;
295     }
296     
297     public Collection JavaDoc getAttributes (Field JavaDoc f) {
298         if (fields.containsKey (f)) {
299             return (Collection JavaDoc) fields.get (f);
300         } else {
301             return EMPTY_COLLECTION;
302         }
303         
304     }
305     
306     public Collection JavaDoc getAttributes (Method JavaDoc m) {
307         if (methods.containsKey (m)) {
308             return ((MethodAttributeBundle) methods.get (m)).getAttributes ();
309         } else {
310             return EMPTY_COLLECTION;
311         }
312     }
313     
314     public Collection JavaDoc getParameterAttributes (Constructor JavaDoc c, int parameter) {
315         if (constructors.containsKey (c)) {
316             return ((MethodAttributeBundle) constructors.get (c)).getParameterAttributes (parameter);
317         } else {
318             return EMPTY_COLLECTION;
319         }
320     }
321     
322     public Collection JavaDoc getParameterAttributes (Method JavaDoc m, int parameter) {
323         if (methods.containsKey (m)) {
324             return ((MethodAttributeBundle) methods.get (m)).getParameterAttributes (parameter);
325         } else {
326             return EMPTY_COLLECTION;
327         }
328     }
329     
330     public Collection JavaDoc getReturnAttributes (Method JavaDoc m) {
331         if (methods.containsKey (m)) {
332             return ((MethodAttributeBundle) methods.get (m)).getReturnAttributes ();
333         } else {
334             return EMPTY_COLLECTION;
335         }
336     }
337     
338     public Collection JavaDoc getAttributes (Constructor JavaDoc c) {
339         if (constructors.containsKey (c)) {
340             return ((MethodAttributeBundle) constructors.get (c)).getAttributes ();
341         } else {
342             return EMPTY_COLLECTION;
343         }
344     }
345 }
346
Popular Tags