1 package org.apache.beehive.controls.api.properties; 2 19 20 import java.lang.annotation.Annotation ; 21 import java.lang.reflect.InvocationHandler ; 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.Proxy ; 24 25 import org.apache.beehive.controls.api.properties.PropertyKey; 26 import org.apache.beehive.controls.api.properties.PropertyMap; 27 import org.apache.beehive.controls.api.properties.PropertySet; 28 29 45 public class PropertySetProxy <T extends Annotation > implements InvocationHandler 46 { 47 55 public static <T extends Annotation > T getProxy(Class <T> propertySet, PropertyMap propertyMap) 56 { 57 assert propertySet != null && propertyMap != null; 58 59 if (!propertySet.isAnnotation()) 60 throw new IllegalArgumentException (propertySet + " is not an annotation type"); 61 62 return (T) Proxy.newProxyInstance(propertySet.getClassLoader(), 63 new Class [] {propertySet }, 64 new PropertySetProxy(propertySet, propertyMap)); 65 } 66 67 70 private PropertySetProxy(Class <T> propertySet, PropertyMap propertyMap) 71 { 72 _propertySet = propertySet; 73 _propertyMap = propertyMap; 74 } 75 76 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 80 { 81 Object value = null; 84 if (method.getDeclaringClass() == Object .class) 85 { 86 try { 87 if (method.getName().equals("getClass")) 88 { 89 value = _propertySet; 90 } 91 else 92 { 93 value = method.invoke(_propertyMap, args); 94 } 95 } 96 catch (Exception e) 97 { 98 e.printStackTrace(); 99 } 100 } 101 else if (method.getDeclaringClass() == Annotation .class && 102 method.getName().equals("annotationType")) 103 { 104 value = _propertySet; 105 } 106 else 107 { 108 109 PropertyKey key = new PropertyKey(_propertySet, method.getName()); 111 value = _propertyMap.getProperty(key); 112 113 if (value instanceof PropertyMap) 116 { 117 PropertyMap propertyMap = (PropertyMap)value; 118 value = getProxy(propertyMap.getMapClass(), propertyMap); 119 } 120 } 121 122 return value; 123 } 124 125 128 public Class <T> getPropertySet() { return _propertySet; } 129 130 134 public PropertyMap getPropertyMap() { return _propertyMap; } 135 136 private Class <T> _propertySet; 137 private PropertyMap _propertyMap; 138 } 139 | Popular Tags |