1 7 package org.jboss.cache.aop; 8 9 import org.jboss.aop.*; 10 import org.jboss.aop.advice.Interceptor; 11 import org.jboss.aop.joinpoint.MethodInvocation; 12 import org.jboss.aop.joinpoint.Invocation; 13 import org.jboss.aop.proxy.ClassProxy; 14 import org.jboss.aop.proxy.ClassProxyFactory; 15 import org.jboss.logging.Logger; 16 import org.jboss.aop.util.MethodHashing; 17 import org.jboss.util.NestedRuntimeException; 18 19 import java.lang.reflect.Method ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.List ; 24 25 32 public class CollectionInterceptorUtil 33 { 34 static Logger logger_ = Logger.getLogger(CollectionInterceptorUtil.class.getName()); 35 36 public static ClassProxy createProxy(Class clazz, Interceptor interceptor) 37 throws Exception 38 { 39 ClassProxy result = ClassProxyFactory.newInstance(clazz); 40 InstanceAdvisor advisor = result._getInstanceAdvisor(); 41 advisor.appendInterceptor(interceptor); 42 return result; 43 } 44 45 public static Map getMethodMap(Class clazz) 46 { 47 Map result = ClassProxyFactory.getMethodMap(clazz.getName()); 48 if (result == null) { 49 try { 50 ClassProxyFactory.newInstance(clazz); 51 } catch (Exception e) { 52 throw new NestedRuntimeException(e); 53 } 54 result = ClassProxyFactory.getMethodMap(clazz.getName()); 55 } 56 return result; 57 } 58 59 public static Map getManagedMethods(Class clazz) 60 { 61 Method tostring = null; 62 try { 63 tostring = Object .class.getDeclaredMethod("toString", new Class [0]); 64 } catch (NoSuchMethodException e) { 65 e.printStackTrace(); 66 throw new NestedRuntimeException("getManagedMathods: " +e); 67 } 68 69 Map managedMethods = new HashMap (); 70 try { 71 Method [] methods = clazz.getDeclaredMethods(); 72 for (int i = 0; i < methods.length; i++) { 73 long hash = MethodHashing.methodHash(methods[i]); 74 managedMethods.put(new Long (hash), methods[i]); 75 } 76 long hash = MethodHashing.methodHash(tostring); 78 managedMethods.put(new Long (hash), tostring); 79 } catch (Exception ignored) { 80 ignored.printStackTrace(); 81 } 82 return managedMethods; 83 } 84 85 public static Object invoke(Invocation invocation, 86 Object interceptor, 87 Map methodMap, 88 Map managedMethods) 89 throws Throwable 90 { 91 92 if (invocation instanceof MethodInvocation) { 93 MethodInvocation methodInvocation = (MethodInvocation) invocation; 94 Long methodHash = new Long (methodInvocation.getMethodHash()); 95 Method method = (Method ) managedMethods.get(methodHash); 96 if (logger_.isDebugEnabled() && method != null) { 97 logger_.trace("invoke(): method intercepted " + method.getName()); 98 } 99 Object [] args = methodInvocation.getArguments(); 100 if (method != null) { 101 return method.invoke(interceptor, args); 102 } else { 103 method = methodInvocation.getMethod(); 104 if (method == null) { 105 method = (Method ) methodMap.get(methodHash); 106 } 107 108 logger_.trace("invke(): invoke non-managed method: " +method.toString()); 109 Object target = methodInvocation.getTargetObject(); 110 if(target == null) { 111 throw new NestedRuntimeException("CollectionInterceptorUtil.invoke(): targetObject is null." + 112 " Can't invoke " +method.toString()); 113 } 114 return method.invoke(target, args); 115 } 117 } 118 return invocation.invokeNext(); 119 } 120 121 } 122 | Popular Tags |