|
|
@@ -1,171 +0,0 @@
|
|
|
-package com.usai.ratradefiling.base;
|
|
|
-
|
|
|
-import org.json.JSONObject;
|
|
|
-
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.lang.reflect.Method;
|
|
|
-
|
|
|
-public class BasicObject extends NoProguard {
|
|
|
-
|
|
|
- public void setValuesForKeysWithJSON(JSONObject json) {
|
|
|
-// Log.e("BasicObject", "setValuesForKeysWithJSON: " );
|
|
|
- if (json == null || json.length() == 0) {
|
|
|
-
|
|
|
-// Log.e("BasicObject", "setValuesForKeysWithJSON: json is null" );
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- try {
|
|
|
- Class cls = getClass();
|
|
|
-// Log.e("BasicObject", "cls != null "+(cls != null) );
|
|
|
-// Log.e("BasicObject", "cls != Class.class "+(cls != Class.class) );
|
|
|
-
|
|
|
-// Log.e("BasicObject", "Class.class "+Class.class );
|
|
|
- while (cls != null && cls != Class.class) {
|
|
|
-// Log.e("BasicObject", "cls "+cls );
|
|
|
- Field[] fields = cls.getFields();
|
|
|
-
|
|
|
-// Log.e("BasicObject", "fields "+fields.length );
|
|
|
-// Log.e("BasicObject", "fields "+fields );
|
|
|
-
|
|
|
- for (Field f : fields) {
|
|
|
-
|
|
|
- String key = f.getName();
|
|
|
-
|
|
|
- Object value = json.opt(key);
|
|
|
-// Log.e("BasicObject full info", "key "+key+" value "+value + " type "+f.getType());
|
|
|
- if (value == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- Class type = f.getType();
|
|
|
- if (type == value.getClass() || ((type == int.class || type == long.class) && value.getClass() == Integer.class) || ((type == float.class || type == double.class) && (value.getClass() == Double.class || value.getClass() == Float.class)) || ((type == Boolean.class || type == boolean.class) && (value.getClass() == boolean.class || value.getClass() == Boolean.class))) {
|
|
|
-
|
|
|
-
|
|
|
- boolean success = invokeSetter(cls,f,type,key,value);
|
|
|
- if (!success) {
|
|
|
- f.set(this,value);
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- if (value.getClass() == String.class) {
|
|
|
-
|
|
|
- String string = (String)value;
|
|
|
-
|
|
|
- if (type == Integer.class || type == int.class || type == long.class) {
|
|
|
-
|
|
|
- value = Integer.valueOf(string);
|
|
|
-
|
|
|
-
|
|
|
- } else if (type == Double.class || type == double.class || type == float.class) {
|
|
|
-
|
|
|
- value = Double.valueOf(string);
|
|
|
-
|
|
|
- } else if (type == Boolean.class || type == boolean.class) {
|
|
|
-
|
|
|
- value = Boolean.valueOf(string);
|
|
|
- }
|
|
|
-
|
|
|
- boolean success = invokeSetter(cls,f,type,key,value);
|
|
|
- if (!success) {
|
|
|
- f.set(this,value);
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- if (type == Object.class) {
|
|
|
- boolean success = invokeObjectSetter(type,key,(Object) value);
|
|
|
-// Log.e("invokeObjectSetter", ""+cls+" , "+f+","+type+" , "+key+" , "+value+" , "+success );
|
|
|
-
|
|
|
- if (!success) {
|
|
|
- f.set(this,value);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- cls = cls.getSuperclass();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public boolean invokeObjectSetter( Class type,String key,Object value) {
|
|
|
-
|
|
|
- String setter = "set" + toUpperCaseFirstOne(key);
|
|
|
- try {
|
|
|
-
|
|
|
-
|
|
|
-// Class<?> myClassType = Class.forName(cls.getName());
|
|
|
-// Class<?>[] types = new Class[] { File.class };
|
|
|
-// Constructor<?> cons = myClassType.getConstructor(types);
|
|
|
-
|
|
|
-// Log.e("invokeObjectSetter", "settler: "+setter+" type "+type );
|
|
|
-
|
|
|
- Class[] cArg = new Class[1];
|
|
|
- cArg[0] = type;
|
|
|
-// Log.e("invokeObjectSetter", getClass().getName() );
|
|
|
- Method method = getClass().getMethod(setter,cArg);
|
|
|
- if (method != null) {
|
|
|
-
|
|
|
- method.invoke(this,value);
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- catch(NoSuchMethodException e)
|
|
|
- {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
-// dbgUtil.Logd("invokeSetter",e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
- public boolean invokeSetter(Class cls, Field f, Class type,String key,Object value) {
|
|
|
-
|
|
|
- String setter = "set" + toUpperCaseFirstOne(key);
|
|
|
- try {
|
|
|
-
|
|
|
-
|
|
|
- Class<?> myClassType = Class.forName(cls.getName());
|
|
|
-// Class<?>[] types = new Class[] { File.class };
|
|
|
-// Constructor<?> cons = myClassType.getConstructor(types);
|
|
|
-
|
|
|
- Method method = myClassType.getMethod(setter,type);
|
|
|
- if (method != null) {
|
|
|
-
|
|
|
- method.invoke(this,value);
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- catch(NoSuchMethodException e)
|
|
|
- {
|
|
|
-// e.printStackTrace();
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
-// dbgUtil.Logd("invokeSetter",e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- public static String toUpperCaseFirstOne(String s){
|
|
|
- if(Character.isUpperCase(s.charAt(0)))
|
|
|
- return s;
|
|
|
- else
|
|
|
- return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
|
|
|
- }
|
|
|
-}
|