| 1 8 package com.tirsen.nanning; 9 10 import java.lang.reflect.Method ; 11 import java.lang.reflect.Proxy ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Set ; 15 import java.util.ArrayList ; 16 17 25 public class Aspects { 26 private static ThreadLocal contextAspectRepository = new InheritableThreadLocal (); 27 private static ThreadLocal currentThis = new InheritableThreadLocal (); 28 29 35 public static List getInterceptors(Object proxy) { 36 Set interceptors = getAspectInstance(proxy).getAllInterceptors(); 37 return new ArrayList (interceptors); 38 } 39 40 47 public static Interceptor[] getInterceptors(Object proxy, Class interfaceClass) { 48 Set interceptors = getAspectInstance(proxy).getInterceptors(interfaceClass); 49 return (Interceptor[]) interceptors.toArray(new Interceptor[interceptors.size()]); 50 } 51 52 59 public static Object getTarget(Object proxy, Class interfaceClass) { 60 return getAspectInstance(proxy).getTarget(interfaceClass); 61 } 62 63 68 public static AspectInstance getAspectInstance(Object proxy) { 69 AspectInstance aspectInstance = (AspectInstance) Proxy.getInvocationHandler(proxy); 70 assert aspectInstance != null; 71 return aspectInstance; 72 } 73 74 80 public static void setTarget(Object proxy, Class interfaceClass, Object target) { 81 getAspectInstance(proxy).setTarget(interfaceClass, target); 82 } 83 84 89 public static Object getThis() { 90 return currentThis.get(); 91 } 92 93 public static boolean isAspectObject(Object o) { 94 return o == null ? false : Proxy.isProxyClass(o.getClass()); 95 } 96 97 public static Object [] getTargets(Object object) { 98 return object == null ? null : Aspects.getAspectInstance(object).getTargets(); 99 } 100 101 public static Interceptor findFirstInterceptorWithClass(Object proxy, Class interceptorClass) { 102 Set allInterceptors = getAspectInstance(proxy).getAllInterceptors(); 103 for (Iterator iterator = allInterceptors.iterator(); iterator.hasNext();) { 104 Interceptor interceptor = (Interceptor) iterator.next(); 105 if (interceptorClass.isInstance(interceptor)) { 106 return interceptor; 107 } 108 } 109 return null; 110 } 111 112 public static AspectFactory getCurrentAspectFactory() { 113 if (getThis() != null) { 114 return getAspectInstance(getThis()).getAspectFactory(); 115 } else { 116 return (AspectFactory) contextAspectRepository.get(); 117 } 118 } 119 120 public static void setContextAspectFactory(AspectFactory factory) { 121 contextAspectRepository.set(factory); 122 } 123 124 public static MethodInterceptor[] getInterceptors(Object proxy, Method method) { 125 List interceptors = getAspectInstance(proxy).getInterceptorsForMethod(method); 126 return (MethodInterceptor[]) interceptors.toArray(new MethodInterceptor[interceptors.size()]); 127 } 128 129 135 public static Class getRealClass(Class proxyClass) { 136 if (!Proxy.isProxyClass(proxyClass)) { 137 return proxyClass; 138 } 139 Class [] interfaces = proxyClass.getInterfaces(); 140 for (int i = 0; i < interfaces.length; i++) { 141 Class anInterface = interfaces[i]; 142 Class realClass = getRealClass(anInterface); 143 if (realClass != null) { 144 return realClass; 145 } 146 } 147 return null; 148 } 149 150 static void setThis(Object proxy) { 151 Aspects.currentThis.set(proxy); 152 } 153 } 154 | Popular Tags |