You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Aspects.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. import java.lang.reflect.Method;
  15. import java.lang.reflect.Modifier;
  16. import java.lang.reflect.InvocationTargetException;
  17. /**
  18. * Handles generic aspectOf method when those are not available in the aspects but added later on
  19. * thru load time weaving.
  20. * Aspects.aspectOf(..) is doing reflective calls to the aspect aspectOf, so for better performance
  21. * consider using ajc compilation of the aspects and using them as a binary dependancies in your project.
  22. *
  23. * @author Alexandre Vasseur
  24. */
  25. public class Aspects {
  26. private final static Class[] EMPTY_CLASS_ARRAY = new Class[0];
  27. private final static Class[] PEROBJECT_CLASS_ARRAY = new Class[]{Object.class};
  28. private final static Class[] PERTYPEWITHIN_CLASS_ARRAY = new Class[]{Class.class};
  29. private final static Object[] EMPTY_OBJECT_ARRAY = new Object[0];
  30. private final static String ASPECTOF = "aspectOf";
  31. private final static String HASASPECT = "hasAspect";
  32. /**
  33. * @param <T> the expected class of the aspect
  34. * @param aspectClass the aspect class
  35. * @return the singleton aspect or the percflow / percflowbelow associated with the current thread
  36. * @throws NoAspectBoundException if no such aspect
  37. */
  38. public static <T> T aspectOf(Class<T> aspectClass) throws NoAspectBoundException {
  39. try {
  40. return (T) getSingletonOrThreadAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY);
  41. } catch (InvocationTargetException e) {
  42. //FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs
  43. throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause());
  44. } catch (Exception e) {
  45. throw new NoAspectBoundException(aspectClass.getName(), e);
  46. }
  47. }
  48. /**
  49. * @param <T> the expected class of the aspect
  50. * @param aspectClass the aspect class
  51. * @param perObject the this/target object for which to look for an aspect instance
  52. * @return the associated perthis / pertarget aspect instance
  53. * @throws NoAspectBoundException if no such aspect, or no aspect bound
  54. */
  55. public static <T> T aspectOf(Class<T> aspectClass, Object perObject) throws NoAspectBoundException {
  56. try {
  57. return (T) getPerObjectAspectOf(aspectClass).invoke(null, new Object[]{perObject});
  58. } catch (InvocationTargetException e) {
  59. //FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs
  60. throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause());
  61. } catch (Exception e) {
  62. throw new NoAspectBoundException(aspectClass.getName(), e);
  63. }
  64. }
  65. /**
  66. * @param <T> the expected class of the aspect
  67. * @param aspectClass the aspect class
  68. * @param perTypeWithin the class for which to search for an aspect instance
  69. * @return the associated aspect instance
  70. * @throws NoAspectBoundException if no such aspect, or no aspect bound
  71. */
  72. public static <T> T aspectOf(Class<T> aspectClass, Class<?> perTypeWithin) throws NoAspectBoundException {
  73. try {
  74. return (T) getPerTypeWithinAspectOf(aspectClass).invoke(null, new Object[]{perTypeWithin});
  75. } catch (InvocationTargetException e) {
  76. // FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs
  77. throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause());
  78. } catch (Exception e) {
  79. throw new NoAspectBoundException(aspectClass.getName(), e);
  80. }
  81. }
  82. /**
  83. * @param aspectClass the aspect class
  84. * @return true if singleton aspect or percflow / percflowbelow aspect is bound
  85. * @throws NoAspectBoundException if not bound
  86. */
  87. public static boolean hasAspect(Class<?> aspectClass) throws NoAspectBoundException {
  88. try {
  89. return ((Boolean)getSingletonOrThreadHasAspect(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY)).booleanValue();
  90. } catch (Exception e) {
  91. return false;
  92. }
  93. }
  94. /**
  95. * @param aspectClass the aspect class
  96. * @param perObject the this/target object for which to look for an aspect instance
  97. * @return true if the perthis / pertarget aspect is bound
  98. * @throws NoAspectBoundException if not bound
  99. */
  100. public static boolean hasAspect(Class<?> aspectClass, Object perObject) throws NoAspectBoundException {
  101. try {
  102. return ((Boolean)getPerObjectHasAspect(aspectClass).invoke(null, new Object[]{perObject})).booleanValue();
  103. } catch (Exception e) {
  104. return false;
  105. }
  106. }
  107. /**
  108. * @param aspectClass the aspect class
  109. * @param perTypeWithin class
  110. * @return true if the pertypewithin aspect is bound
  111. * @throws NoAspectBoundException if not bound
  112. */
  113. public static boolean hasAspect(Class<?> aspectClass, Class<?> perTypeWithin) throws NoAspectBoundException {
  114. try {
  115. return ((Boolean)getPerTypeWithinHasAspect(aspectClass).invoke(null, new Object[]{perTypeWithin})).booleanValue();
  116. } catch (Exception e) {
  117. return false;
  118. }
  119. }
  120. // -- aspectOf
  121. private static Method getSingletonOrThreadAspectOf(Class<?> aspectClass) throws NoSuchMethodException {
  122. Method method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY);
  123. return checkAspectOf(method, aspectClass);
  124. }
  125. private static Method getPerObjectAspectOf(Class<?> aspectClass) throws NoSuchMethodException {
  126. Method method = aspectClass.getDeclaredMethod(ASPECTOF, PEROBJECT_CLASS_ARRAY);
  127. return checkAspectOf(method, aspectClass);
  128. }
  129. private static Method getPerTypeWithinAspectOf(Class<?> aspectClass) throws NoSuchMethodException {
  130. Method method = aspectClass.getDeclaredMethod(ASPECTOF, PERTYPEWITHIN_CLASS_ARRAY);
  131. return checkAspectOf(method, aspectClass);
  132. }
  133. private static Method checkAspectOf(Method method, Class<?> aspectClass) throws NoSuchMethodException {
  134. method.setAccessible(true);
  135. if (!method.isAccessible()
  136. || !Modifier.isPublic(method.getModifiers())
  137. || !Modifier.isStatic(method.getModifiers())) {
  138. throw new NoSuchMethodException(aspectClass.getName() + ".aspectOf(..) is not accessible public static");
  139. }
  140. return method;
  141. }
  142. // -- hasAspect
  143. private static Method getSingletonOrThreadHasAspect(Class aspectClass) throws NoSuchMethodException {
  144. Method method = aspectClass.getDeclaredMethod(HASASPECT, EMPTY_CLASS_ARRAY);
  145. return checkHasAspect(method, aspectClass);
  146. }
  147. private static Method getPerObjectHasAspect(Class aspectClass) throws NoSuchMethodException {
  148. Method method = aspectClass.getDeclaredMethod(HASASPECT, PEROBJECT_CLASS_ARRAY);
  149. return checkHasAspect(method, aspectClass);
  150. }
  151. private static Method getPerTypeWithinHasAspect(Class aspectClass) throws NoSuchMethodException {
  152. Method method = aspectClass.getDeclaredMethod(HASASPECT, PERTYPEWITHIN_CLASS_ARRAY);
  153. return checkHasAspect(method, aspectClass);
  154. }
  155. private static Method checkHasAspect(Method method, Class aspectClass) throws NoSuchMethodException {
  156. method.setAccessible(true);
  157. if (!method.isAccessible()
  158. || !Modifier.isPublic(method.getModifiers())
  159. || !Modifier.isStatic(method.getModifiers())) {
  160. throw new NoSuchMethodException(aspectClass.getName() + ".hasAspect(..) is not accessible public static");
  161. }
  162. return method;
  163. }
  164. }