1 55 56 package org.apache.bsf.engines.activescript; 57 58 import java.util.Vector ; 59 import java.lang.reflect.*; 60 import java.beans.*; 61 62 import org.apache.bsf.*; 63 import org.apache.bsf.util.*; 64 65 public final class JavaBean { 66 67 68 private static Vector lsMembers = new Vector (512, 512); 69 static 70 { 71 lsMembers.addElement(""); } 73 74 private static final short DISPATCH_METHOD= 0x01; 75 private static final short DISPATCH_PROPERTYGET = 0x02; 76 private static final short DISPATCH_PROPERTYPUT= 0x04; 77 private static final short DISPATCH_PROPERTYPUTREF= 0x08; 78 private static final short DISPATCH_CASESENSITIVE= 0x40; 79 private static final short DISPATCH_THROWEXCEPTION= 0x80; 80 81 82 87 88 public final static int bindMember(Class jclass, String name) throws Exception { 89 return bindMember( jclass, name, (short)( DISPATCH_PROPERTYGET | DISPATCH_PROPERTYPUT | DISPATCH_METHOD )); 90 } 91 95 96 public final static int bindMember(Class jclass, String name, short bindType) throws Exception { 97 106 Object match = null; 107 108 boolean throwException = 0 != (bindType & DISPATCH_THROWEXCEPTION); 109 boolean respectCase = 0 != (bindType & DISPATCH_CASESENSITIVE); 110 boolean dispatchMethod= (0!=(bindType & DISPATCH_METHOD)); 111 boolean dispatchPropertyGet= (0!=(bindType & DISPATCH_PROPERTYGET)); 112 boolean dispatchPropertyPut= (0!=(bindType & DISPATCH_PROPERTYPUT)) ||(0!=(bindType & DISPATCH_PROPERTYPUTREF)); 113 114 if(!( dispatchMethod || dispatchPropertyGet || dispatchPropertyPut )) dispatchMethod= dispatchPropertyGet= dispatchPropertyPut= true; 115 116 117 Method jmethods[] = jclass.getMethods(); 118 119 for (int i=0; i<jmethods.length; i++) { 120 121 if (respectCase ?jmethods[i].getName().equals(name):jmethods[i].getName().equalsIgnoreCase(name) ) { 122 123 if (match == null) { 124 match = jmethods[i]; 126 127 } else if (match instanceof Method) { 128 129 if (jmethods[i].getName().equals(((Method)match).getName())) { 130 match = jmethods[i].getName(); 132 if(respectCase) i= jmethods.length; } else { 134 if(!respectCase) 135 { 136 if(throwException) 138 throw new BSFException (BSFException.REASON_OTHER_ERROR, 139 "Method:" + name + " in " + jclass + "differs between two methods in case only, can't distiguish"); 140 return 0; 141 } 142 } 143 144 } else if (!jmethods[i].getName().equals((String )match)) { 145 if(!respectCase) 146 { 147 if(throwException) 149 throw new BSFException (BSFException.REASON_OTHER_ERROR, 150 "Method:" + name + " in " + jclass + "differs between two methods in case only, can't distiguish"); 151 return 0; 152 } 153 } 154 155 } 156 157 } 158 159 if (match != null) { 161 lsMembers.addElement(match); 162 return lsMembers.size()-1; 163 } 164 165 if(!(dispatchPropertyPut || dispatchPropertyGet)) 166 { 167 if(throwException) 168 throw new BSFException (BSFException.REASON_OTHER_ERROR, 169 "Method:" + name + " in " + jclass + "not found."); 170 171 return 0; } 173 174 175 176 BeanInfo beanInfo=null ; 177 if(dispatchPropertyPut || dispatchPropertyGet) 178 { 179 Field jfields[] = jclass.getFields(); 183 for (int i=0; i<jfields.length; i++) { 184 if (respectCase ? jfields[i].getName().equals( name) : jfields[i].getName().equalsIgnoreCase( name) ) { 185 if (match != null) 186 { 187 if(throwException) 188 throw new BSFException (BSFException.REASON_OTHER_ERROR, 189 "Field:" + name + " in " + jclass + "differs between two fields in case only, can't distinguish"); 190 return 0; 191 } 192 match = jfields[i]; 193 if(respectCase) i= jfields.length; } 195 } 196 197 if (match != null) { 199 lsMembers.addElement(match); 200 return -(lsMembers.size()-1); 201 } 202 203 beanInfo = Introspector.getBeanInfo(jclass); 205 PropertyDescriptor properties[] = beanInfo.getPropertyDescriptors(); 206 for (int i=0; i<properties.length; i++) { 207 if (respectCase? properties[i].getName().equals(name) : properties[i].getName().equalsIgnoreCase(name)) { 208 if (match != null) 209 { 210 if(throwException) 211 throw new BSFException (BSFException.REASON_OTHER_ERROR, 212 "Field:" + name + " in " + jclass + "differs between two fields in case only, can't distinguish"); 213 return 0; 214 } 215 match = properties[i]; 216 if(respectCase) i= properties.length; } 218 } 219 220 if (match != null) { 222 lsMembers.addElement(match); 223 return -(lsMembers.size()-1); 224 } 225 } 227 if(dispatchPropertyPut) 228 { 229 if (name.length() > 2 && (respectCase ? name.substring(0,2).equals("on"):name.substring(0,2).equalsIgnoreCase("on"))) { 231 String eventName = name.substring(2); 232 EventSetDescriptor events[] = beanInfo.getEventSetDescriptors(); 233 for (int i=0; i<events.length; i++) { 234 if (respectCase ? events[i].getName().equals(eventName): events[i].getName().equalsIgnoreCase(eventName)) { 235 if (match != null) 236 { 237 if(throwException) 238 throw new BSFException (BSFException.REASON_OTHER_ERROR, 239 "Event:" + name + " in " + jclass + "differs between two fields in case only, can't distinguish"); 240 241 return 0; 242 } 243 match = events[i]; 244 if(respectCase) i= events.length; 245 } 246 } 247 248 if (match != null) { 250 lsMembers.addElement(match); 251 return -(lsMembers.size()-1); 252 } 253 } 254 } 255 256 257 if(throwException) 258 throw new BSFException (BSFException.REASON_OTHER_ERROR, 259 "No method, property, or event matches " + name + " in " + jclass + "."); 260 261 return 0; 262 } 263 269 public final static Object callMethod( 270 JavaBeanAddEventListener engine, 271 Object bean, 272 int methodID, 273 Object [] args) throws Exception 274 { 275 Object result = null; 276 277 if (methodID > 0) { 279 Object member = lsMembers.elementAt(methodID); 280 if (member instanceof Method) { 281 282 try 283 { 284 result=((Method)member).invoke(bean, args); 285 } 286 catch(java.lang.reflect.InvocationTargetException e) 287 { 288 java.lang.Throwable original=e.getTargetException(); 289 290 java.io.StringWriter sw= new java.io.StringWriter (); 291 java.io.PrintWriter pw= new java.io.PrintWriter (sw); 292 original.printStackTrace(pw); 293 throw new BSFException (BSFException.REASON_OTHER_ERROR, 294 "Target method exception(" + original + ") message is: " +original.getMessage() + "stack trace" + sw.toString(), original ); 295 } 296 } else { 297 result=EngineUtils.callBeanMethod(bean, (String )member, args); 298 } 299 } else { 300 Object member = lsMembers.elementAt(-methodID); 301 if (member instanceof PropertyDescriptor) { 302 PropertyDescriptor property = (PropertyDescriptor)member; 303 if (args.length>0) 304 { 305 Method method= property.getWriteMethod(); 306 307 if(null== method) throw new BSFException (BSFException.REASON_OTHER_ERROR, 308 "Property " + property.getName() + " in " + bean.getClass() + " is read only."); 309 result = method.invoke(bean, args); 310 } 311 else 312 { 313 Method method= property.getReadMethod(); 314 315 if(null== method) throw new BSFException (BSFException.REASON_OTHER_ERROR, 316 "Property " + property.getName() + " in " + bean.getClass() + " is write only."); 317 318 result = method.invoke(bean, args); 319 } 320 } else if (member instanceof Field) { 321 Field field = (Field)member; 322 if (args.length>0) 323 field.set(bean, args[0]); 324 else 325 result = field.get(bean); 326 } else { 327 if (args.length>0) { 328 engine.addEventListener(bean, 329 ((EventSetDescriptor)member).getName(), null, 330 args[0].toString()); 331 } 332 } 333 } 334 335 return result; 336 } 337 } 338 | Popular Tags |