KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > attribute > Attributes


1 /*
2  * Nanning Aspects
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package com.tirsen.nanning.attribute;
8
9 import java.lang.reflect.Field JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.util.*;
14 import java.io.InputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * TODO document Attributes
19  *
20  * TODO: there's actually a nasty little bug in here
21  * If there are two methods with the same name, same set of arguments with types with same name but
22  * in _different_ packages the attributes with same name of these methods will collide.
23
24  * <!-- $Id: Attributes.java,v 1.14 2003/06/10 05:26:46 tirsen Exp $ -->
25  *
26  * @author $Author: tirsen $
27  * @version $Revision: 1.14 $
28  */

29
30 public class Attributes {
31     private static List searchPaths = new ArrayList();
32     private static Map classAttributesCache = new HashMap();
33
34     public static String JavaDoc getAttribute(Class JavaDoc klass, String JavaDoc attribute) {
35         return getAttributes(klass).getAttribute(attribute);
36     }
37
38     public static String JavaDoc getAttribute(Method JavaDoc method, String JavaDoc attribute) {
39         return getAttributes(method.getDeclaringClass()).getAttribute(method, attribute);
40     }
41
42     public static String JavaDoc getAttribute(Field JavaDoc field, String JavaDoc attribute) {
43         return getAttributes(field.getDeclaringClass()).getAttribute(field, attribute);
44     }
45
46     public static void addSearchPath(URL JavaDoc searchPath) {
47         searchPaths.add(searchPath);
48     }
49
50     public static void removeSearchPath(URL JavaDoc searchPath) {
51         searchPaths.add(searchPath);
52     }
53
54     public static boolean hasAttribute(Class JavaDoc klass, String JavaDoc attribute) {
55         return getAttributes(klass).hasAttribute(attribute);
56     }
57
58     public static boolean hasAttribute(Method JavaDoc method, String JavaDoc attribute) {
59         return hasAttribute(method.getDeclaringClass(), method, attribute);
60     }
61
62     private static boolean hasAttribute(Class JavaDoc aClass, Method JavaDoc method, String JavaDoc attribute) {
63         return getAttributes(aClass).hasAttribute(method, attribute);
64     }
65
66     public static boolean hasAttribute(Field JavaDoc field, String JavaDoc attribute) {
67         return hasAttribute(field.getDeclaringClass(), field, attribute);
68     }
69
70     private static boolean hasAttribute(Class JavaDoc aClass, Field JavaDoc field, String JavaDoc attribute) {
71         return getAttributes(aClass).hasAttribute(field, attribute);
72     }
73
74     public static boolean hasInheritedAttribute(Field JavaDoc field, String JavaDoc attribute) {
75         return hasInheritedAttribute(field.getDeclaringClass(), field, attribute);
76     }
77
78     public static boolean hasInheritedAttribute(Class JavaDoc aClass, Field JavaDoc field, String JavaDoc attribute) {
79         if (aClass == null) {
80             return false;
81         }
82
83         if (hasAttribute(aClass, field, attribute)) {
84             return true;
85         } else {
86             if (hasInheritedAttribute(aClass.getSuperclass(), field, attribute)) {
87                 return true;
88             }
89             Class JavaDoc[] interfaces = aClass.getInterfaces();
90             for (int i = 0; i < interfaces.length; i++) {
91                 Class JavaDoc anInterface = interfaces[i];
92                 if (hasInheritedAttribute(anInterface, field, attribute)) {
93                     return true;
94                 }
95             }
96             return false;
97         }
98     }
99
100     public static boolean hasInheritedAttribute(Method JavaDoc method, String JavaDoc attribute) {
101         return hasInheritedAttribute(method.getDeclaringClass(), method, attribute);
102     }
103
104     public static boolean hasInheritedAttribute(Class JavaDoc aClass, Method JavaDoc method, String JavaDoc attribute) {
105         if (aClass == null) {
106             return false;
107         }
108
109         try {
110             method = aClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
111         } catch (NoSuchMethodException JavaDoc e) {
112         }
113
114         if (hasAttribute(aClass, method, attribute)) {
115             return true;
116         } else {
117             Class JavaDoc[] interfaces = aClass.getInterfaces();
118             for (int i = 0; i < interfaces.length; i++) {
119                 Class JavaDoc anInterface = interfaces[i];
120                 if (hasInheritedAttribute(anInterface, method, attribute)) {
121                     return true;
122                 }
123             }
124
125             Class JavaDoc superclass = aClass.getSuperclass();
126             if (hasInheritedAttribute(superclass, method, attribute)) {
127                 return true;
128             }
129             return false;
130         }
131     }
132
133     public static String JavaDoc getInheritedAttribute(Class JavaDoc aClass, String JavaDoc attribute) {
134         if (aClass == null) {
135             return null;
136         }
137
138         if (hasAttribute(aClass, attribute)) {
139             return getAttribute(aClass, attribute);
140         } else {
141             String JavaDoc attributeValue = getInheritedAttribute(aClass.getSuperclass(), attribute);
142             if (attributeValue == null) {
143                 Class JavaDoc[] interfaces = aClass.getInterfaces();
144                 for (int i = 0; i < interfaces.length; i++) {
145                     Class JavaDoc anInterface = interfaces[i];
146                     attributeValue = getInheritedAttribute(anInterface, attribute);
147                     if (attributeValue != null) {
148                         break;
149                     }
150                 }
151             }
152             return attributeValue;
153         }
154     }
155
156     public static boolean hasInheritedAttribute(Class JavaDoc aClass, String JavaDoc attribute) {
157         if (aClass == null) {
158             return false;
159         }
160
161         if (hasAttribute(aClass, attribute)) {
162             return true;
163         } else {
164             if (hasInheritedAttribute(aClass.getSuperclass(), attribute)) {
165                 return true;
166             }
167             Class JavaDoc[] interfaces = aClass.getInterfaces();
168             for (int i = 0; i < interfaces.length; i++) {
169                 Class JavaDoc anInterface = interfaces[i];
170                 if (hasInheritedAttribute(anInterface, attribute)) {
171                     return true;
172                 }
173             }
174             return false;
175         }
176     }
177
178     public static ClassAttributes getAttributes(Class JavaDoc aClass) {
179         ClassAttributes classAttributes = (ClassAttributes) classAttributesCache.get(aClass);
180         if (classAttributes == null) {
181             classAttributes = new ClassAttributes(aClass);
182             new PropertyFileAttributeLoader().load(classAttributes);
183             new AttributesXMLParser().load(classAttributes);
184             classAttributesCache.put(aClass, classAttributes);
185         }
186         return classAttributes;
187     }
188
189     public static URL JavaDoc[] getSearchPath() {
190         return (URL JavaDoc[]) searchPaths.toArray(new URL JavaDoc[0]);
191     }
192
193     static InputStream JavaDoc findFile(Class JavaDoc klass, String JavaDoc fileName) throws MalformedURLException JavaDoc {
194         InputStream JavaDoc inputStream = klass.getResourceAsStream(fileName);
195
196         if (inputStream == null) {
197             inputStream = klass.getResourceAsStream('/' + fileName);
198         }
199
200         if (inputStream == null) {
201             for (Iterator iterator = searchPaths.iterator(); iterator.hasNext();) {
202                 URL JavaDoc searchPath = (URL JavaDoc) iterator.next();
203                 URL JavaDoc url = new URL JavaDoc(searchPath, fileName);
204                 try {
205                     inputStream = url.openStream();
206                 } catch (IOException JavaDoc ignore) {
207                 }
208             }
209         }
210         return inputStream;
211     }
212 }
Popular Tags