Search code examples
javaarrayslistreflectiontoarray

Converting array of Objects to array of custom Types


I have a method to build the array for the required type. It works for the primitive types. But when I have array of custom objects it doesn't work. So I have tweaked it. But still it fails. Code is like this :

    private Object buildArray(  String type,   Object object) {
    final Class<?> requiredType =  loadClass(type);
    final String typeName = type.substring(2).replace(";", "").trim();
        Object[] array = ((Object[]) object);
        ArrayList<Object> arrayList = new ArrayList<Object>(array.length);
        for (Object customObj : array) {
            arrayList.add(castToRequiredType(typeName, customObj));
        }
        return arrayList.toArray();
 }

In this castToRequiredType : casts the CustomObject to the CustomType where CustomType is a class. And array to be build is of type CustomType. I am stuck at dynamically building the array of CustomType.

Any help in this direction is welcome. Thanks in advance.


Solution

  • Thanks I have solved it using Axis's Array Util For the same