java反射可以用于实现parcelable接口的对象吗?

时间:2012-01-30 17:04:37

标签: android service ipc parcelable

以前我写了一个应用程序,它使用反射来将数据从json序列化到对象,并且该解决方案工作正常。现在我正在研究这个应用程序的V2.0,我现在正在尝试隔离服务中的所有模型逻辑,并使活动层与之分离。这样做的目的是服务可以在后台生存并针对Web服务运行定期更新。 我的问题是,只要我尝试使用反射来实例化一个实现parcelable接口的对象,它就会抛出一个InstantiationException。我的猜测是,parcelable接口以某种方式干扰了对象的默认构造函数。

一种解决方案是创建一个我构建的中间对象,然后将该对象发送到正确的parcelable对象的构造函数,并在IPC中发送该对象。但我觉得可能有一个更简单的解决方案,涉及以某种方式使用parcelable,我还没有想到。

public class ManagedObjectWrapper {

    private Object anObject;//the instance of the object itself
    private Class<?> theClass;//Metadata
    private Field[] declaredFields;//Metadata

    public ManagedObjectWrapper(Class<?> c){

        theClass=c;
        declaredFields = c.getDeclaredFields();
        try {
                           //this is where it crashes
            anObject =  c.newInstance();
        } catch (InstantiationException e) {

            e.printStackTrace();
        } catch (IllegalAccessException e){ 
            e.printStackTrace();
        }
    }

    public Object GetObject(){return this.anObject;}
    public Class<?> GetClass(){return this.theClass;}
    public Field[] GetDeclaredFields(){return this.declaredFields;}

private Object anObject;//the instance of the object itself private Class<?> theClass;//Metadata private Field[] declaredFields;//Metadata public ManagedObjectWrapper(Class<?> c){ theClass=c; declaredFields = c.getDeclaredFields(); try { //this is where it crashes anObject = c.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e){ e.printStackTrace(); } } public Object GetObject(){return this.anObject;} public Class<?> GetClass(){return this.theClass;} public Field[] GetDeclaredFields(){return this.declaredFields;}

这就是我如何使用反射从json中获取对象

}

private ArrayList<ManagedObjectWrapper> getObjectsFromJSON(String pluralizedColumn,JSONArray array) throws Exception
    {
        ArrayList<ManagedObjectWrapper> returnList = new ArrayList<ManagedObjectWrapper>();
        String packageName = extContext.getPackageName();
        String singularObjectName = pluralizedColumn.substring(0, pluralizedColumn.length()-1);//remove the plural s 
        String canonicalClassName = packageName.concat(".").concat(singularObjectName);
        Class<?> theClass = Class.forName(canonicalClassName);

        for(int i = 0;i < array.length(); i++){

            ManagedObjectWrapper mow = new ManagedObjectWrapper(theClass);

            JSONObject obj = array.getJSONObject(i);
                for(Field field : mow.GetDeclaredFields()){
                        Class<?>[] params = {field.getType()
                            };
                    if(!field.getName().contentEquals("CREATOR")){
                        Method setterMethod = mow.GetClass().getDeclaredMethod(SET_METHOD_PREFIX.concat(field.getName()),params);
                        Object nullTest = obj.get(field.getName());

                    if(nullTest.equals(null)){

                        }else{
                            setterMethod.invoke(mow.GetObject(), obj.get(field.getName()));
                        }
                    }

                }

        returnList.add(mow);
    }

1 个答案:

答案 0 :(得分:0)

是的,它可以。只需在类上实现默认构造函数,代码中其他更深层次隐藏的错误让我觉得它并不那么简单。但回到这个问题让我再次尝试,现在它就像以前一样工作