1 22 package org.jboss.annotation.factory; 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 @SuppressWarnings ("unchecked") 84 private Object doEquals(Object proxy, Object obj) 85 { 86 if (obj == proxy) return Boolean.TRUE; 87 if (obj == null) return Boolean.FALSE; 88 89 Class [] intfs = proxy.getClass().getInterfaces(); 90 if (!intfs[0].isAssignableFrom(obj.getClass())) 91 { 92 return Boolean.FALSE; 93 } 94 try 95 { 96 Proxy.getInvocationHandler(obj); 97 } 98 catch (Exception ex) 99 { 100 return Boolean.FALSE; 101 } 102 return Boolean.TRUE; 103 } 104 105 private Object doHashCode() 106 { 107 return new Integer (map.hashCode()); 108 } 109 110 public static Object createProxy(Map map, Class annotation) throws Exception 111 { 112 AnnotationProxy proxyHandler = new AnnotationProxy(annotation, map); 113 return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class []{annotation}, proxyHandler); 114 } 115 } 116 | Popular Tags |