tags:

views:

128

answers:

5

Hi,

I have a little problem with Java (being a C++ programmer).

I have 2 related classes:

public class Patient() {
...
}

public class PatientPersistent extends Patient {
...
    public void foo() {
    System.out.println(super.getClass().toString());
    }
}

This will output:

class org.example.smartgwt.server.model.PatientPersistent

Is there a way to get the parent class type? i.e.

class org.example.smartgwt.server.model.Patient.

This will allow me to generalize some methods which I need to implement in each child which is awful.

Thanks!


UPDATE

I'm using Dozer to convert my domain Hibernate object to a Serializable version. I don't want the client to know of this, so the client only sees the Patient class. On the server side I perform conversions.

public class DataObject<Type> {

    private static final Class<Object> DstType = Type;

    public Object convert(Object srcData, final BeanFactory factory) {
        Mapper mapper = (Mapper)factory.getBean("dozerMapper");

        return (Object)mapper.map(srcData, DstType);
    }
}

public class Patient() implements Serializable {
    public Set foo;
}    

public class PatientPersistent extends Patient {

    public org.hibernate.collection.PersistentSet foo;
    DataObject<Patient> converter = new DataObject<Patient>;

    public Patient convertToSerializable(final BeanFactory factory) {
        return (Patient)converter.convert(this, factory);
    }
}

public class main() {
    // This object is not serializable so I cannot send it to the client
    PatientPersistent serializableBar = new PatientPersistent();

    // Using Dozer to copy the data PatientPersistent -> Patient
    // This will load the Dozer spring bean and copy as mapped
    Patient copiedSerializableData = serializableBar.convertToPersistent(bar, factory);
}

I know this code does not work, but it's just to make my point. I would like to be able to convert the object to it's serializable form so that I can send it back to the client. That's why I would like to give the parent's type. Calling the mapper will always be the same thing, a source object and a Dest.class.

Maybe I'm just too confused with java.

Thanks

+1  A: 

Try: getClass().getSuperclass().toString()

fd
+6  A: 
getClass().getSuperclass()

But don't use this. It is certainly a sign of bad design.

Bozho
Ok well I won't use it. But can you please enlighten me and tell me why it's a bad idea?
code-gijoe
I still can't grasp entirely what you are trying to achieve. Could you include some more explanations in the question.
Bozho
I tried again and failed again, but I hope my explanation is clearer. Anyway all of this is just to avoid copy/paste (which is alway great).
code-gijoe
describe the whole object-flow - what is the client, what does it require, what it sends to server, and what should server send back
Bozho
The client is the UI remote station.1- Client sends Patient data via RPC (which means the patient is serializable).2- Server does some processing with this data. Let's say it saves new entry.3- After hitting the DB, Hibernate gives back a persistent object PatientPersistent which is not serializable and cannot be sent back to client.4- Use Dozer to transfer the PatientPersistent object to a serializable version.5- Return this object to the client.Not a perfect example but it's pretty close to my work flow.
code-gijoe
+3  A: 

Object.getClass() returns the runtime type of an object, so prepending super to the call doesn't really do much.

You could use getClass().getSuperclass().

Mike Daniels
+2  A: 

Nice... super.getClass() is actually Object's getClass(), which returns the runtime type of the instance it is invoked on (this in this case). Therefore you receive the same class...

Instead of asking for the runtime class of this using the super's implementation, You should ask for the super class of the class returned by getClass:

getClass().getSuperclass()

And by the way, what do you mean by "This will allow me to generalize some methods which I need to implement in each child which is awful."? Are you sure you have no other design choice?

Eyal Schneider
+1  A: 

I find a lot of time that solving a problem with "magic" in the way you are suggesting leads to a solution that is not very flexible.

If you are trying to get your super's class, what you probably want is to be calling a method in your parent's class that explicitly implements whatever action you were going to take after you looked up the parent's type.

Furthermore, there is a good chance that the design would be better yet if the method you were currently implementing were actually IN your parent class and that method was referring to an abstract method implemented in the child class--flipping your design on its head.

Of course I'm inferring this all from your desire to get the parent type and may be totally wrong, but I do think that if knowing your parent's class is the answer you're looking for then you are asking the wrong question--back up a few feet and look again.

Bill K