1 19 20 package org.netbeans.modules.schema2beans; 21 22 import java.lang.reflect.*; 23 24 27 public class ReflectiveBeanProp extends BeanProp { 28 private Method writer; 29 private Method arrayWriter; 30 private Method reader; 31 private Method arrayReader; 32 private Method adder; 33 private Method remover; 34 35 public ReflectiveBeanProp(BaseBean bean, String dtdName, String beanName, 36 int type, Class propClass, 37 Method writer, Method arrayWriter, Method reader, Method arrayReader, Method adder, Method remover) { 38 super(bean, dtdName, beanName, type, propClass, -1); 39 bindings = null; 40 attributes = null; 41 this.writer = writer; 42 this.arrayWriter = arrayWriter; 43 this.reader = reader; 44 this.arrayReader = arrayReader; 45 this.adder = adder; 46 this.remover = remover; 47 } 48 49 public ReflectiveBeanProp(BaseBean bean, String dtdName, String beanName, 50 int type, Class propClass, boolean isRoot, 51 Method writer, Method arrayWriter, Method reader, Method arrayReader, Method adder, Method remover) { 52 super(bean, dtdName, beanName, type, propClass, isRoot, -1); 53 bindings = null; 54 attributes = null; 55 this.writer = writer; 56 this.arrayWriter = arrayWriter; 57 this.reader = reader; 58 this.arrayReader = arrayReader; 59 this.adder = adder; 60 this.remover = remover; 61 } 62 63 DOMBinding getBinding(int index) { 64 throw new IllegalStateException (); 65 } 66 67 protected int bindingsSize() { 68 Object [] arr = getObjectArray(0); 69 if (arr == null) 70 return 0; 71 return arr.length; 72 } 73 74 public Object getValue(int index) { 75 try { 76 if (isIndexed()) 77 return reader.invoke(bean, new Object [] {new Integer (index)}); 78 else 79 return reader.invoke(bean, new Object [] {}); 80 } catch (java.lang.IllegalAccessException e) { 81 throw new RuntimeException (e); 82 } catch (java.lang.reflect.InvocationTargetException e) { 83 throw new RuntimeException (e); 84 } 85 } 86 87 public Object getValueById(int id) { 88 return getObjectArray(0)[idToIndex(id)]; 89 } 90 91 public int indexToId(int index) { 92 return index; 93 } 94 95 public int idToIndex(int id) { 96 return id; 97 } 98 99 protected Object [] getObjectArray(int extraElements) { 100 if (arrayReader == null) 101 return new Object [] {}; 102 try { 103 return (Object []) arrayReader.invoke(bean, new Object [] {}); 104 } catch (java.lang.IllegalAccessException e) { 105 throw new RuntimeException (e); 106 } catch (java.lang.reflect.InvocationTargetException e) { 107 throw new RuntimeException (e); 108 } 109 } 110 111 public void setValue(Object [] value) { 112 try { 113 arrayWriter.invoke(bean, new Object [] {value}); 114 } catch (java.lang.IllegalAccessException e) { 115 throw new RuntimeException (e); 116 } catch (java.lang.reflect.InvocationTargetException e) { 117 throw new RuntimeException (e); 118 } 119 } 120 121 public int removeValue(Object value) { 122 try { 123 return ((Integer ) remover.invoke(bean, new Object [] {value})).intValue(); 124 } catch (java.lang.IllegalAccessException e) { 125 throw new RuntimeException (e); 126 } catch (java.lang.reflect.InvocationTargetException e) { 127 throw new RuntimeException (e); 128 } 129 } 130 131 public void removeValue(int index) { 132 Object [] arr = getObjectArray(0); 133 if (arr == null) 134 return; 135 Object [] newArr = (Object []) Array.newInstance(arr.getClass().getComponentType(), 136 arr.length-1); 137 System.arraycopy(arr, 0, newArr, 0, index); 138 System.arraycopy(arr, index+1, newArr, index, arr.length - index - 1); 139 setValue(newArr); 140 } 141 142 protected int setElement(int index, Object value, boolean add) { 143 try { 144 if (add) 145 index = ((Integer ) adder.invoke(bean, new Object [] {value})).intValue(); 146 else if (isIndexed()) 147 writer.invoke(bean, new Object [] {new Integer (index), value}); 148 else 149 writer.invoke(bean, new Object [] {value}); 150 return index; 151 } catch (java.lang.IllegalAccessException e) { 152 throw new RuntimeException ("setElement: "+getBeanName(), e); 153 } catch (java.lang.reflect.InvocationTargetException e) { 154 throw new RuntimeException ("setElement: "+getBeanName(), e); 155 } 156 } 157 158 public void setAttributeValue(int index, String name, String value) { 159 throw new IllegalStateException (); 160 } 161 162 public String getAttributeValue(int index, String name) { 163 throw new IllegalStateException (); 164 } 165 166 public DOMBinding registerDomNode(org.w3c.dom.Node node, DOMBinding binding, 167 BaseBean bean) throws Schema2BeansException { 168 throw new IllegalStateException (); 169 } 170 171 public String getFullName() { 172 return getFullName(0); 173 } 174 175 public String getFullName(int index) { 176 if (reader == null) 177 return bean.nameSelf(); 178 else 179 return bean.nameSelf()+"/"+bean.nameChild(getValue(index), false, false); 180 } 181 182 void buildPathName(DOMBinding binding, StringBuffer str) { 183 throw new IllegalStateException (); 184 } 185 186 String buildFullName(int index, String attr) { 187 throw new IllegalStateException (); 188 } 189 } 190 | Popular Tags |