10 Replies - 876 Views - Last Post: 11 January 2008 - 05:05 AM Rate Topic: -----

Topic Sponsor:

#1 jowharshamshiri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 24-December 07

access to unknown objects

Posted 10 January 2008 - 05:17 AM

hi guys.
heres the situation im in.
_im given an object.
_i know that it is an instance of one of a number of classes i have at hand.
_i need to extract all of the attributes and write them to a file like:
...
attr#32=1
attr#33="hello"
...
_i do this because i need to recreate that object some where else. in my system an object equals an other object if they instanciate the same class and contain the same values.

heres what ive done:
_i check to find which class does this object instanciate.
_according to that i pass it to a predefined method that extracts all of the attributes of the objects of that class and saves them.

heres whats wrong with my solution:
_i am limited to a number of classes.
_if i want to do the same thing for the objects of a new class i will have to modify my source code and add a method for them.

heres what i need.
_a universal method that enables me to have access to any known or unknown object i have.
_or what ever other solution you guys can think of.

This post has been edited by jowharshamshiri: 10 January 2008 - 05:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: access to unknown objects

#2 babasmith  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-April 06

Re: access to unknown objects

Posted 10 January 2008 - 07:29 AM

Did you consider Reflection ?
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: access to unknown objects

Posted 10 January 2008 - 07:56 AM

Or serialization, or RMI, etc. They all do quite similar things.
Was This Post Helpful? 0
  • +
  • -

#4 AngeluS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 231
  • Joined: 11-April 07

Re: access to unknown objects

Posted 10 January 2008 - 08:51 AM

Reflection and Serialization; aren't they two totally different things?!
Was This Post Helpful? 0
  • +
  • -

#5 jowharshamshiri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 24-December 07

Re: access to unknown objects

Posted 10 January 2008 - 10:01 AM

i checked the sun tutorials and according to that reflection seems like just what i needed but im gonna have to read up on it. so thanks to you all. youre always helpful.

This post has been edited by jowharshamshiri: 10 January 2008 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#6 jowharshamshiri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 24-December 07

Re: access to unknown objects

Posted 10 January 2008 - 12:09 PM

wow! where does this java thing end for heavens sake?
i checked serialization. it doesnt do what i wanted but it opens a whole new window of opportunity. to send an object over sockets? thats like a dream come true.

This post has been edited by jowharshamshiri: 10 January 2008 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#7 jowharshamshiri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 24-December 07

Re: access to unknown objects

Posted 10 January 2008 - 01:34 PM

i cant use reflection cause then i still wouldnt have access to the private and protected fields. and unfortunately these too are part of the identity of an object.
on to serialization:
this is quoted from http://java.sun.com/.../serialization/

"The bottom line: serialization does not care about access modifiers such as private -- all nontransient fields are considered part of an object's persistent state and are eligible for persistence. "

so i guess i found what i wanted(thanks to 1lacca). and the cool thing is you can send the flattened object(i.e. the object state saved as a stream of bytes) over the sockets and use it to recreate the object some where else! the only thing is it just saves the "object state" not the class it instantiates so you must have the class present where ever you want to recreate the object. basically this serialization hides all of the hassle in the process. im lovin' it.

This post has been edited by jowharshamshiri: 10 January 2008 - 01:39 PM

Was This Post Helpful? 0
  • +
  • -

#8 jowharshamshiri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 24-December 07

Re: access to unknown objects

Posted 10 January 2008 - 05:21 PM

this is getting too long but i did implement a sample system with serialization and it does work. but when i look at what i ve done now(that is send a request to server in the form of an object and recieve a response in the same form) i see that rmi does the same thing smoother. so im going to be switching it all to rmi.

and 1lacca you did put "etc" in your sentence. what other technique is there?
Was This Post Helpful? 0
  • +
  • -

#9 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: access to unknown objects

Posted 11 January 2008 - 03:40 AM

View Postjowharshamshiri, on 11 Jan, 2008 - 01:21 AM, said:

this is getting too long but i did implement a sample system with serialization and it does work. but when i look at what i ve done now(that is send a request to server in the form of an object and recieve a response in the same form) i see that rmi does the same thing smoother. so im going to be switching it all to rmi.

and 1lacca you did put "etc" in your sentence. what other technique is there?


I've put etc because there are many solutions in this area, all of them with different advantages and disadvantages. Since I'm not sure about your ultimate goal, I didn't know which one would be the best, so I've only mentioned the two most general one, and left alone the more "heavy weight" ones, like CORBA(probably way too complicated for your project, and RMI does it just fine), EJBs(but if you use a database Hibernate might also worth a look) and other things like that, but I was sure, that if you start googling those basic ones you would stumble upon them. On the other side of these things are ObjectOutputStream and ObjectInputStream that use serialization as well. And there are a lot more...

Quote

i cant use reflection cause then i still wouldnt have access to the private and protected fields. and unfortunately these too are part of the identity of an object.

You can, check the setAccessible method of the Field class.
Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: access to unknown objects

Posted 11 January 2008 - 03:46 AM

View PostAngeluS, on 10 Jan, 2008 - 04:51 PM, said:

Reflection and Serialization; aren't they two totally different things?!

Yes, they are different things. However nobody wrote that reflection is similar to serialization ;) The idea was that these are used to do similar things to what the op asked for, but my wording really twisted it (looks-like that day my mind was on vacation...)
Was This Post Helpful? 0
  • +
  • -

#11 jowharshamshiri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 24-December 07

Re: access to unknown objects

Posted 11 January 2008 - 05:05 AM

Quote

You can, check the setAccessible method of the Field class.


yes youre right. i should have looked deeper into it. this is a sample that exposes all of the fields of a given object.

import java.lang.reflect.*;
public class Exposer {
	public void expose(Object object){
		try{
			Class c = object.getClass();
			System.out.println(c + " {");
			Field fields[] = c.getDeclaredFields();
			System.out.println("\t<fields>");
			for (Field f : fields) {
					f.setAccessible(true);
					System.out.println("\t" + f + " = " + f.get(object));
			}
			System.out.println("\t</fields>");
			System.out.println("}");
		}catch(Exception e){System.out.println(e);}
	}
	public static void main(String[] args) {
		Exposer s=new Exposer(); 
		s.expose("sample string"); 
	}
}


This post has been edited by jowharshamshiri: 11 January 2008 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1