KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.util.List JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 class Util {
29     
30     public static String JavaDoc getSignature (Method JavaDoc m) {
31         return m.getName () + "(" + getParameterList (m.getParameterTypes ()) + ")";
32     }
33     
34     public static String JavaDoc getSignature (Constructor JavaDoc c) {
35         return "(" + getParameterList (c.getParameterTypes ()) + ")";
36     }
37     
38     public static String JavaDoc decodedClassName (String JavaDoc rawName) throws IllegalArgumentException JavaDoc {
39         if (!rawName.startsWith ("[")) {
40             return rawName;
41         } else {
42             StringBuffer JavaDoc nesting = new StringBuffer JavaDoc ();
43             int i = 0;
44             while (rawName.charAt (i) == '[') {
45                 nesting.append ("[]");
46                 i++;
47             }
48             String JavaDoc type = "";
49             switch (rawName.charAt (i)) {
50             case 'B': type = "byte"; break;
51             case 'C': type = "char"; break;
52             case 'D': type = "double"; break;
53             case 'F': type = "float"; break;
54             case 'I': type = "int"; break;
55             case 'J': type = "long"; break;
56             case 'L': type = rawName.substring (i + 1, rawName.length () - 1); break;
57             case 'S': type = "short"; break;
58             case 'Z': type = "boolean"; break;
59             default: throw new IllegalArgumentException JavaDoc ("Can't decode " + rawName);
60             }
61             
62             return type + nesting.toString ();
63         }
64     }
65     
66     public static String JavaDoc getParameterList (Class JavaDoc[] params) {
67         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
68         for (int i = 0; i < params.length; i++) {
69             if (i > 0) {
70                 sb.append (",");
71             }
72             sb.append (decodedClassName (params[i].getName ()));
73         }
74         return sb.toString ();
75     }
76     
77     public static void checkTarget (int target, Object JavaDoc attribute, String JavaDoc element) {
78         Target targetAttr = (Target) Attributes.getAttribute (attribute.getClass (), Target.class);
79         if (targetAttr == null) {
80             return;
81         }
82         
83         if ((targetAttr.getFlags () & target) == 0) {
84             throw new InvalidAttributeTargetError (attribute.getClass ().getName (), element, targetAttr.getFlags ());
85         }
86     }
87     
88     public static void checkTarget (int target, Set JavaDoc attributes, String JavaDoc element) {
89         Iterator JavaDoc iter = attributes.iterator ();
90         while (iter.hasNext ()) {
91             checkTarget (target, iter.next (), element);
92         }
93     }
94     
95     public static void validateRepository (Class JavaDoc owningClass, AttributeRepositoryClass repo) {
96         checkTarget (Target.CLASS, repo.getClassAttributes (), owningClass.getName ());
97         
98         Map JavaDoc fieldAttrs = repo.getFieldAttributes ();
99         for (Iterator JavaDoc iter = fieldAttrs.keySet ().iterator(); iter.hasNext ();) {
100             String JavaDoc fieldName = (String JavaDoc) iter.next ();
101             checkTarget (Target.FIELD, (Collection JavaDoc) fieldAttrs.get (fieldName), owningClass.getName () + "." + fieldName);
102         }
103         
104         Map JavaDoc ctorAttrs = repo.getConstructorAttributes ();
105         for (Iterator JavaDoc iter = ctorAttrs.keySet ().iterator(); iter.hasNext ();) {
106             String JavaDoc ctorName = (String JavaDoc) iter.next ();
107             List JavaDoc bundle = (List JavaDoc) ctorAttrs.get (ctorName);
108             
109             for (int i = 0; i < bundle.size (); i++) {
110                 switch (i) {
111                 case 0:
112                     checkTarget (Target.CONSTRUCTOR, (Collection JavaDoc) bundle.get (0), owningClass.getName () + "." + ctorName);
113                     break;
114                     
115                 default:
116                     checkTarget (Target.CONSTRUCTOR_PARAMETER, (Collection JavaDoc) bundle.get (i), "parameter " + (i) + " of " + owningClass.getName () + ctorName);
117                 }
118             }
119         }
120         
121         Map JavaDoc methodAttrs = repo.getMethodAttributes ();
122         for (Iterator JavaDoc iter = methodAttrs.keySet ().iterator(); iter.hasNext ();) {
123             String JavaDoc methodName = (String JavaDoc) iter.next ();
124             List JavaDoc bundle = (List JavaDoc) methodAttrs.get (methodName);
125             
126             for (int i = 0; i < bundle.size (); i++) {
127                 switch (i) {
128                 case 0:
129                     checkTarget (Target.METHOD, (Collection JavaDoc) bundle.get (0), owningClass.getName () + "." + methodName);
130                     break;
131                     
132                 case 1:
133                     checkTarget (Target.RETURN, (Collection JavaDoc) bundle.get (1), "return value of " + owningClass.getName () + "." + methodName);
134                     break;
135                     
136                 default:
137                     checkTarget (Target.METHOD_PARAMETER, (Collection JavaDoc) bundle.get (i), "parameter " + (i - 1) + " of " + owningClass.getName () + "." + methodName);
138                 }
139             }
140         }
141     }
142 }
Popular Tags