1 /*
2 * Nanning Aspects
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7 package org.codehaus.nanning.attribute;
8
9 import java.lang.reflect.Field;
10 import java.lang.reflect.Method;
11 import java.net.URL;
12 import java.net.MalformedURLException;
13 import java.util.*;
14 import java.io.InputStream;
15 import java.io.IOException;
16
17 /***
18 * Main entry-point for the runtime access of 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 <em>different</em> packages the attributes with same name of these methods will collide.
23 *
24 * <!-- $Id: Attributes.java,v 1.1 2003/07/04 10:53:59 lecando Exp $ -->
25 *
26 * @author $Author: lecando $
27 * @version $Revision: 1.1 $
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 getAttribute(Class klass, String attribute) {
35 return getAttributes(klass).getAttribute(attribute);
36 }
37
38 public static String getAttribute(Method method, String attribute) {
39 return getAttributes(method.getDeclaringClass()).getAttribute(method, attribute);
40 }
41
42 public static String getAttribute(Field field, String attribute) {
43 return getAttributes(field.getDeclaringClass()).getAttribute(field, attribute);
44 }
45
46 public static void addSearchPath(URL searchPath) {
47 searchPaths.add(searchPath);
48 }
49
50 public static void removeSearchPath(URL searchPath) {
51 searchPaths.add(searchPath);
52 }
53
54 public static boolean hasAttribute(Class klass, String attribute) {
55 return getAttributes(klass).hasAttribute(attribute);
56 }
57
58 public static boolean hasAttribute(Method method, String attribute) {
59 return hasAttribute(method.getDeclaringClass(), method, attribute);
60 }
61
62 private static boolean hasAttribute(Class aClass, Method method, String attribute) {
63 return getAttributes(aClass).hasAttribute(method, attribute);
64 }
65
66 public static boolean hasAttribute(Field field, String attribute) {
67 return hasAttribute(field.getDeclaringClass(), field, attribute);
68 }
69
70 private static boolean hasAttribute(Class aClass, Field field, String attribute) {
71 return getAttributes(aClass).hasAttribute(field, attribute);
72 }
73
74 public static boolean hasInheritedAttribute(Field field, String attribute) {
75 return hasInheritedAttribute(field.getDeclaringClass(), field, attribute);
76 }
77
78 public static boolean hasInheritedAttribute(Class aClass, Field field, String 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[] interfaces = aClass.getInterfaces();
90 for (int i = 0; i < interfaces.length; i++) {
91 Class 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 method, String attribute) {
101 return hasInheritedAttribute(method.getDeclaringClass(), method, attribute);
102 }
103
104 public static boolean hasInheritedAttribute(Class aClass, Method method, String attribute) {
105 if (aClass == null) {
106 return false;
107 }
108
109 try {
110 method = aClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
111 } catch (NoSuchMethodException e) {
112 }
113
114 if (hasAttribute(aClass, method, attribute)) {
115 return true;
116 } else {
117 Class[] interfaces = aClass.getInterfaces();
118 for (int i = 0; i < interfaces.length; i++) {
119 Class anInterface = interfaces[i];
120 if (hasInheritedAttribute(anInterface, method, attribute)) {
121 return true;
122 }
123 }
124
125 Class superclass = aClass.getSuperclass();
126 if (hasInheritedAttribute(superclass, method, attribute)) {
127 return true;
128 }
129 return false;
130 }
131 }
132
133 public static String getInheritedAttribute(Class aClass, String attribute) {
134 if (aClass == null) {
135 return null;
136 }
137
138 if (hasAttribute(aClass, attribute)) {
139 return getAttribute(aClass, attribute);
140 } else {
141 String attributeValue = getInheritedAttribute(aClass.getSuperclass(), attribute);
142 if (attributeValue == null) {
143 Class[] interfaces = aClass.getInterfaces();
144 for (int i = 0; i < interfaces.length; i++) {
145 Class 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 aClass, String 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[] interfaces = aClass.getInterfaces();
168 for (int i = 0; i < interfaces.length; i++) {
169 Class 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 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[] getSearchPath() {
190 return (URL[]) searchPaths.toArray(new URL[0]);
191 }
192
193 static InputStream findFile(Class klass, String fileName) throws MalformedURLException {
194 InputStream 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 searchPath = (URL) iterator.next();
203 URL url = new URL(searchPath, fileName);
204 try {
205 inputStream = url.openStream();
206 } catch (IOException ignore) {
207 }
208 }
209 }
210 return inputStream;
211 }
212 }
This page was automatically generated by Maven