1 7 package org.jboss.aop.annotation; 8 9 import java.lang.reflect.InvocationHandler ; 10 import java.lang.reflect.Method ; 11 import java.lang.reflect.Proxy ; 12 import java.util.Map ; 13 14 20 public class AnnotationProxy implements InvocationHandler 21 { 22 Map map; 23 24 public AnnotationProxy(Map valueMap) 25 { 26 map = valueMap; 27 } 28 29 public Object invoke(Object proxy, Method method, Object [] args) 30 throws Throwable 31 { 32 if (method.getName().equals("equals")) 33 { 34 return doEquals(proxy, args[0]); 35 } 36 else if (method.getName().equals("hashCode")) 37 { 38 return doHashCode(); 39 } 40 else if (method.getName().equals("toString")) 41 { 42 return map.toString(); 43 } 44 52 return map.get(method.getName()); 53 } 54 55 private Object doEquals(Object proxy, Object obj) 56 { 57 if (obj == proxy) return Boolean.TRUE; 58 if (obj == null) return Boolean.FALSE; 59 60 Class [] intfs = proxy.getClass().getInterfaces(); 61 if (!intfs[0].isAssignableFrom(obj.getClass())) 62 { 63 return Boolean.FALSE; 64 } 65 try 66 { 67 Proxy.getInvocationHandler(obj); 68 } 69 catch (Exception ex) 70 { 71 return Boolean.FALSE; 72 } 73 return Boolean.TRUE; 74 } 75 76 private Object doHashCode() 77 { 78 return new Integer (map.hashCode()); 79 } 80 81 public static Object createProxy(javassist.bytecode.annotation.Annotation info) throws Exception 82 { 83 Class annotation = Thread.currentThread().getContextClassLoader().loadClass(info.getTypeName()); 84 return createProxy(info, annotation); 85 } 86 87 public static Object createProxy(javassist.bytecode.annotation.Annotation info, Class annotation) throws Exception 88 { 89 Map map = ProxyMapCreator.createProxyMap(annotation, info); 90 AnnotationProxy proxyHandler = new AnnotationProxy(map); 91 return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class []{annotation}, proxyHandler); 92 } 93 94 public static Object createProxy(Map map, Class annotation) throws Exception 95 { 96 AnnotationProxy proxyHandler = new AnnotationProxy(map); 97 return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class []{annotation}, proxyHandler); 98 } 99 } 100 | Popular Tags |