Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Forcing a class to implement a marker interface specified at run time.

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

I have a compiled JavaBean named "AClass" which may or may not implement some interfaces at compile-time. Regardless, I want to force this class at runtime to implement a marker interface of my choice. Is this possible? Can anybody steer me in the right direction? Can a new JVM langauge like Scala or Closure do this? Am I insane?

Thanks
Eric
 
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite sure what you mean. You can use the instanceof operator, or use the name of the marker interface as a type (in a parameter, for example). But that might not answer your question.
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I'm confused as well. If the class doesn't implement some interface at compile time, how do you propose forcing it to implement it at runtime?

There are libraries out there that allow you to take a class at runtime, add some methods to it (that implement an interface), and then have it declare that it implements the interface - is that what you're asking about? If so, a library like Javassist is called for.
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, it knew it sounded bizarre. That's why I asked. It would be nice to tell an instance at runtime to be a certain type. I'll look at Javassist. Thanks.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IIRC Spring can do this via Introduction Advice.
 
It's a tiny ad only because the water is so cold.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic