Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Array of unknown type

I have an object which I must validate values off the problem, some of the attributes of the Objects are arrays of custom objects. Such that it will involve some boring down into the individual elements of the array. Excuting the getters for each element such as:

AttribGrp[] x =  Object.getAttribGrp()
x[i].getSomeValue()

It is this I need to get to. I have been extracted the data using an Enum with the list of the attributes In the following manner.

public String getAttribValueAsString(MethodEnum attribName) 
{
    String attribValue = null;
    Object value = getAttrib(attribName.toString());

    if (value != null)
        attribValue = value.toString();

    return attribValue;
}

calling:

    private Object invoke(String methodName, Object newValue)
{
    Object value = null;
    try
    {
        methodInvoker.setTargetMethod(methodName);

        if (newValue != null)
            methodInvoker.setArguments(new Object[]{newValue});
        else
            methodInvoker.setArguments(new Object[]{});             

        methodInvoker.prepare();
        value = methodInvoker.invoke();
    }
    catch (ClassNotFoundException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    catch (NoSuchMethodException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    catch (InvocationTargetException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    catch (IllegalAccessException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    return value;
}

I will be working with a number of arrays of different types and of different values within the arrays. I want to create a method as follows.

    public Object getAttribArray(RIORepeatingGrpEnum repeatingGrp)
{
        repeatingGrp[] grp = null;
              Object grpVal = getAttrib(repeatingGrp.toString());
              if(grp != null)
                   grp = (repeatingGrp[]) grpVal;

              return grp;
}

This is giving me multiple errors mainly concerned with repeatingGrp[]. The array type should be the same name as enum. Is it possible to create a method like this that will create an array of non defined type?

like image 892
Will Avatar asked Aug 10 '11 14:08

Will


2 Answers

If you want to have arrays of unknown types, use generics:

 public <T> T[] getAttribArray(Class<T> repeatingGrpClass)
 {
    //get the attribute based on the class (which you might get based on the enum for example)
    return (T[]) getAttrib( repeatingGrpClass.getName() ); //note that you might want to use the class object instead of its name here
 }
like image 168
Thomas Avatar answered Oct 07 '22 16:10

Thomas


No, you cannot use a variable (repeatingGrp) as a type.

There are ways to do "dynamic" casting, but these wouldn't help you. The return type of getAttribArray is Object, which would defeat the point of casting to a particular type.

And even if you could fix that, it's still not clear what you could do with this mechanism. What do you want to be able to do with the result of getAttribArray()?

like image 2
Oliver Charlesworth Avatar answered Oct 07 '22 16:10

Oliver Charlesworth