KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006 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  * variant of Aspects in the aspectj5rt project - this one isn't Java5 - Andy Clement
11  *******************************************************************************/

12 package org.aspectj.lang;
13
14
15 import java.lang.reflect.Method JavaDoc;
16 import java.lang.reflect.Modifier JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18
19 /**
20  * For users working on a level of Java prior to Java5, Aspects14 handles generic aspectOf methods when they
21  * are not available in the aspects but added later on through load time weaving. Users on Java5 should use
22  * the class Aspects instead.
23  * <p/>
24  * Aspects14.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 public class Aspects14 {
28
29     private final static Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
30     private final static Class JavaDoc[] PEROBJECT_CLASS_ARRAY = new Class JavaDoc[]{Object JavaDoc.class};
31     private final static Class JavaDoc[] PERTYPEWITHIN_CLASS_ARRAY = new Class JavaDoc[]{Class JavaDoc.class};
32     private final static Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
33     private final static String JavaDoc ASPECTOF = "aspectOf";
34     private final static String JavaDoc HASASPECT = "hasAspect";
35
36     /**
37      * Returns the singleton aspect or the percflow / percflowbelow associated with the current thread
38      *
39      * @param aspectClass
40      * @return
41      * @throws NoAspectBoundException if no such aspect
42      */

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

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

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

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

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

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

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

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