1 55 package org.jboss.axis.utils; 56 57 import org.jboss.axis.AxisFault; 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.InternalException; 60 import org.jboss.axis.description.FieldDesc; 61 import org.jboss.axis.description.TypeDesc; 62 import org.jboss.logging.Logger; 63 64 import java.beans.Introspector ; 65 import java.beans.PropertyDescriptor ; 66 import java.lang.reflect.Field ; 67 import java.lang.reflect.Method ; 68 import java.lang.reflect.Modifier ; 69 import java.security.AccessController ; 70 import java.security.PrivilegedAction ; 71 import java.util.ArrayList ; 72 import java.util.Arrays ; 73 import java.util.List ; 74 import java.util.Vector ; 75 76 public class BeanUtils 77 { 78 79 public static final Object [] noArgs = new Object []{}; 80 private static Logger log = Logger.getLogger(BeanUtils.class.getName()); 81 82 88 public static BeanPropertyDescriptor[] getPd(Class javaType) 89 { 90 return getPd(javaType, null); 91 } 92 93 100 public static BeanPropertyDescriptor[] getPd(Class javaType, TypeDesc typeDesc) 101 { 102 BeanPropertyDescriptor[] pd; 103 try 104 { 105 final Class secJavaType = javaType; 106 107 PropertyDescriptor [] rawPd = getPropertyDescriptors(secJavaType); 109 pd = processPropertyDescriptors(rawPd, javaType, typeDesc); 110 } 111 catch (Exception e) 112 { 113 throw new InternalException(e); 115 } 116 return pd; 117 } 118 119 private static PropertyDescriptor [] getPropertyDescriptors(final Class secJavaType) 120 { 121 return (PropertyDescriptor [])AccessController.doPrivileged(new PrivilegedAction () 122 { 123 public Object run() 124 { 125 PropertyDescriptor [] result = null; 126 try 128 { 129 boolean isAxisFault = AxisFault.class.isAssignableFrom(secJavaType); 131 boolean isThrowable = Throwable .class.isAssignableFrom(secJavaType); 132 133 if (isAxisFault) 134 { 135 result = Introspector. 137 getBeanInfo(secJavaType, AxisFault.class). 138 getPropertyDescriptors(); 139 } 140 else if (isThrowable) 141 { 142 result = Introspector. 144 getBeanInfo(secJavaType, Throwable .class). 145 getPropertyDescriptors(); 146 } 147 else 148 { 149 result = Introspector. 151 getBeanInfo(secJavaType). 152 getPropertyDescriptors(); 153 } 154 155 if (isThrowable) 157 { 158 List bpList = new ArrayList (); 159 bpList.add(new PropertyDescriptor ("message", Throwable .class, "getMessage", null)); 160 bpList.addAll(Arrays.asList(result)); 161 result = new PropertyDescriptor [bpList.size()]; 162 bpList.toArray(result); 163 } 164 } 165 catch (java.beans.IntrospectionException Iie) 166 { 167 } 168 170 return result; 171 } 172 }); 173 } 174 175 178 public static Vector getBeanAttributes(Class javaType, TypeDesc typeDesc) 179 { 180 Vector ret = new Vector (); 181 182 if (typeDesc == null) 183 { 184 186 try 189 { 190 Method getAttributeElements = 191 javaType.getMethod("getAttributeElements", 192 new Class []{}); 193 String [] array = (String [])getAttributeElements.invoke(null, noArgs); 195 196 ret = new Vector (array.length); 198 for (int i = 0; i < array.length; i++) 199 { 200 ret.add(array[i]); 201 } 202 } 203 catch (Exception e) 204 { 205 ret.clear(); 206 } 207 } 208 else 209 { 210 FieldDesc[] fields = typeDesc.getFields(); 211 if (fields != null) 212 { 213 for (int i = 0; i < fields.length; i++) 214 { 215 FieldDesc field = fields[i]; 216 if (!field.isElement()) 217 { 218 ret.add(field.getFieldName()); 219 } 220 } 221 } 222 } 223 224 return ret; 225 } 226 227 237 public static BeanPropertyDescriptor[] processPropertyDescriptors(PropertyDescriptor [] rawPd, Class cls) 238 { 239 return processPropertyDescriptors(rawPd, cls, null); 240 } 241 242 public static BeanPropertyDescriptor[] processPropertyDescriptors(PropertyDescriptor [] rawPd, Class cls, TypeDesc typeDesc) 243 { 244 245 BeanPropertyDescriptor[] myPd = new BeanPropertyDescriptor[rawPd.length]; 247 248 ArrayList pd = new ArrayList (); 249 250 try 251 { 252 for (int i = 0; i < rawPd.length; i++) 253 { 254 if (rawPd[i].getName().equals(Constants.ANYCONTENT)) 256 continue; 257 pd.add(new BeanPropertyDescriptor(rawPd[i])); 258 } 259 260 Field fields[] = cls.getFields(); 262 if (fields != null && fields.length > 0) 263 { 264 for (int i = 0; i < fields.length; i++) 267 { 268 Field f = fields[i]; 269 String clsName = f.getDeclaringClass().getName(); 273 if (clsName.startsWith("java.") || 274 clsName.startsWith("javax.")) 275 { 276 continue; 277 } 278 if (!(Modifier.isStatic(f.getModifiers()) || 280 Modifier.isFinal(f.getModifiers()) || 281 Modifier.isTransient(f.getModifiers()))) 282 { 283 String fName = f.getName(); 284 boolean found = false; 285 for (int j = 0; j < rawPd.length && !found; j++) 286 { 287 String pName = 288 ((BeanPropertyDescriptor)pd.get(j)).getName(); 289 if (pName.length() == fName.length() && 290 pName.substring(0, 1).equalsIgnoreCase(fName.substring(0, 1))) 291 { 292 293 found = pName.length() == 1 || 294 pName.substring(1).equals(fName.substring(1)); 295 } 296 } 297 298 if (!found) 299 { 300 pd.add(new FieldPropertyDescriptor(f.getName(), f)); 301 } 302 } 303 } 304 } 305 306 if (typeDesc != null && 308 typeDesc.getFields(true) != null) 309 { 310 ArrayList ordered = new ArrayList (); 311 FieldDesc[] fds = typeDesc.getFields(true); 313 for (int i = 0; i < fds.length; i++) 314 { 315 FieldDesc field = fds[i]; 316 if (field.isElement()) 317 { 318 boolean found = false; 319 for (int j = 0; 320 j < pd.size() && !found; 321 j++) 322 { 323 if (field.getFieldName().equals(((BeanPropertyDescriptor)pd.get(j)).getName())) 324 { 325 ordered.add(pd.remove(j)); 326 found = true; 327 } 328 } 329 } 330 } 331 while (pd.size() > 0) 333 { 334 ordered.add(pd.remove(0)); 335 } 336 pd = ordered; 338 } 339 340 myPd = new BeanPropertyDescriptor[pd.size()]; 341 for (int i = 0; i < pd.size(); i++) 342 { 343 myPd[i] = (BeanPropertyDescriptor)pd.get(i); 344 } 345 } 346 catch (Exception e) 347 { 348 log.error(Messages.getMessage("badPropertyDesc00", 349 cls.getName()), e); 350 throw new InternalException(e); 351 } 352 353 return myPd; 354 } 355 356 public static BeanPropertyDescriptor getAnyContentPD(Class javaType) 357 { 358 PropertyDescriptor [] pds = getPropertyDescriptors(javaType); 359 for (int i = 0; i < pds.length; i++) 360 { 361 PropertyDescriptor pd = pds[i]; 362 if (pd.getName().equals(Constants.ANYCONTENT)) 363 return new BeanPropertyDescriptor(pd); 364 } 365 return null; 366 } 367 } 368 | Popular Tags |