• 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

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: 79178
377
  • 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: 43081
77
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic