Milan
Milan

Reputation: 653

Casting objects to Integer,string ,

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.

Upvotes: 0

Views: 1692

Answers (4)

Kannan Ekanath
Kannan Ekanath

Reputation: 17601

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
}

Upvotes: 0

Milan
Milan

Reputation: 653

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!

Upvotes: 0

fish
fish

Reputation: 1050

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.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718778

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.

Upvotes: 4

Related Questions