Methods Summary |
---|
public static java.lang.Object | convertArrayToObject(java.lang.Object array, java.lang.Class destClass)Convert an array object of which type is T[] to ArrayOfT class.
int dim = getArrayDimension(array.getClass());
if (dim == 0) {
return null;
}
Object dest = null;
try {
// create the destArray
int arraylen = Array.getLength(array);
Object destArray = null;
Class destComp = null;
if (!destClass.isArray()) {
dest = destClass.newInstance();
BeanPropertyDescriptor pd = getArrayComponentPD(destClass);
if (pd == null)
return null;
destComp = pd.getType();
destArray = Array.newInstance(destComp, arraylen);
pd.set(dest, destArray);
} else {
destComp = destClass.getComponentType();
dest = Array.newInstance(destComp, arraylen);
destArray = dest;
}
// iniialize the destArray
for (int i = 0; i < arraylen; i++) {
Array.set(destArray, i, createNewInstance(destComp));
}
// set the destArray
for (int i = 0; i < arraylen; i++) {
Object comp = Array.get(array, i);
if(comp == null)
continue;
if (comp.getClass().isArray()) {
Class cls = Array.get(destArray, i).getClass();
Array.set(destArray, i, convertArrayToObject(comp, cls));
}
else {
Array.set(destArray, i, comp);
}
}
} catch (IllegalAccessException ignore) {
return null;
} catch (InvocationTargetException ignore) {
return null;
} catch (InstantiationException ignore) {
return null;
}
return dest;
|
public static java.lang.Object | convertObjectToArray(java.lang.Object obj, java.lang.Class arrayType)Convert ArrayOfT to T[].
try {
ArrayInfo arri = new ArrayInfo();
boolean rc = internalIsConvertable(obj.getClass(), arri, arrayType);
if (rc == false) {
return obj;
}
BeanPropertyDescriptor pd = null;
pd = getArrayComponentPD(obj.getClass());
if (pd == null) {
return NON_CONVERTABLE;
}
Object comp = pd.get(obj);
if (comp == null) {
return null;
}
int arraylen = 0;
if (comp.getClass().isArray()) {
arraylen = Array.getLength(comp);
} else {
return comp;
}
int[] dims = new int[arri.dimension];
dims[0] = arraylen;
Object targetArray = Array.newInstance(arri.componentType, dims);
for (int i = 0; i < arraylen; i++) {
Object subarray = Array.get(comp, i);
Class subarrayClass = arrayType.getComponentType();
Array.set(targetArray, i, convertObjectToArray(subarray, subarrayClass));
}
return targetArray;
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
|
private static java.lang.Object | createNewInstance(java.lang.Class cls)
Object obj = null;
if (!cls.isPrimitive())
obj = cls.newInstance();
else {
if (boolean.class.isAssignableFrom(cls))
obj = new Boolean(false);
else if (byte.class.isAssignableFrom(cls))
obj = new Byte((byte)0);
else if (char.class.isAssignableFrom(cls))
obj = new Character('\u0000");
else if (short.class.isAssignableFrom(cls))
obj = new Short((short)0);
else if (int.class.isAssignableFrom(cls))
obj = new Integer(0);
else if (long.class.isAssignableFrom(cls))
obj = new Long(0L);
else if (float.class.isAssignableFrom(cls))
obj = new Float(0.0F);
else if (double.class.isAssignableFrom(cls))
obj = new Double(0.0D);
}
return obj;
|
private static BeanPropertyDescriptor | getArrayComponentPD(java.lang.Class clazz)Gets the BeanPropertyDescriptor of ArrayOfT class's array member.
BeanPropertyDescriptor bpd = null;
int count = 0;
Class cls = clazz;
while (cls != null && cls.getName() != null && !cls.getName().equals("java.lang.Object")) {
BeanPropertyDescriptor bpds[] = BeanUtils.getPd(clazz);
for (int i = 0; i < bpds.length; i++) {
BeanPropertyDescriptor pd = bpds[i];
if (pd.isReadable() && pd.isWriteable() && pd.isIndexed()) {
count++;
if (count >= 2)
return null;
else
bpd = pd;
}
}
cls = cls.getSuperclass();
}
if (count == 1) {
return bpd;
}
else
return null;
|
public static int | getArrayDimension(java.lang.Class arrayType)Gets the dimension of arrayType
if (!arrayType.isArray())
return 0;
int dim = 0;
Class compType = arrayType;
do {
dim++;
arrayType = compType;
compType = arrayType.getComponentType();
} while (compType.isArray());
return dim;
|
private static boolean | internalIsConvertable(java.lang.Class clazz, org.apache.axis.utils.ArrayUtil$ArrayInfo arri, java.lang.Class arrayType)Check if the clazz(perhaps ArrayOfT class) can be converted to T[].
BeanPropertyDescriptor pd = null, oldPd = null;
if (!arrayType.isArray())
return false;
Class destArrCompType = arrayType.getComponentType();
Class src = clazz;
int depth = 0;
while (true) {
pd = getArrayComponentPD(src);
if (pd == null)
break;
depth++;
src = pd.getType();
oldPd = pd;
if (destArrCompType.isAssignableFrom(src))
break;
}
if (depth == 0 || oldPd.getType() == null) {
return false;
}
arri.componentType = oldPd.getType();
arri.dimension = depth;
Class componentType = oldPd.getType();
int[] dims = new int[depth];
Object array = Array.newInstance(componentType, dims);
arri.arrayType = array.getClass();
if (arrayType.isAssignableFrom(arri.arrayType))
return true;
else
return false;
|
public static boolean | isConvertable(java.lang.Class clazz, java.lang.Class arrayType)Check if the clazz(perhaps ArrayOfT class) can be converted to T[].
ArrayInfo arrInfo = new ArrayInfo();
return internalIsConvertable(clazz, arrInfo, arrayType);
|