6

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?

2
  • I don't understand "repeatingGrp[]" you are treating it as a type, but repeatingGrp is a variable name... Commented Aug 10, 2011 at 14:18
  • possible duplicate of Creating Arrays of Generic Types in Java Commented Aug 10, 2011 at 14:19

3 Answers 3

5

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
 }
Sign up to request clarification or add additional context in comments.

Comments

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

8 Comments

OK I can get that. But say I was able to have the return type: repeatGrp[] which exists within the incoming object.
@Will: But then what? What would you then do with the result?
I would pass over the array calling the various getter methods on each element to extract the values and validate them.
@Will: Why not do that with polymorphism? Derive all your types from a common base class (or interface), and then have that base-class/interface declare a validate() method.
I'm using a rules engine for the rules. It is this engine that is calling this code. I'm declaring the different types available via RIORepeatingGrpEnum granted that is just the names since that way the arrays can be extracted when it runs the get RIORepeatingGrpEnum
|
0

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;
}

1 Comment

You still have that Object -> Object[] cast in the return statement :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.