java - Reducing if else to determine the dataType -


how reduce usiong reflection

private static object determinedatatype(final string value, string datatype) {     system.out.println("name--->>" + datatype);     if(datatype.equals(boolean.class.getname()))     {        return new boolean(value);     }     else if(datatype.equals(string.class.getname()))     {         return new string(value);     }     else if(datatype.equals(character.class.getname()))     {         return new string(value);     }     else if(datatype.equals(byte.class.getname()))     {         return new byte(value);     }     else if(datatype.equals(short.class.getname()))     {         return new short(value);     }     else if(datatype.equals(integer.class.getname()))     {         return new integer(value);     }     else if(datatype.equals(long.class.getname()))     {         return new long(value);     }     else if(datatype.equals(float.class.getname()))     {         return  new float(value);     }     else if(datatype.equals(double.class.getname()))     {         return  new double(value);     }     //defualt return string value, lets' aopi validation     return new string(value); } 

you can use class.forname , method getconstructor (here example):

object instance = class.forname(datatype)          .getconstructor(new class[] {string.class})          .newinstance(new object[]{value}); 

by way not work character , boolean because make special treatment them.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -