KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Set JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Class used to define attributes programmatically for a class.
30  * It is recommended that this class is used in the static initializer for
31  * a class:
32  * <pre><code>
33  * public class RuntimeSample extends SuperSample implements SampleIFJoin {
34  *
35  * static {
36  * try {
37  * RuntimeAttributeRepository rar =
38  * new RuntimeAttributeRepository (RuntimeSample.class);
39  *
40  * rar.addClassAttribute (new ThreadSafe ());
41  *
42  * rar.addFieldAttribute ("field", new ThreadSafe ());
43  *
44  * rar.addMethodAttribute ("someMethod", new Class[]{},
45  * new Dependency ( SampleService.class, "sample-some-method1" ));
46  *
47  * rar.addParameterAttribute ("methodWithAttributes",
48  * new Class[]{ Integer.TYPE, Integer.TYPE }, 1,
49  * new ThreadSafe ());
50  *
51  * rar.addReturnAttribute ("methodWithAttributes",
52  * new Class[]{ Integer.TYPE, Integer.TYPE },
53  * new Dependency ( SampleService.class, "sample-return" ));
54  *
55  * rar.addMethodAttribute ("someMethod",
56  * new Class[]{ Integer.TYPE },
57  * new Dependency ( SampleService.class, "sample-some-method2" ));
58  *
59  * Attributes.setAttributes (rar);
60  * } catch (Exception e) {
61  * throw new Error ("Unable to set attribute information: " + e.toString ());
62  * }
63  * }
64  * </code></pre>
65  */

66 public class RuntimeAttributeRepository implements AttributeRepositoryClass {
67     
68     /**
69      * Flag indicating whether this repository is modifiable.
70      * Once sealed, a repository can't be un-sealed.
71      */

72     private boolean sealed = false;
73     
74     /**
75      * Set of class attributes. See javadoc for <code>AttributeRepositoryClass</code> for structure.
76      */

77     private final Set JavaDoc classAttributes = new HashSet JavaDoc ();
78     
79     /**
80      * Set of field attributes. See javadoc for <code>AttributeRepositoryClass</code> for structure.
81      */

82     private final Map JavaDoc fieldAttributes = new HashMap JavaDoc ();
83     
84     /**
85      * Set of ctor attributes. See javadoc for <code>AttributeRepositoryClass</code> for structure.
86      */

87     private final Map JavaDoc constructorAttributes = new HashMap JavaDoc ();
88     
89     /**
90      * Set of method attributes. See javadoc for <code>AttributeRepositoryClass</code> for structure.
91      */

92     private final Map JavaDoc methodAttributes = new HashMap JavaDoc ();
93     
94     /**
95      * Class we are defining attributes for.
96      */

97     private final Class JavaDoc clazz;
98     
99     /**
100      * Create a new runtime repository.
101      */

102     public RuntimeAttributeRepository (Class JavaDoc clazz) {
103         this.clazz = clazz;
104     }
105     
106     /**
107      * Adds a new attribute to the class itself.
108      */

109     public void addClassAttribute (Object JavaDoc attribute) {
110         classAttributes.add (attribute);
111     }
112     
113     /**
114      * Convenience function to check if the repository is sealed.
115      *
116      * @throws IllegalStateException if sealed
117      */

118     private void checkSealed () throws IllegalStateException JavaDoc {
119         if (sealed) {
120             throw new IllegalStateException JavaDoc ("RuntimeAttributeRepository has been sealed.");
121         }
122     }
123     
124     /**
125      * Convenience method to get and initialize an enry in the method or
126      * constructor attribute map.
127      */

128     private List JavaDoc getMethodOrConstructorAttributeBundle (Map JavaDoc map, String JavaDoc signature, int numSlots) {
129         List JavaDoc bundle = (List JavaDoc) map.get (signature);
130         if (bundle == null) {
131             bundle = new ArrayList JavaDoc ();
132             map.put (signature, bundle);
133             
134             for (int i = 0; i < numSlots; i++) {
135                 bundle.add (new HashSet JavaDoc ());
136             }
137         }
138         
139         return bundle;
140     }
141     
142     /**
143      * Convenience method to get and initialize an entry in the method map.
144      *
145      * @return a fully initialized List (as defined for the <code>AttributeRepositoryClass</code> interface)
146      * for the given method.
147      */

148     private List JavaDoc getMethodAttributeBundle (Method JavaDoc m) {
149         String JavaDoc signature = Util.getSignature (m);
150         if (m.getDeclaringClass () != clazz) {
151             throw new IllegalArgumentException JavaDoc ("There is no " + signature + " in " + clazz.getName () + ". It is defined in " +
152                 m.getDeclaringClass ().getName ());
153         }
154         
155         return getMethodOrConstructorAttributeBundle (methodAttributes, signature, m.getParameterTypes ().length + 2);
156     }
157     
158     /**
159      * Convenience method to get and initialize an entry in the constructor map.
160      *
161      * @return a fully initialized List (as defined for the <code>AttributeRepositoryClass</code> interface)
162      * for the given constructor.
163      */

164     private List JavaDoc getConstructorAttributeBundle (Constructor JavaDoc c) {
165         String JavaDoc signature = Util.getSignature (c);
166         if (c.getDeclaringClass () != clazz) {
167             throw new IllegalArgumentException JavaDoc ("There is no " + signature + " in " + clazz.getName () + ". It is defined in " +
168                 c.getDeclaringClass ().getName ());
169         }
170         
171         return getMethodOrConstructorAttributeBundle (constructorAttributes, signature, c.getParameterTypes ().length + 1);
172     }
173     
174     /**
175      * Adds an attribute to a field.
176      */

177     public void addFieldAttribute (String JavaDoc name, Object JavaDoc attribute) throws NoSuchFieldException JavaDoc, SecurityException JavaDoc {
178         addFieldAttribute (clazz.getDeclaredField (name), attribute);
179     }
180     
181     /**
182      * Adds an attribute to a field.
183      */

184     public void addFieldAttribute (Field JavaDoc f, Object JavaDoc attribute) {
185         checkSealed ();
186         String JavaDoc signature = f.getName ();
187         if (f.getDeclaringClass () != clazz) {
188             throw new IllegalArgumentException JavaDoc ("There is no " + signature + " in " + clazz.getName () + ". It is defined in " +
189                 f.getDeclaringClass ().getName ());
190         }
191         
192         Set JavaDoc attributeSet = (Set JavaDoc) fieldAttributes.get (signature);
193         if (attributeSet == null) {
194             attributeSet = new HashSet JavaDoc ();
195             fieldAttributes.put (signature, attributeSet);
196         }
197         
198         attributeSet.add (attribute);
199     }
200     
201     /**
202      * Adds an attribute to a constructor. The constructor is obtained via the getDeclaredConstrutor method
203      * of the class this repository defines.
204      */

205     public void addConstructorAttribute (Class JavaDoc[] parameters, Object JavaDoc attribute) throws NoSuchMethodException JavaDoc, SecurityException JavaDoc {
206         addConstructorAttribute (clazz.getDeclaredConstructor (parameters), attribute);
207     }
208     
209     /**
210      * Adds an attribute to a constructor.
211      */

212     public void addConstructorAttribute (Constructor JavaDoc c, Object JavaDoc attribute) {
213         checkSealed ();
214         List JavaDoc bundle = getConstructorAttributeBundle (c);
215         Set JavaDoc ctorAttrs = (Set JavaDoc) bundle.get (0);
216         ctorAttrs.add (attribute);
217     }
218     
219     /**
220      * Adds an attribute to a method. The method is obtained via the getDeclaredMethod method
221      * of the class this repository defines.
222      */

223     public void addMethodAttribute (String JavaDoc name, Class JavaDoc[] parameters, Object JavaDoc attribute) throws NoSuchMethodException JavaDoc, SecurityException JavaDoc {
224         addMethodAttribute (clazz.getDeclaredMethod (name, parameters), attribute);
225     }
226     
227     public void addMethodAttribute (Method JavaDoc m, Object JavaDoc attribute) {
228         checkSealed ();
229         List JavaDoc bundle = getMethodAttributeBundle (m);
230         Set JavaDoc methodAttrs = (Set JavaDoc) bundle.get (0);
231         methodAttrs.add (attribute);
232     }
233     
234     /**
235      * Adds an attribute to a parameter of a constructor. The constructor is obtained via the getDeclaredConstrutor method
236      * of the class this repository defines.
237      */

238     public void addParameterAttribute (Class JavaDoc[] parameters, int parameterIndex, Object JavaDoc attribute) throws NoSuchMethodException JavaDoc, SecurityException JavaDoc {
239         addParameterAttribute (clazz.getDeclaredConstructor (parameters), parameterIndex, attribute);
240     }
241     
242     /**
243      * Adds an attribute to a parameter of a constructor.
244      */

245     public void addParameterAttribute (Constructor JavaDoc c, int parameterIndex, Object JavaDoc attribute) {
246         checkSealed ();
247         List JavaDoc bundle = getConstructorAttributeBundle (c);
248         
249         Set JavaDoc parameterAttrs = (Set JavaDoc) bundle.get (parameterIndex + 1);
250         parameterAttrs.add (attribute);
251     }
252     
253     
254     /**
255      * Adds an attribute to a parameter of a method. The method is obtained via the getDeclaredMethod method
256      * of the class this repository defines.
257      */

258     public void addParameterAttribute (String JavaDoc name, Class JavaDoc[] parameters, int parameterIndex, Object JavaDoc attribute) throws NoSuchMethodException JavaDoc, SecurityException JavaDoc {
259         addParameterAttribute (clazz.getDeclaredMethod (name, parameters), parameterIndex, attribute);
260     }
261     
262     /**
263      * Adds an attribute to a parameter of a method. The method is obtained via the getDeclaredMethod method
264      * of the class this repository defines.
265      */

266     public void addParameterAttribute (Method JavaDoc m, int parameterIndex, Object JavaDoc attribute) {
267         checkSealed ();
268         List JavaDoc bundle = getMethodAttributeBundle (m);
269         
270         Set JavaDoc parameterAttrs = (Set JavaDoc) bundle.get (parameterIndex + 2);
271         parameterAttrs.add (attribute);
272     }
273     
274     /**
275      * Adds an attribute to the return value of a method. The method is obtained via the getDeclaredMethod method
276      * of the class this repository defines.
277      */

278     public void addReturnAttribute (String JavaDoc name, Class JavaDoc[] parameters, Object JavaDoc attribute) throws NoSuchMethodException JavaDoc, SecurityException JavaDoc {
279         addReturnAttribute (clazz.getDeclaredMethod (name, parameters), attribute);
280     }
281     
282     /**
283      * Adds an attribute to the return value of a method. The method is obtained via the getDeclaredMethod method
284      * of the class this repository defines.
285      */

286     public void addReturnAttribute (Method JavaDoc m, Object JavaDoc attribute) {
287         checkSealed ();
288         
289         List JavaDoc bundle = getMethodAttributeBundle (m);
290         
291         Set JavaDoc returnAttrs = (Set JavaDoc) bundle.get (1);
292         returnAttrs.add (attribute);
293     }
294     
295     /**
296      * Gets the class this repository defines attributes for.
297      */

298     public Class JavaDoc getDefinedClass () {
299         return clazz;
300     }
301     
302     public Set JavaDoc getClassAttributes () {
303         return classAttributes;
304     }
305     
306     public Map JavaDoc getFieldAttributes () {
307         return fieldAttributes;
308     }
309     
310     public Map JavaDoc getMethodAttributes () {
311         return methodAttributes;
312     }
313     
314     public Map JavaDoc getConstructorAttributes () {
315         return constructorAttributes;
316     }
317     
318     /**
319      * Seals this repository. A sealed repository can't be modified.
320      */

321     public void seal () {
322         sealed = true;
323     }
324 }
Popular Tags