Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic casting & serialised objects 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I'm at the tail end of my course and it touched briefly on the serializable interface that enables objects to be saved to file as binary data.

Don't worry, what I'm about to ask has nothing to do with my course, the assignemts are done and i'm just waitng for the exam!

Right now that the caveat is out the way....

part of the info on serialised objects was that you can use getClass to obtain an objects class so you don't even need to know what types of object are stored.

That's all they said on the subject and no examples were given, so I thought I'd have a play myself as I found this intriguing. However I have been unable to get any code to compile. I sent my example to my tutor but they have also been unable to come up with a working solution and so hoped you guys could.

Senario...

You have a binary file of serialised objects, they were stored as a Set of objects but you don't know what class they are, you just know they can all understand the method mySpecialMethod and you wish to execute it against each object.

As you don't know the class of each set element, I'm guessing you have to use class Object to load them... however if I then try to cast the object to then execute the required method I get a complile error...
cannot find symbol - method mySpecialMethod()

Which is telling me the compiler is not seing the cast take place and so is unable to get access to the correct class's message protocol, how would you do the following and get it to compile...

Code:
   public void myTest()
   {

      Set<Object> recoveredSet = new HashSet<Object>((Set)ObjectIO.retrieveObject());
           
      for(Object obj : recoveredSet)
      {         
         System.out.println(obj.getClass().cast(obj).mySpecialMethod());
      }

   }

Is it possible to dynamically cast objects and then run instance methods from their class protocol?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
[lol] there I go again asking questions people can't answer. It's a gift!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
mySpecialMethod is a method of your class, in your code the only class mentioned is Object, so I will get back the question to you:

If you were the compiler, how would yo be able to check that method can actually be invoked at compile time?

Is there a way to do that? Yes, reflection.

Cheers,
Dian
 
So when the course book talked about being able to use getClass() and not needing to know what class the serialised objects are, was basically talking rubbish?

If you were the compiler, how would yo be able to check that method can actually be invoked at compile time?
You can't at compile time, that's my whole point of the question, the cast is 'run-time'. The cast is done dynamically and can only be worked out when the code runs.

As we have not been taught anything about reflection, I'll just consider the book to be talking rubbish and forget about it, I don't want to clog my mind up with stuff not relevant to the course.

The last bit i'm reading is about TDD (Test Driven Development), how on earth you are meant to start the recording feature and create test classes against classes, objects and methods that don't exist is beyond me.

How can I write a test against a method and write the assertions in JUnit, if the method hasn't been written?

The test won't compile because it is trying to send a message send to an object where the method doesn't exist.

There are a few concepts on this course I just don't get, but like everything in life, you can't know or understand everything about everything!







"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
part of the info on serialised objects was that you can use getClass to obtain an objects class so you don't even need to know what types of object are stored

That's true, you can instantiate the object without knowing the type, but you need to know it to invoke methods on it.

Cheers,
Dian
 
That's what I thought Dian.

If you don't kow the object type you are loading, how would you know what methods are available once you've loaded the object.

Perhaps I took their description too literal or was thinking further outside the box than was expected.

Not knowing the type of data you have isn't of much help is it!

The best the tutor could come up with was...
Code:
   public void myTest()
   {
      Object anObj = ObjectIO.retrieveObject();
      Set<myClass> recoveredSet = new
HashSet<myClass>((HashSet<myClass>)anObj);
      for(myClassobj : recoveredSet)
      {
         System.out.println(obj.myMethod());
       }
   }

But even that produces a compliler warning and you still need to know that it is of class myClass.

I think I think too much sometimes [lol]







"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Well, this that nothing to do with Java or OO: if you get a thing, you cannot drive it unless you know the thing is a car or a bike.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top