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