diff options
author | avasseur <avasseur> | 2005-05-02 08:17:36 +0000 |
---|---|---|
committer | avasseur <avasseur> | 2005-05-02 08:17:36 +0000 |
commit | cdddd38d261f0ae75bf95f0859e7a440be1bb3d5 (patch) | |
tree | fa61448ab8b0d848cd65930fc5e9a44a71433ea8 /runtime/src | |
parent | 0b7744f560fb4ff70b70884a4932c8ebeb603fd2 (diff) | |
download | aspectj-cdddd38d261f0ae75bf95f0859e7a440be1bb3d5.tar.gz aspectj-cdddd38d261f0ae75bf95f0859e7a440be1bb3d5.zip |
PTW perClause for @AJ + perClause test
Diffstat (limited to 'runtime/src')
-rw-r--r-- | runtime/src/org/aspectj/lang/Aspects.java | 112 |
1 files changed, 102 insertions, 10 deletions
diff --git a/runtime/src/org/aspectj/lang/Aspects.java b/runtime/src/org/aspectj/lang/Aspects.java index 0d8cb5ccb..dd1286365 100644 --- a/runtime/src/org/aspectj/lang/Aspects.java +++ b/runtime/src/org/aspectj/lang/Aspects.java @@ -13,13 +13,14 @@ package org.aspectj.lang; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.lang.reflect.InvocationTargetException; /** * Handles generic aspectOf method when those are not available in the aspects but added later on * thru load time weaving. * <p/> * Aspects.aspectOf(..) is doing reflective calls to the aspect aspectOf, so for better performance - * consider using preparation of the aspects. + * consider using ajc compilation of the aspects and using them as a binary dependancies in your project. * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */ @@ -27,11 +28,13 @@ public class Aspects { private final static Class[] EMPTY_CLASS_ARRAY = new Class[0]; private final static Class[] PEROBJECT_CLASS_ARRAY = new Class[]{Object.class}; + private final static Class[] PERTYPEWITHIN_CLASS_ARRAY = new Class[]{Class.class}; private final static Object[] EMPTY_OBJECT_ARRAY = new Object[0]; private final static String ASPECTOF = "aspectOf"; + private final static String HASASPECT = "hasAspect"; /** - * Returns the singleton aspect + * Returns the singleton aspect or the percflow / percflowbelow associated with the current thread * * @param aspectClass * @return @@ -39,7 +42,9 @@ public class Aspects { */ public static Object aspectOf(Class aspectClass) throws NoAspectBoundException { try { - return getSingletonAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY); + return getSingletonOrThreadAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY); + } catch (InvocationTargetException e) { + throw new NoAspectBoundException(aspectClass.getName(), e.getCause()); } catch (Exception e) { throw new NoAspectBoundException(aspectClass.getName(), e); } @@ -55,22 +60,78 @@ public class Aspects { public static Object aspectOf(Class aspectClass, Object perObject) throws NoAspectBoundException { try { return getPerObjectAspectOf(aspectClass).invoke(null, new Object[]{perObject}); + } catch (InvocationTargetException e) { + throw new NoAspectBoundException(aspectClass.getName(), e.getCause()); } catch (Exception e) { throw new NoAspectBoundException(aspectClass.getName(), e); } } - public static Object aspectOf(Class aspectClass, Thread perThread) throws NoAspectBoundException { - //TODO - how to know it s a real per Thread ? - // if it is actually a singleton one, we will have it as well... + /** + * Returns the pertypewithin aspect + * @param aspectClass + * @param perTypeWithin class + * @return + * @throws NoAspectBoundException if no such aspect, or no aspect bound + */ + public static Object aspectOf(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException { try { - return getSingletonAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY); + return getPerTypeWithinAspectOf(aspectClass).invoke(null, new Object[]{perTypeWithin}); + } catch (InvocationTargetException e) { + throw new NoAspectBoundException(aspectClass.getName(), e.getCause()); } catch (Exception e) { throw new NoAspectBoundException(aspectClass.getName(), e); } } - private static Method getSingletonAspectOf(Class aspectClass) throws NoSuchMethodException { + /** + * Returns true if singleton aspect or percflow / percflowbelow aspect is bound + * + * @param aspectClass + * @return + * @throws NoAspectBoundException if not bound + */ + public static boolean hasAspect(Class aspectClass) throws NoAspectBoundException { + try { + return ((Boolean)getSingletonOrThreadHasAspect(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY)).booleanValue(); + } catch (Exception e) { + return false; + } + } + + /** + * Returns true if the perthis / pertarget aspect is bound + * @param aspectClass + * @param perObject + * @return + * @throws NoAspectBoundException if not bound + */ + public static boolean hasAspect(Class aspectClass, Object perObject) throws NoAspectBoundException { + try { + return ((Boolean)getPerObjectHasAspect(aspectClass).invoke(null, new Object[]{perObject})).booleanValue(); + } catch (Exception e) { + return false; + } + } + + /** + * Returns true if the pertypewithin aspect is bound + * @param aspectClass + * @param perTypeWithin class + * @return + * @throws NoAspectBoundException if not bound + */ + public static boolean hasAspect(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException { + try { + return ((Boolean)getPerTypeWithinHasAspect(aspectClass).invoke(null, new Object[]{perTypeWithin})).booleanValue(); + } catch (Exception e) { + return false; + } + } + + // -- aspectOf + + private static Method getSingletonOrThreadAspectOf(Class aspectClass) throws NoSuchMethodException { Method method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY); return checkAspectOf(method, aspectClass); } @@ -80,8 +141,12 @@ public class Aspects { return checkAspectOf(method, aspectClass); } - private static Method checkAspectOf(Method method, Class aspectClass) - throws NoSuchMethodException { + private static Method getPerTypeWithinAspectOf(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(ASPECTOF, PERTYPEWITHIN_CLASS_ARRAY); + return checkAspectOf(method, aspectClass); + } + + private static Method checkAspectOf(Method method, Class aspectClass) throws NoSuchMethodException { method.setAccessible(true); if (!method.isAccessible() || !Modifier.isPublic(method.getModifiers()) @@ -90,4 +155,31 @@ public class Aspects { } return method; } + + // -- hasAspect + + private static Method getSingletonOrThreadHasAspect(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(HASASPECT, EMPTY_CLASS_ARRAY); + return checkHasAspect(method, aspectClass); + } + + private static Method getPerObjectHasAspect(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(HASASPECT, PEROBJECT_CLASS_ARRAY); + return checkHasAspect(method, aspectClass); + } + + private static Method getPerTypeWithinHasAspect(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(HASASPECT, PERTYPEWITHIN_CLASS_ARRAY); + return checkHasAspect(method, aspectClass); + } + + private static Method checkHasAspect(Method method, Class aspectClass) throws NoSuchMethodException { + method.setAccessible(true); + if (!method.isAccessible() + || !Modifier.isPublic(method.getModifiers()) + || !Modifier.isStatic(method.getModifiers())) { + throw new NoSuchMethodException(aspectClass.getName() + ".hasAspect(..) is not accessible public static"); + } + return method; + } } |