package org.jboss.axis.utils;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Constants;
import org.jboss.axis.InternalException;
import org.jboss.axis.description.FieldDesc;
import org.jboss.axis.description.TypeDesc;
import org.jboss.logging.Logger;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
public class BeanUtils
{
public static final Object[] noArgs = new Object[]{};
private static Logger log = Logger.getLogger(BeanUtils.class.getName());
public static BeanPropertyDescriptor[] getPd(Class javaType)
{
return getPd(javaType, null);
}
public static BeanPropertyDescriptor[] getPd(Class javaType, TypeDesc typeDesc)
{
BeanPropertyDescriptor[] pd;
try
{
final Class secJavaType = javaType;
PropertyDescriptor[] rawPd = getPropertyDescriptors(secJavaType);
pd = processPropertyDescriptors(rawPd, javaType, typeDesc);
}
catch (Exception e)
{
throw new InternalException(e);
}
return pd;
}
private static PropertyDescriptor[] getPropertyDescriptors(final Class secJavaType)
{
return (PropertyDescriptor[])AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
PropertyDescriptor[] result = null;
try
{
boolean isAxisFault = AxisFault.class.isAssignableFrom(secJavaType);
boolean isThrowable = Throwable.class.isAssignableFrom(secJavaType);
if (isAxisFault)
{
result = Introspector.
getBeanInfo(secJavaType, AxisFault.class).
getPropertyDescriptors();
}
else if (isThrowable)
{
result = Introspector.
getBeanInfo(secJavaType, Throwable.class).
getPropertyDescriptors();
}
else
{
result = Introspector.
getBeanInfo(secJavaType).
getPropertyDescriptors();
}
if (isThrowable)
{
List bpList = new ArrayList();
bpList.add(new PropertyDescriptor("message", Throwable.class, "getMessage", null));
bpList.addAll(Arrays.asList(result));
result = new PropertyDescriptor[bpList.size()];
bpList.toArray(result);
}
}
catch (java.beans.IntrospectionException Iie)
{
}
return result;
}
});
}
public static Vector getBeanAttributes(Class javaType, TypeDesc typeDesc)
{
Vector ret = new Vector();
if (typeDesc == null)
{
try
{
Method getAttributeElements =
javaType.getMethod("getAttributeElements",
new Class[]{});
String[] array = (String[])getAttributeElements.invoke(null, noArgs);
ret = new Vector(array.length);
for (int i = 0; i < array.length; i++)
{
ret.add(array[i]);
}
}
catch (Exception e)
{
ret.clear();
}
}
else
{
FieldDesc[] fields = typeDesc.getFields();
if (fields != null)
{
for (int i = 0; i < fields.length; i++)
{
FieldDesc field = fields[i];
if (!field.isElement())
{
ret.add(field.getFieldName());
}
}
}
}
return ret;
}
public static BeanPropertyDescriptor[] processPropertyDescriptors(PropertyDescriptor[] rawPd, Class cls)
{
return processPropertyDescriptors(rawPd, cls, null);
}
public static BeanPropertyDescriptor[] processPropertyDescriptors(PropertyDescriptor[] rawPd, Class cls, TypeDesc typeDesc)
{
BeanPropertyDescriptor[] myPd = new BeanPropertyDescriptor[rawPd.length];
ArrayList pd = new ArrayList();
try
{
for (int i = 0; i < rawPd.length; i++)
{
if (rawPd[i].getName().equals(Constants.ANYCONTENT))
continue;
pd.add(new BeanPropertyDescriptor(rawPd[i]));
}
Field fields[] = cls.getFields();
if (fields != null && fields.length > 0)
{
for (int i = 0; i < fields.length; i++)
{
Field f = fields[i];
String clsName = f.getDeclaringClass().getName();
if (clsName.startsWith("java.") ||
clsName.startsWith("javax."))
{
continue;
}
if (!(Modifier.isStatic(f.getModifiers()) ||
Modifier.isFinal(f.getModifiers()) ||
Modifier.isTransient(f.getModifiers())))
{
String fName = f.getName();
boolean found = false;
for (int j = 0; j < rawPd.length && !found; j++)
{
String pName =
((BeanPropertyDescriptor)pd.get(j)).getName();
if (pName.length() == fName.length() &&
pName.substring(0, 1).equalsIgnoreCase(fName.substring(0, 1)))
{
found = pName.length() == 1 ||
pName.substring(1).equals(fName.substring(1));
}
}
if (!found)
{
pd.add(new FieldPropertyDescriptor(f.getName(), f));
}
}
}
}
if (typeDesc != null &&
typeDesc.getFields(true) != null)
{
ArrayList ordered = new ArrayList();
FieldDesc[] fds = typeDesc.getFields(true);
for (int i = 0; i < fds.length; i++)
{
FieldDesc field = fds[i];
if (field.isElement())
{
boolean found = false;
for (int j = 0;
j < pd.size() && !found;
j++)
{
if (field.getFieldName().equals(((BeanPropertyDescriptor)pd.get(j)).getName()))
{
ordered.add(pd.remove(j));
found = true;
}
}
}
}
while (pd.size() > 0)
{
ordered.add(pd.remove(0));
}
pd = ordered;
}
myPd = new BeanPropertyDescriptor[pd.size()];
for (int i = 0; i < pd.size(); i++)
{
myPd[i] = (BeanPropertyDescriptor)pd.get(i);
}
}
catch (Exception e)
{
log.error(Messages.getMessage("badPropertyDesc00",
cls.getName()), e);
throw new InternalException(e);
}
return myPd;
}
public static BeanPropertyDescriptor getAnyContentPD(Class javaType)
{
PropertyDescriptor[] pds = getPropertyDescriptors(javaType);
for (int i = 0; i < pds.length; i++)
{
PropertyDescriptor pd = pds[i];
if (pd.getName().equals(Constants.ANYCONTENT))
return new BeanPropertyDescriptor(pd);
}
return null;
}
}