KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > lang > Aspects


1 /*******************************************************************************
2  * Copyright (c) 2005 Contributors.
3  * All rights reserved.
4  * This program and the accompanying materials are made available
5  * under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution and is available at
7  * http://eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * initial implementation Alexandre Vasseur
11  * generic signature update Adrian Colyer
12  *******************************************************************************/

13 package org.aspectj.lang;
14
15
16 import java.lang.reflect.Method JavaDoc;
17 import java.lang.reflect.Modifier JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19
20 /**
21  * Handles generic aspectOf method when those are not available in the aspects but added later on
22  * thru load time weaving.
23  * <p/>
24  * Aspects.aspectOf(..) is doing reflective calls to the aspect aspectOf, so for better performance
25  * consider using ajc compilation of the aspects and using them as a binary dependancies in your project.
26  *
27  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
28  */

29 public class Aspects {
30
31     private final static Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
32     private final static Class JavaDoc[] PEROBJECT_CLASS_ARRAY = new Class JavaDoc[]{Object JavaDoc.class};
33     private final static Class JavaDoc[] PERTYPEWITHIN_CLASS_ARRAY = new Class JavaDoc[]{Class JavaDoc.class};
34     private final static Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
35     private final static String JavaDoc ASPECTOF = "aspectOf";
36     private final static String JavaDoc HASASPECT = "hasAspect";
37
38     /**
39      * Returns the singleton aspect or the percflow / percflowbelow associated with the current thread
40      *
41      * @param aspectClass
42      * @return
43      * @throws NoAspectBoundException if no such aspect
44      */

45     public static <T> T aspectOf(Class JavaDoc<T> aspectClass) throws NoAspectBoundException {
46         try {
47             return (T) getSingletonOrThreadAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY);
48         } catch (InvocationTargetException JavaDoc e) {
49             //FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs
50
throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause());
51
} catch (Exception JavaDoc e) {
52             throw new NoAspectBoundException(aspectClass.getName(), e);
53         }
54     }
55
56     /**
57      * Returns the perthis / pertarget aspect
58      * @param aspectClass
59      * @param perObject
60      * @return
61      * @throws NoAspectBoundException if no such aspect, or no aspect bound
62      */

63     public static <T> T aspectOf(Class JavaDoc<T> aspectClass, Object JavaDoc perObject) throws NoAspectBoundException {
64         try {
65             return (T) getPerObjectAspectOf(aspectClass).invoke(null, new Object JavaDoc[]{perObject});
66         } catch (InvocationTargetException JavaDoc e) {
67             //FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs
68
throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause());
69
} catch (Exception JavaDoc e) {
70             throw new NoAspectBoundException(aspectClass.getName(), e);
71         }
72     }
73
74     /**
75      * Returns the pertypewithin aspect
76      * @param aspectClass
77      * @param perTypeWithin class
78      * @return
79      * @throws NoAspectBoundException if no such aspect, or no aspect bound
80      */

81     public static <T> T aspectOf(Class JavaDoc<T> aspectClass, Class JavaDoc<?> perTypeWithin) throws NoAspectBoundException {
82         try {
83             return (T) getPerTypeWithinAspectOf(aspectClass).invoke(null, new Object JavaDoc[]{perTypeWithin});
84         } catch (InvocationTargetException JavaDoc e) {
85 // FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs
86
throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause());
87
} catch (Exception JavaDoc e) {
88             throw new NoAspectBoundException(aspectClass.getName(), e);
89         }
90     }
91
92     /**
93      * Returns true if singleton aspect or percflow / percflowbelow aspect is bound
94      *
95      * @param aspectClass
96      * @return
97      * @throws NoAspectBoundException if not bound
98      */

99     public static boolean hasAspect(Class JavaDoc<?> aspectClass) throws NoAspectBoundException {
100         try {
101             return ((Boolean JavaDoc)getSingletonOrThreadHasAspect(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY)).booleanValue();
102         } catch (Exception JavaDoc e) {
103             return false;
104         }
105     }
106
107     /**
108      * Returns true if the perthis / pertarget aspect is bound
109      * @param aspectClass
110      * @param perObject
111      * @return
112      * @throws NoAspectBoundException if not bound
113      */

