• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

restore Object class instance from container to its original type

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.

I`m writing simplest factory context that takes class name as String , analyze annotations of this class , instantiate this class , run needed methods of this instance and finally return this instance outside.
I also has requirements to implement destroy() method of the factory context, that will go through all spawned instances and run their respective destroy methods .

My current bottleneck is :
when i instantiate class instance from factory context, i save it inside HashMap in key field as Object and return it outside.
Later, when i need to run destroy on each instance, i`m going through HashMap keys , and at this point, i need to figure out what type each Object is to call its destroy() method.
Because without casting it to this type, i cannot see that such method eve exist.
My question is, how can i save type of the instance in any container in order to use it in the cast later ?

I`ll try to put code here .
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Gurianov wrote:
Later, when i need to run destroy on each instance, i`m going through HashMap keys , and at this point, i need to figure out what type each Object is to call its destroy() method.
Because without casting it to this type, i cannot see that such method eve exist.
My question is, how can i save type of the instance in any container in order to use it in the cast later ?



Can't you just use the instanceof operator to determine if the instance is of the type that you want to cast it to first?

Henry
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can't you just use the instanceof operator to determine if the instance is of the type that you want to cast it to first?



I do not know what is the type of the instance at this point, so there is nothing to compare with.
I thought about to keep in container Hashmap<Object o, String nameOfTheCLass> , but can i do cast with string as argument?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you have no idea what types you might get. But you think they have a destroy method? Why? Is there some interface they are supposed to implement?
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It looks like you have no idea what types you might get. But you think they have a destroy method? Why? Is there some interface they are supposed to implement?



Correct.

Line 43 of code tries to find @DestroyMethod annotation and put this method into list.
I havent implement this part yet , where if destroyMethods container is empty, ContextMain will do nothing at shutdown. This mean that class , that was passed to ContextMain, has no methods annotated with @DestroyMethod

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Personally, it would probably help us more if you provide us with your use-case, but since you don't seem to want to do that ...

Daniel Gurianov wrote:
I do not know what is the type of the instance at this point, so there is nothing to compare with.
I thought about to keep in container Hashmap<Object o, String nameOfTheCLass> , but can i do cast with string as argument?



Not directly, but very close. Take a look at the Java reflection library ... http://docs.oracle.com/javase/tutorial/reflect/

Given a class type, whose name is held in a string, you can get a Class instance for it. And with that Class instance, you can use the cast() method to try to cast any object to that class type.

Henry
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is beyond Beginner level so I'm moving the thread to Java In General
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everybody for the answers!

On subject : i ws not able to find how to cast Object to type that is in form of String. E.g. having HashMap with <Object,"TestClass"> get Object switch type to TestClass.

On solution of my code above.

Just found it in documentation

Reflection provides a means for invoking methods on a class. Typically, this would only be necessary if it is not possible to cast an instance of the class to the desired type in non-reflective code.
here

Exactly my case.

To run destroy method for each instance when context is shut down , it is enough to have HashMap<Object,HashMap<Integer,Method>>
where:

Object - spawned instance
Method - destroy method reflection instance , taken from class before instance created.
Integer - order of destroy method (which order destroy methods will be executed if there are more than one).


Finally , at the context shutdown, next happens inside context shutdown method :

 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic