1 package org.apache.beehive.controls.api.properties; 2 19 20 import java.lang.annotation.Annotation ; 21 import java.lang.reflect.Method ; 22 import java.util.HashMap ; 23 24 import org.apache.beehive.controls.api.ControlException; 25 26 30 public class PropertyKey implements java.io.Serializable 31 { 32 36 public PropertyKey(Class <? extends Annotation > propertySet, String propertyName) 37 { 38 if (!propertySet.isAnnotation()) 39 { 40 throw new IllegalArgumentException ("Class " + propertySet + 41 " is not a valid annotation type"); 42 } 43 44 try 45 { 46 _getMethod = propertySet.getMethod(propertyName, (Class [])null); 47 _propertySet = propertySet; 48 _propertyName = propertyName; 49 _propertyType = _getMethod.getReturnType(); 50 51 _hashCode = new String (propertySet.getName() + "." + propertyName).hashCode(); 56 } 57 catch (NoSuchMethodException nsme) 58 { 59 throw new IllegalArgumentException (propertyName + 60 "is not a valid member of the metadata interface " + propertySet); 61 } 62 } 63 64 protected Method getMethod() 65 { 66 if (null == _getMethod) 67 { 68 try 69 { 70 _getMethod = _propertySet.getMethod(_propertyName, (Class [])null); 71 } 72 catch(NoSuchMethodException nsmEx) 73 { 74 throw new ControlException("Unable to locate PropertyKey accessor method", nsmEx); 78 } 79 } 80 return _getMethod; 81 } 82 83 87 public Object getDefaultValue() 88 { 89 return getMethod().getDefaultValue(); 92 } 93 94 97 Object extractValue(Annotation annot) 98 { 99 try 100 { 101 return getMethod().invoke(annot, new Object [] {}); 102 } 103 catch (RuntimeException re) { throw re; } 105 catch (Exception e) 106 { 107 throw new RuntimeException ("Unable to extract value for " + _propertyName, e); 108 } 109 } 110 111 public boolean equals(Object obj) 112 { 113 if (this == obj) 115 return true; 116 117 if (obj == null || !(obj instanceof PropertyKey) || _hashCode != obj.hashCode()) 119 return false; 120 121 PropertyKey keyObj = (PropertyKey)obj; 123 return _propertySet.equals(keyObj._propertySet) && 124 _propertyName.equals(keyObj._propertyName); 125 } 126 127 public int hashCode() { return _hashCode; } 128 129 public String toString() 130 { 131 return "PropertyKey: " + _propertySet.getName() + "." + _propertyName; 132 } 133 134 public Class <? extends Annotation > getPropertySet() { return _propertySet; } 135 public String getPropertyName() { return _propertyName; } 136 public Class getPropertyType() { return _propertyType; } 137 public Annotation [] getAnnotations() { return getMethod().getAnnotations();} 138 139 Class <? extends Annotation > _propertySet; 140 String _propertyName; 141 Class _propertyType; 142 int _hashCode; 143 144 private transient Method _getMethod; 147 } 148 | Popular Tags |