public class Objects {
public static Object getObjectFromString (Class c,String message,String allSplit,String mapSplit) throws Exception{
Object o = c.newInstance();
Field[] declaredFields = c.getDeclaredFields();
String[] split = message.split(allSplit);
Object[] os = new Object[declaredFields.length];
for (String s : split) {
String[] split1 = s.split(mapSplit);
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
if(split1[0].equals(declaredField.getName())){
Object o1 = typeConversion(declaredField.getGenericType(), split1[1]);
declaredField.set(o,o1);
break;
}
}
}
return o;
}
public static Object typeConversion(Type t,String str){
if(t.equals(String.class)){
return str;
}else if(t.equals(Integer.class)){
return Integer.parseInt(str);
}else if(t.equals(Long.class)){
return Long.parseLong(str);
}else if(t.equals(Double.class)){
return Double.parseDouble(str);
}else if(t.equals(Float.class)){
return Float.parseFloat(str);
}else if(t.equals(Long.class)){
return Long.parseLong(str);
}
return null;
}
public static <T> String getJson(T t) throws Exception{
Class<?> c = t.getClass();
Field[] declaredFields = c.getDeclaredFields();
StringBuilder string = new StringBuilder();
if(declaredFields==null||declaredFields.length==0)
return null;
string.append(declaredFields[0].getName());
string.append("=");
string.append(declaredFields[0].get(t));
for (int i = 1; i < declaredFields.length; i++) {
string.append("&");
string.append(declaredFields[i].getName());
string.append("=");
string.append(declaredFields[i].get(t));
}
return string.toString();
}
}