1 12 package org.aspectj.lang; 13 14 15 import java.lang.reflect.Method ; 16 import java.lang.reflect.Modifier ; 17 import java.lang.reflect.InvocationTargetException ; 18 19 27 public class Aspects14 { 28 29 private final static Class [] EMPTY_CLASS_ARRAY = new Class [0]; 30 private final static Class [] PEROBJECT_CLASS_ARRAY = new Class []{Object .class}; 31 private final static Class [] PERTYPEWITHIN_CLASS_ARRAY = new Class []{Class .class}; 32 private final static Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 33 private final static String ASPECTOF = "aspectOf"; 34 private final static String HASASPECT = "hasAspect"; 35 36 43 public static Object aspectOf(Class aspectClass) throws NoAspectBoundException { 44 try { 45 return getSingletonOrThreadAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY); 46 } catch (InvocationTargetException e) { 47 throw new NoAspectBoundException(aspectClass.getName(), e); } catch (Exception e) { 50 throw new NoAspectBoundException(aspectClass.getName(), e); 51 } 52 } 53 54 61 public static Object aspectOf(Class aspectClass, Object perObject) throws NoAspectBoundException { 62 try { 63 return getPerObjectAspectOf(aspectClass).invoke(null, new Object []{perObject}); 64 } catch (InvocationTargetException e) { 65 throw new NoAspectBoundException(aspectClass.getName(), e); } catch (Exception e) { 68 throw new NoAspectBoundException(aspectClass.getName(), e); 69 } 70 } 71 72 79 public static Object aspectOf(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException { 80 try { 81 return getPerTypeWithinAspectOf(aspectClass).invoke(null, new Object []{perTypeWithin}); 82 } catch (InvocationTargetException e) { 83 throw new NoAspectBoundException(aspectClass.getName(), e); } catch (Exception e) { 86 throw new NoAspectBoundException(aspectClass.getName(), e); 87 } 88 } 89 90 97 public static boolean hasAspect(Class aspectClass) throws NoAspectBoundException { 98 try { 99 return ((Boolean )getSingletonOrThreadHasAspect(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY)).booleanValue(); 100 } catch (Exception e) { 101 return false; 102 } 103 } 104 105 112 public static boolean hasAspect(Class aspectClass, Object perObject) throws NoAspectBoundException { 113 try { 114 return ((Boolean )getPerObjectHasAspect(aspectClass).invoke(null, new Object []{perObject})).booleanValue(); 115 } catch (Exception e) { 116 return false; 117 } 118 } 119 120 127 public static boolean hasAspect(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException { 128 try { 129 return ((Boolean )getPerTypeWithinHasAspect(aspectClass).invoke(null, new Object []{perTypeWithin})).booleanValue(); 130 } catch (Exception e) { 131 return false; 132 } 133 } 134 135 137 private static Method getSingletonOrThreadAspectOf(Class aspectClass) throws NoSuchMethodException { 138 Method method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY); 139 return checkAspectOf(method, aspectClass); 140 } 141 142 private static Method getPerObjectAspectOf(Class aspectClass) throws NoSuchMethodException { 143 Method method = aspectClass.getDeclaredMethod(ASPECTOF, PEROBJECT_CLASS_ARRAY); 144 return checkAspectOf(method, aspectClass); 145 } 146 147 private static Method getPerTypeWithinAspectOf(Class aspectClass) throws NoSuchMethodException { 148 Method method = aspectClass.getDeclaredMethod(ASPECTOF, PERTYPEWITHIN_CLASS_ARRAY); 149 return checkAspectOf(method, aspectClass); 150 } 151 152 private static Method checkAspectOf(Method method, Class aspectClass) throws NoSuchMethodException { 153 method.setAccessible(true); 154 if (!method.isAccessible() 155 || !Modifier.isPublic(method.getModifiers()) 156 || !Modifier.isStatic(method.getModifiers())) { 157 throw new NoSuchMethodException (aspectClass.getName() + ".aspectOf(..) is not accessible public static"); 158 } 159 return method; 160 } 161 162 164 private static Method getSingletonOrThreadHasAspect(Class aspectClass) throws NoSuchMethodException { 165 Method method = aspectClass.getDeclaredMethod(HASASPECT, EMPTY_CLASS_ARRAY); 166 return checkHasAspect(method, aspectClass); 167 } 168 169 private static Method getPerObjectHasAspect(Class aspectClass) throws NoSuchMethodException { 170 Method method = aspectClass.getDeclaredMethod(HASASPECT, PEROBJECT_CLASS_ARRAY); 171 return checkHasAspect(method, aspectClass); 172 } 173 174 private static Method getPerTypeWithinHasAspect(Class aspectClass) throws NoSuchMethodException { 175 Method method = aspectClass.getDeclaredMethod(HASASPECT, PERTYPEWITHIN_CLASS_ARRAY); 176 return checkHasAspect(method, aspectClass); 177 } 178 179 private static Method checkHasAspect(Method method, Class aspectClass) throws NoSuchMethodException { 180 method.setAccessible(true); 181 if (!method.isAccessible() 182 || !Modifier.isPublic(method.getModifiers()) 183 || !Modifier.isStatic(method.getModifiers())) { 184 throw new NoSuchMethodException (aspectClass.getName() + ".hasAspect(..) is not accessible public static"); 185 } 186 return method; 187 } 188 } 189 | Popular Tags |