1 16 package org.apache.axis.utils; 17 18 import org.apache.axis.AxisFault; 19 import org.apache.axis.Constants; 20 import org.apache.axis.InternalException; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.description.FieldDesc; 23 import org.apache.axis.description.TypeDesc; 24 import org.apache.commons.logging.Log; 25 26 import java.beans.Introspector ; 27 import java.beans.PropertyDescriptor ; 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.Modifier ; 31 import java.security.AccessController ; 32 import java.security.PrivilegedAction ; 33 import java.util.ArrayList ; 34 import java.util.Vector ; 35 36 public class BeanUtils { 37 38 public static final Object [] noArgs = new Object [] {}; 39 protected static Log log = 40 LogFactory.getLog(BeanUtils.class.getName()); 41 42 47 public static BeanPropertyDescriptor[] getPd(Class javaType) { 48 return getPd(javaType, null); 49 } 50 51 57 public static BeanPropertyDescriptor[] getPd(Class javaType, TypeDesc typeDesc) { 58 BeanPropertyDescriptor[] pd; 59 try { 60 final Class secJavaType = javaType; 61 62 PropertyDescriptor [] rawPd = getPropertyDescriptors(secJavaType); 64 pd = processPropertyDescriptors(rawPd,javaType,typeDesc); 65 } catch (Exception e) { 66 throw new InternalException(e); 68 } 69 return pd; 70 } 71 72 private static PropertyDescriptor [] getPropertyDescriptors(final Class secJavaType) { 73 return (PropertyDescriptor [])AccessController.doPrivileged( 74 new PrivilegedAction () { 75 public Object run() { 76 PropertyDescriptor [] result = null; 77 try { 79 if (AxisFault.class.isAssignableFrom(secJavaType)) { 81 result = Introspector. 83 getBeanInfo(secJavaType,AxisFault.class). 84 getPropertyDescriptors(); 85 } else if (Throwable .class != secJavaType && Throwable .class.isAssignableFrom(secJavaType)) { 86 result = Introspector. 88 getBeanInfo(secJavaType,Throwable .class). 89 getPropertyDescriptors(); 90 } else { 91 result = Introspector. 93 getBeanInfo(secJavaType). 94 getPropertyDescriptors(); 95 } 96 } catch (java.beans.IntrospectionException Iie) { 98 } 99 return result; 100 } 101 }); 102 } 103 104 107 public static Vector getBeanAttributes(Class javaType, TypeDesc typeDesc) { 108 Vector ret = new Vector (); 109 110 if (typeDesc == null) { 111 113 try { 116 Method getAttributeElements = 117 javaType.getMethod("getAttributeElements", 118 new Class [] {}); 119 String [] array = (String [])getAttributeElements.invoke(null, noArgs); 121 122 ret = new Vector (array.length); 124 for (int i = 0; i < array.length; i++) { 125 ret.add(array[i]); 126 } 127 } catch (Exception e) { 128 ret.clear(); 129 } 130 } else { 131 FieldDesc [] fields = typeDesc.getFields(); 132 if (fields != null) { 133 for (int i = 0; i < fields.length; i++) { 134 FieldDesc field = fields[i]; 135 if (!field.isElement()) { 136 ret.add(field.getFieldName()); 137 } 138 } 139 } 140 } 141 142 return ret; 143 } 144 154 public static BeanPropertyDescriptor[] processPropertyDescriptors( 155 PropertyDescriptor [] rawPd, Class cls) { 156 return processPropertyDescriptors(rawPd, cls, null); 157 } 158 159 public static BeanPropertyDescriptor[] processPropertyDescriptors( 160 PropertyDescriptor [] rawPd, Class cls, TypeDesc typeDesc) { 161 162 BeanPropertyDescriptor[] myPd = new BeanPropertyDescriptor[rawPd.length]; 164 165 ArrayList pd = new ArrayList (); 166 167 try { 168 for (int i=0; i < rawPd.length; i++) { 169 if (rawPd[i].getName().equals(Constants.ANYCONTENT)) 171 continue; 172 pd.add(new BeanPropertyDescriptor(rawPd[i])); 173 } 174 175 Field fields[] = cls.getFields(); 177 if (fields != null && fields.length > 0) { 178 for (int i=0; i < fields.length; i++) { 181 Field f = fields[i]; 182 String clsName = f.getDeclaringClass().getName(); 186 if (clsName.startsWith("java.") || 187 clsName.startsWith("javax.")) { 188 continue; 189 } 190 if (!(Modifier.isStatic(f.getModifiers()) || 192 Modifier.isFinal(f.getModifiers()) || 193 Modifier.isTransient(f.getModifiers()))) { 194 String fName = f.getName(); 195 boolean found = false; 196 for (int j=0; j< rawPd.length && !found; j++) { 197 String pName = 198 ((BeanPropertyDescriptor)pd.get(j)).getName(); 199 if (pName.length() == fName.length() && 200 pName.substring(0,1).equalsIgnoreCase( 201 fName.substring(0,1))) { 202 203 found = pName.length() == 1 || 204 pName.substring(1).equals(fName.substring(1)); 205 } 206 } 207 208 if (!found) { 209 pd.add(new FieldPropertyDescriptor(f.getName(), f)); 210 } 211 } 212 } 213 } 214 215 if (typeDesc != null && 217 typeDesc.getFields(true) != null) { 218 ArrayList ordered = new ArrayList (); 219 FieldDesc[] fds = typeDesc.getFields(true); 221 for (int i=0; i<fds.length; i++) { 222 FieldDesc field = fds[i]; 223 if (field.isElement()) { 224 boolean found = false; 225 for (int j=0; 226 j<pd.size() && !found; 227 j++) { 228 if (field.getFieldName().equals( 229 ((BeanPropertyDescriptor)pd.get(j)).getName())) { 230 ordered.add(pd.remove(j)); 231 found = true; 232 } 233 } 234 } 235 } 236 while (pd.size() > 0) { 238 ordered.add(pd.remove(0)); 239 } 240 pd = ordered; 242 } 243 244 myPd = new BeanPropertyDescriptor[pd.size()]; 245 for (int i=0; i <pd.size(); i++) { 246 myPd[i] = (BeanPropertyDescriptor) pd.get(i); 247 } 248 } catch (Exception e) { 249 log.error(Messages.getMessage("badPropertyDesc00", 250 cls.getName()), e); 251 throw new InternalException(e); 252 } 253 254 return myPd; 255 } 256 257 public static BeanPropertyDescriptor getAnyContentPD(Class javaType) { 258 PropertyDescriptor [] pds = getPropertyDescriptors(javaType); 259 return getSpecificPD(pds, Constants.ANYCONTENT); 260 } 261 262 public static BeanPropertyDescriptor getSpecificPD(PropertyDescriptor [] pds, 263 String name) { 264 for (int i = 0; i < pds.length; i++) { 265 PropertyDescriptor pd = pds[i]; 266 if (pd.getName().equals(name)) 267 return new BeanPropertyDescriptor(pd); 268 } 269 return null; 270 } 271 272 public static BeanPropertyDescriptor getSpecificPD(BeanPropertyDescriptor[] pds, 273 String name) { 274 for (int i = 0; i < pds.length; i++) { 275 BeanPropertyDescriptor pd = pds[i]; 276 if (pd.getName().equals(name)) 277 return pd; 278 } 279 return null; 280 } 281 } 282 | Popular Tags |