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.

Aspects14.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. import java.lang.reflect.InvocationTargetException;
  14. import java.lang.reflect.Method;
  15. import java.lang.reflect.Modifier;
  16. /**
  17. * For users working on a level of Java prior to Java5, Aspects14 handles generic aspectOf methods when they
  18. * are not available in the aspects but added later on through load time weaving. Users on Java5 should use
  19. * the class Aspects instead.
  20. * <p/>
  21. * Aspects14.aspectOf(..) is doing reflective calls to the aspect aspectOf, so for better performance
  22. * consider using ajc compilation of the aspects and using them as a binary dependancies in your project.
  23. */
  24. public class Aspects14 {
  25. private final static Class[] EMPTY_CLASS_ARRAY = new Class[0];
  26. private final static Class[] PEROBJECT_CLASS_ARRAY = new Class[]{Object.class};
  27. private final static Class[] PERTYPEWITHIN_CLASS_ARRAY = new Class[]{Class.class};
  28. private final static Object[] EMPTY_OBJECT_ARRAY = new Object[0];
  29. private final static String ASPECTOF = "aspectOf";
  30. private final static String HASASPECT = "hasAspect";
  31. /**
  32. * Returns the singleton aspect or the percflow / percflowbelow associated with the current thread
  33. *
  34. * @param aspectClass aspect class for which to discover the aspect instance
  35. * @return an aspect instance
  36. * @throws NoAspectBoundException if no such aspect
  37. */
  38. public static Object aspectOf(Class aspectClass) throws NoAspectBoundException {
  39. try {
  40. return 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. * Returns the perthis / pertarget aspect
  50. * @param aspectClass aspect class for which to discover the aspect instance
  51. * @param perObject object for which to discover the aspect instance
  52. * @return an aspect instance
  53. * @throws NoAspectBoundException if no such aspect, or no aspect bound
  54. */
  55. public static Object aspectOf(Class aspectClass, Object perObject) throws NoAspectBoundException {
  56. try {
  57. return 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. * Returns the pertypewithin aspect
  67. * @param aspectClass aspect class for which to discover the aspect instance
  68. * @param perTypeWithin class
  69. * @return
  70. * @throws NoAspectBoundException if no such aspect, or no aspect bound
  71. */
  72. public static Object aspectOf(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException {
  73. try {
  74. return 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. * Returns true if singleton aspect or percflow / percflowbelow aspect is bound
  84. *
  85. * @param aspectClass aspect class for which to check the aspect instance
  86. * @return
  87. * @throws NoAspectBoundException if not bound
  88. */
  89. public static boolean hasAspect(Class aspectClass) throws NoAspectBoundException {
  90. try {
  91. return ((Boolean)getSingletonOrThreadHasAspect(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY)).booleanValue();
  92. } catch (Exception e) {
  93. return false;
  94. }
  95. }
  96. /**
  97. * Returns true if the perthis / pertarget aspect is bound
  98. * @param aspectClass aspect class for which to check the aspect instance
  99. * @param perObject
  100. * @return true if aspect instance exists for the class/object combination
  101. * @throws NoAspectBoundException if not bound
  102. */
  103. public static boolean hasAspect(Class aspectClass, Object perObject) throws NoAspectBoundException {
  104. try {
  105. return ((Boolean)getPerObjectHasAspect(aspectClass).invoke(null, new Object[]{perObject})).booleanValue();
  106. } catch (Exception e) {
  107. return false;
  108. }
  109. }
  110. /**
  111. * Returns true if the pertypewithin aspect is bound
  112. * @param aspectClass aspect class for which to check the aspect instance
  113. * @param perTypeWithin class
  114. * @return true if aspect instance exists for this aspect class/pertypewithin class combination
  115. * @throws NoAspectBoundException if not bound
  116. */
  117. public static boolean hasAspect(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException {
  118. try {
  119. return ((Boolean)getPerTypeWithinHasAspect(aspectClass).invoke(null, new Object[]{perTypeWithin})).booleanValue();
  120. } catch (Exception e) {
  121. return false;
  122. }
  123. }
  124. // -- aspectOf
  125. private static Method getSingletonOrThreadAspectOf(Class aspectClass) throws NoSuchMethodException {
  126. Method method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY);
  127. return checkAspectOf(method, aspectClass);
  128. }
  129. private static Method getPerObjectAspectOf(Class aspectClass) throws NoSuchMethodException {
  130. Method method = aspectClass.getDeclaredMethod(ASPECTOF, PEROBJECT_CLASS_ARRAY);
  131. return checkAspectOf(method, aspectClass);
  132. }
  133. private static Method getPerTypeWithinAspectOf(Class aspectClass) throws NoSuchMethodException {
  134. Method method = aspectClass.getDeclaredMethod(ASPECTOF, PERTYPEWITHIN_CLASS_ARRAY);
  135. return checkAspectOf(method, aspectClass);
  136. }
  137. private static Method checkAspectOf(Method method, Class aspectClass) throws NoSuchMethodException {
  138. method.setAccessible(true);
  139. if (!method.isAccessible()
  140. || !Modifier.isPublic(method.getModifiers())
  141. || !Modifier.isStatic(method.getModifiers())) {
  142. throw new NoSuchMethodException(aspectClass.getName() + ".aspectOf(..) is not accessible public static");
  143. }
  144. return method;
  145. }
  146. // -- hasAspect
  147. private static Method getSingletonOrThreadHasAspect(Class aspectClass) throws NoSuchMethodException {
  148. Method method = aspectClass.getDeclaredMethod(HASASPECT, EMPTY_CLASS_ARRAY);
  149. return checkHasAspect(method, aspectClass);
  150. }
  151. private static Method getPerObjectHasAspect(Class aspectClass) throws NoSuchMethodException {
  152. Method method = aspectClass.getDeclaredMethod(HASASPECT, PEROBJECT_CLASS_ARRAY);
  153. return checkHasAspect(method, aspectClass);
  154. }
  155. private static Method getPerTypeWithinHasAspect(Class aspectClass) throws NoSuchMethodException {
  156. Method method = aspectClass.getDeclaredMethod(HASASPECT, PERTYPEWITHIN_CLASS_ARRAY);
  157. return checkHasAspect(method, aspectClass);
  158. }
  159. private static Method checkHasAspect(Method method, Class aspectClass) throws NoSuchMethodException {
  160. method.setAccessible(true);
  161. if (!method.isAccessible()
  162. || !Modifier.isPublic(method.getModifiers())
  163. || !Modifier.isStatic(method.getModifiers())) {
  164. throw new NoSuchMethodException(aspectClass.getName() + ".hasAspect(..) is not accessible public static");
  165. }
  166. return method;
  167. }
  168. }