tags:

views:

117

answers:

4

Hello everybody!

I have one small problem. I have list of types(int,string,..)

ArrayList<Class> typeList;

and I have some input values;

ArrayList<Object> values;

How to cast some value to some type if I know which type from the typeList is the values;

typeList.get(i).cast(values.get(i)); <- this is not working???

Actualy I generate dynamic form in runtime. With Java reflection I get parameterTypes from methods from some class, I generate form with input fields and then i want to cast the text from the input fields to specific types from the paramterTypes that I got with Java reflection from some class.

A: 

Given your update in the comments section you could try the following assuming the list of classes is known (code not known to compile/run but just provided for a guidance)

for(int i = 0 ; i < valueList.size(); i++) {
    Object object = valueList.get(i);
    if(object instanceof String) {
        //generate code for string
    } else if (object instanceof Number) {
        //generate code for number
    } 
    //do like this for all class types
}
Calm Storm
and what if I have some specific class? Some another class.
Milan
Actually you don't need the typeList in this case at all, just compare 'object instanceof String' instead of 'clazz instanceof String... and actually 'clazz instanceof String' does not even work, you should do clazz.isAssignableFrom(String.class) if you want to do that.
fish
Like I said you need to have a valid specific list of classes. On the other hand you can get a list of values that implement an interface and generate code out of the interface
Calm Storm
+4  A: 

In most cases you simply cannot cast an object of one type to another type. In the cases where you can do the cast, the following will work:

Class<SomeClass> clazz = ...
SomeOtherClass obj = ...
SomeClass result = clazz.cast(obj);

Note that SomeClass is the type you are casting to, not the type you are casting from.

However, if the obj value is not of the right type (i.e. a SomeClass or some subtype of SomeClass) the above will give you a ClassCastException.

It sound like your program should be trying to convert the objects, not simply cast them. Or if you really do need to cast the objects, then your program needs to be prepared to deal with possible ClassCastExceptions.

Stephen C
A: 

If you don't have very many types now or in the future, I would say go for the if-else structure suggested by Calm Storm.

However, if you want to make the system more generic and extendable, you could use something similar to JavaBeans mechanisms, ie. have a "editor" for each type:

interface TypeEditor {
    public String generateCode(Object value);
}

Then have different concrete classes for different types, like StringEditor, IntegerEditor...

For handling the editors, create something like TypeEditorManager:

public class TypeEditorManager {
    private static Map<Class, TypeEditor> editors = ....;
    static {
        editors.put(String.class, new StringEditor());
        editors.put(Integer.class, new IntegerEditor());
        ...
    }

    public TypeEditor getTypeEditor(Object value) {
        return editors.get(value.getClass());
    }
}

Note that getTypeEditor will return null if the class of the value is a subtype of a registered class. I leave it to you as a homework to figure out how to match superclasses ;-) (Ok, I'm lazy...)

You can also make this easier to extend by having the mapping of types to editors for example in a separate file, Spring configuration etc...

And then, make this even nicer by using generics.

fish
Oh but this is not what you wanted to achieve, sorry :-( I'll still leave this here in case it might generate some value to you.
fish
A: 

SOLUTION:

So I solve the problem like this:

if(typeList.get(t).equals(byte.class))
{
inputParams[t] = Byte.parseByte(input.getValue().toString());
}   
else if(typeList.get(t).equals(short.class))
{
inputParams[t] = Short.parseShort(input.getValue().toString());
}
else if(typeList.get(t).equals(int.class))
{
inputParams[t] = Integer.parseInt(input.getValue().toString());
}           
else if(typeList.get(t).equals(long.class))
{
inputParams[t] = Long.parseLong(input.getValue().toString());
}           
else if(typeList.get(t).equals(float.class))
{
inputParams[t] = Float.parseFloat(input.getValue().toString());
}           
else if(listOperationParameters.get(t).equals(double.class))
{
inputParams[t] = Double.parseDouble(input.getValue().toString());
}           
else if(typeList.get(t).equals(boolean.class))
{
inputParams[t] = Boolean.parseBoolean(input.getValue().toString());
}           
else if(typeList.get(t).equals(char.class))
{
inputParams[t] = input.getValue().toString().charAt(0);
}           
else{
inputParams[t] = typeList[t].cast(input.getValue());
}

Primitive types cannot be cast, thats why I check, and at the end if its some another object, (also String) I cast it.

Thank you everybody for the help!

Milan