• 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

dynamic type casting

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

i need to know how to get the class dynamically or type cast to a dynamic class

below is the scenario

I have a class A. Class A generates two classes B and C (for example) depending on the input given to class A.
The class of B is constant and class name of C is dynamically generated. Class B has getter and setter for Class C, which returns Object (not C).

Now in class A I create a instance of Class B and access the getter of class C using reflection (I have the class Name in a prop file).

Since the getter method returns object, I am not able to access the methods of class C.

I need to dynamically typecast the object to Class c. I have the class name as String,but running out of ideas to do it.


Any suggestion/idea is most welcome

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shasi Sekar,

I've not understood your question properly.

Class A generates two classes B and C


How? What do you mean by Class A 'generates' class B & C?

The class of B is constant


Again, what is meaning of a class being 'constant'? (and what is class 'of' B?)

Class B has getter and setter for Class C


Getter and setters are for instance variables. Here, you are simply creating new instance of C, invoking a method of C, which again creates new instance of C. So, technically, it is not a getter method.

I need to dynamically typecast the object to Class c. I have the class name as String,but running out of ideas to do it.


So, the class name is not C? Am I missing anything here? Because if class name is not C, then class B is not even gonna be compile (because a method of class B creates an object of class C).

Apart from this, what my understanding of your issue is - there is a String in your property file, that String is a class name, you want to create instance of that class and invoke some methods on it.

Well, it can very well be done by using Reflection APIs. Please check out classes like Class, Method etc.
Also, Oracle's tutorial on reflection is a good read.

Also, I would suggest to completely eliminate class B (if its only job is to give you object of Class C, then you can do it directly in Class A)

I hope this helps.
 
Shasi Sekar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your immediate reply

"Class A generates two classes B and C" --> my typo .... it creates instances. Also the instance of C can only be got from B.

"The class of B is constant" --> this too is a typo --> should be "The class name of B is constant"

The last part is right,

class name of C is not constant. it might be D or E.

Both B and C would compile because Class B and C are generated dynamically where class B has the info of class C or D ....

As you said reflection is right. and i saw the methods Class and Method.

but if i say class.newInstance(); it returns an object ,

here is my problem..... after getting the instance thro' invoke method or newInstance .... i need to typecast it to class C

I have the class name as C in a string object, That is it
String classname = "C"
ie. if i say

Object x = (classname)class.newInstance();
classname is not resolved to a type.

My hurdle here is type casting.

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Even if you do this, 'x' is still reference of type Object, and you are not gonna be able to invoke methods of 'classname' (or C, or D, or whatever) on x

To be able to do that, x has to be a reference of type 'classname'. How are you gonna code for it?

My hurdle here is type casting.


Let's get to the basics - why do you need to cast it at all? To invoke methods of that 'classname', right?

Well, here comes Method class into picture.

Hint : Take a close look at 'invoke' method of Method class (invoke is the name of method, and Method is the name of class - funny, isn't it?)

I hope this helps.
 
Shasi Sekar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Anayonkar,

I figured it out. i was missing it to get the methods for the second time from the instance that is got.

Thanks for the missing link
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My little nephew sometimes runs around with scissors. I usually take those away from him so he doesn't hurt himself or his little brother.

So, Shasi, what you're doing is fine if you're just experimenting with the reflection API and all. Otherwise, what is it you're actually trying to do, besides the obvious? Sounds to me like it could lead to you hurting yourself or others. Just a little concerned about code safety. Java reflection and introspection can be a dangerous weapon when not wielded judiciously and with extreme prudence.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shasi Sekar wrote:thank you Anayonkar,

I figured it out. i was missing it to get the methods for the second time from the instance that is got.

Thanks for the missing link


You are welcome.

However, as mentioned by Junilu Lacar, reflection is very very powerful tool - and one should be extremely careful while using it.
 
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic