Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been trying to create my own library to serialize and de-serialize primitive types from a class to xml and from xml to a class instance using reflection to examine method naming patterns and method return types.

So far I have been able to do this with all the basic primitive types but I got stuck on serializing an array of the same primitives.

For example I invoke the class method to get the array of primitives:

method.invoke(clazz, (Object[])null);

This method will return only a primitive array int[], double[], float[], char[] etc. though we don't know which one it will be.

I have tried using a generic such as

T t = (T)method.invoke(clazz, (Object[])null);
T[] t = (T[])method.invoke(clazz, (Object[])null);

But it doesn't let me cast from the primitive array to an object.

And you can't use Array.newInstance assuming we don't know the type.

Is there a way that I can convert this array of primitives to say an Object array in a generic manner.

In a generic manner meaning without needing to know or checking what the type of the array is. Or should I just cycle through all the primitive types and handle all of them separately.

I can do this both ways the only reason I want to do this in a generic manner is to cut down on the redundant code.

Thanks in advance.

share|improve this question
    
+1 Great question! –  Paul Bellora Jun 1 '13 at 4:30

3 Answers 3

up vote 9 down vote accepted

You can use the Array utility class

public static Object[] toObjectArray(Object array) {
    int length = Array.getLength(array);
    Object[] ret = new Object[length];
    for(int i = 0; i < length; i++)
        ret[i] = Array.get(array, i);
    return ret;
}
share|improve this answer
1  
It worked perfectly. I never thought that I could access the object as an array. Thank you. –  Andrew Cumming May 31 '13 at 21:39

Does java.lang.reflect.Array.get() do what you want?

share|improve this answer
    
You also need the getLength() –  Peter Lawrey May 31 '13 at 21:32
    
It would be my next step. This would allow me to get an item from the array but I still need to know how to store it in a proper array first. –  Andrew Cumming May 31 '13 at 21:32
Object result = method.invoke(clazz, (Object[])null);
Class<?> arrayKlazz = result.getClass();
if (arrayKlazz.isArray()) {
    Class<?> klazz = result.getComponentType();
    if (klazz == int.class) {
        int[] intArray = arrayKlazz.cast(result);
    }
}

It seems more appropiate to hold (primitive) arrays in an Object (result above).

share|improve this answer
    
How will this help get an Object[] from the result? –  Peter Lawrey May 31 '13 at 21:34
    
Object[] is already answered. But solving the problem of serialization with Object[] for an int[], double[] etcetera seems a bit going too far. A general array should be held in an Object. I want to suggest that without too large emphasis. If an Object[] is wished - fine. –  Joop Eggen May 31 '13 at 21:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.