Jump to content
  • Advertisement
Sign in to follow this  
Kaze

[java] java reflection + dynamic cast

This topic is 4284 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

so basically for a project im working on im trying to create a plugin system, to do this im loading the class as a class object from a class file, then using the java reflection api to see what methods it has and call them. My question, is there any way i can cast a object as a interface that it dosen't implement but i have confirmed it has all the required methods EDIT: I could create a dummy class that implement the interface and invokes the appropriate method object but this is a rather messy solution

Share this post


Link to post
Share on other sites
Advertisement
Probably not. You can, however, create a wrapper which implements the interface and forwards calls through reflection (if you have control over what the interface is).

Share this post


Link to post
Share on other sites
thanks anyway, i was hopping there was a better solution but it shouldn't be too hard either way

Share this post


Link to post
Share on other sites
No, it is not possible. But why would you want that? Clarify your reasons, and we might be able to suggest something for you [grin]

Share this post


Link to post
Share on other sites
The project is for a encryption application
basically the project already has a working release and can use different algorithms stored in there own class that implement a interface, I want to add the ability to dynamically load new algorithms from external classes at runtime, i need it to be in the form of a object cast a the interface to avoid major re-coding. A class that stores methods and implements the interface isn't too bad of a solution, i was just curious if there was a better way.

Share this post


Link to post
Share on other sites
I know what you mean, but unfortunately, all the possible workarounds for that that I can think of are not worth the trouble if compared to the classic solution. =[

Share this post


Link to post
Share on other sites
Make an adapter class which implements the required interface and calls the preexisting module's functions. (Requires a new adapter class per real class, but if you're dealing with a preexisting body of code that's a finite number.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Make an adapter class which implements the required interface and calls the preexisting module's functions. (Requires a new adapter class per real class, but if you're dealing with a preexisting body of code that's a finite number.)

Or generate one using a bytecode assembler (like BCEL).
Or write a custom classloader that transforms thoses classes by adding the interface to that class (since it alrady implements it with its functions).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!