3

I've an object pObject

Object pObject = someRpcCall();

I don't know the type of pObject

What i know is System.out.println(pObject.toString()) outputs

{partner_shipping_id=12, partner_order_id=11, user_id=1, partner_invoice_id=13, pricelist_id=1, fiscal_position=false, payment_term=false}

How can I convert this pObject to object of the following class

import android.os.Parcel;
import android.os.Parcelable;
public class Customer implements Parcelable {

    private int id;
    private String name = "";

    public Customer() {
        // TODO Auto-generated constructor stub
    }

    /**
     * This will be used only by the MyCreator
     * 
     * @param source
     */
    public Customer(Parcel source) {
        /*
         * Reconstruct from the Parcel
         */
        id = source.readInt();
        name = source.readString();
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

        @Override
        public Customer createFromParcel(Parcel source) {
            return new Customer(source);
        }

        @Override
        public Customer[] newArray(int size) {
            return new Customer[size];
            // TODO Auto-generated method stub
        }

    };

}
Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236

2 Answers2

16

Whats the output of System.out.println(pObject.getClass().getName());

If its the same Customer class, then you could cast the object like this

Customer cust = (Customer) pObject;

Rajeev Sreedharan
  • 1,753
  • 4
  • 20
  • 30
  • `pObject.getClass().getName()` yields `java.util.HashMap` – Mithun Sreedharan Aug 31 '11 at 11:53
  • 1
    In that case you would need to iterate through all the key/value pairs and explictly set the values. There would be some kind of mapping somewhere by which you automate it, but incase you dont have then use it like this: `HashMap hashMap = (HashMap) pObject; Customer customer = new Customer(); customer.setId(hashMap.get("partner_shipping_id"));` But such hard coding is generally best avoided, unless you dont have a choice. – Rajeev Sreedharan Aug 31 '11 at 12:03
  • `System.out.println(pObject.getClass().getName())` tip really helped me, so accepting your answer – Mithun Sreedharan Aug 31 '11 at 12:41
  • @RajeevSreedharan : Ive got a question on this : Would it probably be possible to do the following `Class someObj=(pObject.getClass()) pObject` , Id like to convert the object dynamically back to its class, no matter which class it is. – Luatic Apr 22 '17 at 11:26
  • @user7185318 Objects do not need a conversion, references do. Dynamic casting is not possible at compile time. `pObject.getClass()` would return you a `Class`. Generic calls are best achieved through reflection. – Rajeev Sreedharan Apr 22 '17 at 11:46
  • @RajeevSreedharan : But lets assume I have an Object[] with three objects of type A,B and C. They all have a method toString(). So how could I now dynamically cast to A,B, or C, depending on what getClass() returns ? I always did it using ifs/switches. But I was asking myself if there would be an easier way. So, is there any ? – Luatic Apr 22 '17 at 15:52
0

The answer to the above problem is provided, but I have a generic solution which I want to share all of you.

  1. First, fetch the class name using Object object(provided)
  2. Using Enum know the Class name
  3. Create a reference object of the known class
  4. Initialize your Object class object

e.g:

package com.currentobject;
import com.currentobject.model.A;
import com.currentobject.model.B;
import com.currentobject.model.C;

Class CurrentObject{

public void objectProvider(Object object){
String className = object.getClass().getCanonicalName();
ModelclassName modelclass = ModelclassName.getOperationalName(className);

    switch (modelclass) {
        case A:

            A a = (A) object;

            break;
        case B:
            B b = (B) object;


            break;
        case C:
            C c = (C) object;

            break;
        }
    }   
}


enum ModelclassName {
    A("com.currentobject.model.A"),
    B("com.currentobject.model.B"),
    C("com.currentobject.model.C");

    private ModelclassName(String name) {
        this.name = name;
    }

    public static ModelclassName getOperationalName(final String operationName) {

        for(ModelclassName oprname :ModelclassName.values()) {
            if(oprname.name.equalsIgnoreCase(operationName)){
                return oprname ;
            }
        }
        return null;

    }

    String name;
}
Tapan
  • 285
  • 2
  • 8