1 package jfun.yan.xml.nut; 2 3 import java.lang.reflect.Method ; 4 import java.util.HashMap ; 5 import java.util.Map ; 6 7 import jfun.util.Misc; 8 import jfun.yan.factory.Factory; 9 10 11 17 public class NutDescriptor implements java.io.Serializable { 18 static final class CollectionDescriptor implements java.io.Serializable { 19 final Method setter; 20 final Class elem_type; 21 22 CollectionDescriptor(Class elem_type, Method setter) { 23 this.elem_type = elem_type; 24 this.setter = setter; 25 } 26 public String toString(){ 27 return Misc.getTypeName(elem_type)+"[]"; 28 } 29 } 30 static final class RegularDescriptor implements java.io.Serializable { 31 final Map sub_descriptors; 32 33 RegularDescriptor(Map sub_descriptors) { 34 this.sub_descriptors = sub_descriptors; 35 } 36 public String toString(){ 37 return sub_descriptors.toString(); 38 } 39 } 40 private final Class type; 41 private Map props; 42 private boolean collection = false; 43 private CollectionDescriptor cdesc; 44 private RegularDescriptor rdesc; 45 private Evaluator evaluator; 46 private final Map adders = new HashMap (); 47 private final Map addertypes = new HashMap (); 48 private MethodSuite anonymous_adder_suite; 49 void setAdderSuite(MethodSuite suite){ 50 this.anonymous_adder_suite = suite; 51 } 52 void putAdder(String name, Method mtd){ 53 adders.put(name, mtd); 54 addertypes.put(name, mtd.getParameterTypes()[0]); 55 } 56 61 public Method getAdder(String name){ 62 return (Method )adders.get(name); 63 } 64 65 72 public Method getAnonymousAdder(Object arg){ 73 return anonymous_adder_suite.getMethod(arg); 74 } 75 80 public Class getAdderType(String name){ 81 return (Class )addertypes.get(name); 82 } 83 public String toString(){ 84 return Misc.getTypeName(type); 85 } 86 NutDescriptor(Class type) { 87 this.type = type; 88 } 89 90 CollectionDescriptor getCollectionDescriptor() { 91 return cdesc; 92 } 93 100 public boolean isCollectionNut() { 101 return collection; 102 } 103 106 public Evaluator getEvaluator() { 107 return evaluator; 108 } 109 110 113 public Class getType() { 114 return type; 115 } 116 119 public Map getPropertyDescriptors(){ 120 return props; 121 } 122 127 public NutDescriptor getSubDescriptor(String name){ 128 return (NutDescriptor)rdesc.sub_descriptors.get(name); 129 } 130 135 public Method getSetter(){ 136 return cdesc.setter; 137 } 138 145 public Class getSetterElementType(){ 146 return cdesc.elem_type; 147 } 148 void setCollectionDescriptor(CollectionDescriptor cdesc) { 149 this.cdesc = cdesc; 150 this.collection = true; 151 } 152 153 void setEvaluator(Evaluator evaluator) { 154 this.evaluator = evaluator; 155 } 156 void setPropertyDescriptors(Map props) { 157 this.props = props; 158 } 159 void setRegularDescriptor(RegularDescriptor rdesc) { 160 this.rdesc = rdesc; 161 this.collection = false; 162 } 163 public boolean equals(Object obj) { 164 if(obj instanceof NutDescriptor){ 165 final NutDescriptor other = (NutDescriptor)obj; 166 return type.equals(other.type); 167 } 168 else return false; 169 } 170 public int hashCode() { 171 return type.hashCode(); 172 } 173 private Factory factory = null; 174 180 public synchronized Object createNut() 181 throws IllegalAccessException , InstantiationException { 182 if(factory == null){ 183 return type.newInstance(); 184 } 185 else{ 186 return factory.create(); 187 } 188 } 189 196 public synchronized void setFactory(Factory factory){ 197 this.factory = factory; 198 } 199 } 200 | Popular Tags |