114     public static boolean hasAspect(Class JavaDoc<?> aspectClass, Object JavaDoc perObject) throws NoAspectBoundException {
115         try {
116             return ((Boolean JavaDoc)getPerObjectHasAspect(aspectClass).invoke(null, new Object JavaDoc[]{perObject})).booleanValue();
117         } catch (Exception JavaDoc e) {
118             return false;
119         }
120     }
121
122     /**
123      * Returns true if the pertypewithin aspect is bound
124      * @param aspectClass
125      * @param perTypeWithin class
126      * @return
127      * @throws NoAspectBoundException if not bound
128      */

129     public static boolean hasAspect(Class JavaDoc<?> aspectClass, Class JavaDoc<?> perTypeWithin) throws NoAspectBoundException {
130         try {
131             return ((Boolean JavaDoc)getPerTypeWithinHasAspect(aspectClass).invoke(null, new Object JavaDoc[]{perTypeWithin})).booleanValue();
132         } catch (Exception JavaDoc e) {
133             return false;
134         }
135     }
136
137     // -- aspectOf
138

139     private static Method JavaDoc getSingletonOrThreadAspectOf(Class JavaDoc<?> aspectClass) throws NoSuchMethodException JavaDoc {
140         Method JavaDoc method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY);
141         return checkAspectOf(method, aspectClass);
142     }
143
144     private static Method JavaDoc getPerObjectAspectOf(Class JavaDoc<?> aspectClass) throws NoSuchMethodException JavaDoc {
145         Method JavaDoc method = aspectClass.getDeclaredMethod(ASPECTOF, PEROBJECT_CLASS_ARRAY);
146         return checkAspectOf(method, aspectClass);
147     }
148
149     private static Method JavaDoc getPerTypeWithinAspectOf(Class JavaDoc<?> aspectClass) throws NoSuchMethodException JavaDoc {
150         Method JavaDoc method = aspectClass.getDeclaredMethod(ASPECTOF, PERTYPEWITHIN_CLASS_ARRAY);
151         return checkAspectOf(method, aspectClass);
152     }
153
154     private static Method JavaDoc checkAspectOf(Method JavaDoc method, Class JavaDoc<?> aspectClass) throws NoSuchMethodException JavaDoc {
155         method.setAccessible(true);
156         if (!method.isAccessible()
157             || !Modifier.isPublic(method.getModifiers())
158             || !Modifier.isStatic(method.getModifiers())) {
159             throw new NoSuchMethodException JavaDoc(aspectClass.getName() + ".aspectOf(..) is not accessible public static");
160         }
161         return method;
162     }
163
164     // -- hasAspect
165

166     private static Method JavaDoc getSingletonOrThreadHasAspect(Class JavaDoc aspectClass) throws NoSuchMethodException JavaDoc {
167         Method JavaDoc method = aspectClass.getDeclaredMethod(HASASPECT, EMPTY_CLASS_ARRAY);
168         return checkHasAspect(method, aspectClass);
169     }
170
171     private static Method JavaDoc getPerObjectHasAspect(Class JavaDoc aspectClass) throws NoSuchMethodException JavaDoc {
172         Method JavaDoc method = aspectClass.getDeclaredMethod(HASASPECT, PEROBJECT_CLASS_ARRAY);
173         return checkHasAspect(method, aspectClass);
174     }
175
176     private static Method JavaDoc getPerTypeWithinHasAspect(Class JavaDoc aspectClass) throws NoSuchMethodException JavaDoc {
177         Method JavaDoc method = aspectClass.getDeclaredMethod(HASASPECT, PERTYPEWITHIN_CLASS_ARRAY);
178         return checkHasAspect(method, aspectClass);
179     }
180
181     private static Method JavaDoc checkHasAspect(Method JavaDoc method, Class JavaDoc aspectClass) throws NoSuchMethodException JavaDoc {
182         method.setAccessible(true);
183         if (!method.isAccessible()
184             || !Modifier.isPublic(method.getModifiers())
185             || !Modifier.isStatic(method.getModifiers())) {
186             throw new NoSuchMethodException JavaDoc(aspectClass.getName() + ".hasAspect(..) is not accessible public static");
187         }
188         return method;
189     }
190 }
191
Popular Tags