Reputation: 8631
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?
Upvotes: 6
Views: 6301
Reputation: 6642
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;
}
Upvotes: 0
Reputation: 88707
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
}
Upvotes: 5
Reputation: 272517
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()
?
Upvotes: 2