Creating an Array of unknown type

对着背影说爱祢 提交于 2020-08-19 04:32:33

问题


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?


回答1:


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
 }



回答2:


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()?




回答3:


As Oli Charlesworth points out, you can not use the variable name to cast. For a generic type, you will have to cast to Object or Object[].

Also the Object -> Object[] cast looks illegal. You probably just want a straight cast like so:

public Object[] getAttribArray(RIORepeatingGrpEnum repeatingGrp)
{
    Object[] grp = (Object[])getAttrib(repeatingGrp.toString());
    return grp;
}


来源:https://stackoverflow.com/questions/7012241/creating-an-array-of-unknown-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!