1 13 package org.aspectj.lang; 14 15 16 import java.lang.reflect.Method ; 17 import java.lang.reflect.Modifier ; 18 import java.lang.reflect.InvocationTargetException ; 19 20 29 public class Aspects { 30 31 private final static Class [] EMPTY_CLASS_ARRAY = new Class [0]; 32 private final static Class [] PEROBJECT_CLASS_ARRAY = new Class []{Object .class}; 33 private final static Class [] PERTYPEWITHIN_CLASS_ARRAY = new Class []{Class .class}; 34 private final static Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 35 private final static String ASPECTOF = "aspectOf"; 36 private final static String HASASPECT = "hasAspect"; 37 38 45 public static <T> T aspectOf(Class <T> aspectClass) throws NoAspectBoundException { 46 try { 47 return (T) getSingletonOrThreadAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY); 48 } catch (InvocationTargetException e) { 49 throw new NoAspectBoundException(aspectClass.getName(), e); } catch (Exception e) { 52 throw new NoAspectBoundException(aspectClass.getName(), e); 53 } 54 } 55 56 63 public static <T> T aspectOf(Class <T> aspectClass, Object perObject) throws NoAspectBoundException { 64 try { 65 return (T) getPerObjectAspectOf(aspectClass).invoke(null, new Object []{perObject}); 66 } catch (InvocationTargetException e) { 67 throw new NoAspectBoundException(aspectClass.getName(), e); } catch (Exception e) { 70 throw new NoAspectBoundException(aspectClass.getName(), e); 71 } 72 } 73 74 81 public static <T> T aspectOf(Class <T> aspectClass, Class <?> perTypeWithin) throws NoAspectBoundException { 82 try { 83 return (T) getPerTypeWithinAspectOf(aspectClass).invoke(null, new Object []{perTypeWithin}); 84 } catch (InvocationTargetException e) { 85 throw new NoAspectBoundException(aspectClass.getName(), e); } catch (Exception e) { 88 throw new NoAspectBoundException(aspectClass.getName(), e); 89 } 90 } 91 92 99 public static boolean hasAspect(Class <?> aspectClass) throws NoAspectBoundException { 100 try { 101 return ((Boolean )getSingletonOrThreadHasAspect(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY)).booleanValue(); 102 } catch (Exception e) { 103 return false; 104 } 105 } 106 107 114 public static boolean hasAspect(Class <?> aspectClass, Object perObject) throws NoAspectBoundException { 115 try { 116 return ((Boolean )getPerObjectHasAspect(aspectClass).invoke(null, new Object []{perObject})).booleanValue(); 117 } catch (Exception e) { 118 return false; 119 } 120 } 121 122 129 public static boolean hasAspect(Class <?> aspectClass, Class <?> perTypeWithin) throws NoAspectBoundException { 130 try { 131 return ((Boolean )getPerTypeWithinHasAspect(aspectClass).invoke(null, new Object []{perTypeWithin})).booleanValue(); 132 } catch (Exception e) { 133 return false; 134 } 135 } 136 137 139 private static Method getSingletonOrThreadAspectOf(Class <?> aspectClass) throws NoSuchMethodException { 140 Method method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY); 141 return checkAspectOf(method, aspectClass); 142 } 143 144 private static Method getPerObjectAspectOf(Class <?> aspectClass) throws NoSuchMethodException { 145 Method method = aspectClass.getDeclaredMethod(ASPECTOF, PEROBJECT_CLASS_ARRAY); 146 return checkAspectOf(method, aspectClass); 147 } 148 149 private static Method getPerTypeWithinAspectOf(Class <?> aspectClass) throws NoSuchMethodException { 150 Method method = aspectClass.getDeclaredMethod(ASPECTOF, PERTYPEWITHIN_CLASS_ARRAY); 151 return checkAspectOf(method, aspectClass); 152 } 153 154 private static Method checkAspectOf(Method method, Class <?> aspectClass) throws NoSuchMethodException { 155 method.setAccessible(true); 156 if (!method.isAccessible() 157 || !Modifier.isPublic(method.getModifiers()) 158 || !Modifier.isStatic(method.getModifiers())) { 159 throw new NoSuchMethodException (aspectClass.getName() + ".aspectOf(..) is not accessible public static"); 160 } 161 return method; 162 } 163 164 166 private static Method getSingletonOrThreadHasAspect(Class aspectClass) throws NoSuchMethodException { 167 Method method = aspectClass.getDeclaredMethod(HASASPECT, EMPTY_CLASS_ARRAY); 168 return checkHasAspect(method, aspectClass); 169 } 170 171 private static Method getPerObjectHasAspect(Class aspectClass) throws NoSuchMethodException { 172 Method method = aspectClass.getDeclaredMethod(HASASPECT, PEROBJECT_CLASS_ARRAY); 173 return checkHasAspect(method, aspectClass); 174 } 175 176 private static Method getPerTypeWithinHasAspect(Class aspectClass) throws NoSuchMethodException { 177 Method method = aspectClass.getDeclaredMethod(HASASPECT, PERTYPEWITHIN_CLASS_ARRAY); 178 return checkHasAspect(method, aspectClass); 179 } 180 181 private static Method checkHasAspect(Method method, Class aspectClass) throws NoSuchMethodException { 182 method.setAccessible(true); 183 if (!method.isAccessible() 184 || !Modifier.isPublic(method.getModifiers()) 185 || !Modifier.isStatic(method.getModifiers())) { 186 throw new NoSuchMethodException (aspectClass.getName() + ".hasAspect(..) is not accessible public static"); 187 } 188 return method; 189 } 190 } 191 | Popular Tags |