1 22 package org.jboss.aop.annotation.factory.duplicate; 23 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Proxy ; 27 import java.util.Map ; 28 29 36 public class AnnotationProxy implements InvocationHandler 37 { 38 Map map; 39 Class annotationType; 40 41 public AnnotationProxy(Class annotationType, Map valueMap) 42 { 43 this.annotationType = annotationType; 44 map = valueMap; 45 } 46 47 public Object invoke(Object proxy, Method method, Object [] args) 48 throws Throwable 49 { 50 if (method.getName().equals("equals")) 51 { 52 return doEquals(proxy, args[0]); 53 } 54 else if (method.getName().equals("hashCode")) 55 { 56 return doHashCode(); 57 } 58 else if (method.getName().equals("toString")) 59 { 60 return map.toString(); 61 } 62 else if (method.getName().equals("annotationType")) 63 { 64 return annotationType; 65 } 66 67 75 return map.get(method.getName()); 76 } 77 78 public Object getValue(String name) 79 { 80 return map.get(name); 81 } 82 83 private Object doEquals(Object proxy, Object obj) 84 { 85 if (obj == proxy) return Boolean.TRUE; 86 if (obj == null) return Boolean.FALSE; 87 88 Class [] intfs = proxy.getClass().getInterfaces(); 89 if (!intfs[0].isAssignableFrom(obj.getClass())) 90 { 91 return Boolean.FALSE; 92 } 93 try 94 { 95 Proxy.getInvocationHandler(obj); 96 } 97 catch (Exception ex) 98 { 99 return Boolean.FALSE; 100 } 101 return Boolean.TRUE; 102 } 103 104 private Object doHashCode() 105 { 106 return new Integer (map.hashCode()); 107 } 108 109 public static Object createProxy(Map map, Class annotation) throws Exception 110 { 111 AnnotationProxy proxyHandler = new AnnotationProxy(annotation, map); 112 return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class []{annotation}, proxyHandler); 113 } 114 } 115 | Popular Tags |