Help coderanch get a
new server
by contributing to the fundraiser
  • 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

[AspectJ] Adding PropertyChangeEvents

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


My friend Alan Moore writes:
I've often thought about [how Jess prefers JavaBeans to] implement property change events. This *could* be implemented at runtime
by byte code weaving for existing objects that doesn't fire property change
events.


This is a really good idea if it would work. My question for you is, could it? AspectJ would have to
  • Add a member to a class (PropertyChangeSupport)
  • Record the argument to a method in a variable
  • Run the code for the method
  • Send an event using the PropertyChangeSupport object and the remembered variable


  • Additionally, you might want AspectJ to consider which methods to implement based on a call to java.beans.Introspector.introspect().
    Do you think this would be done?
     
    Author
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ernest,
    This can be quite easily accomplished using AspectJ. Here is the code:
    All one needs to do is include concrete aspects such as following. All the details are taken care in the reusable base aspect:

    The above aspect enables the required property change notification functionality in the Person class. You may use wildcards to enable multiple classes in one go. For example, the following declare statement would make all the classes in com.javaranch.beans to implement the property-change functionality:

    Here is a test class that exercises the solution:

    Here is the output when we compile all the sources and run the Test class.

    Here is the base aspect that does all the magic. You need to write such an aspect only once (or never, if you use the one I provide :-) ). Then you can include in any project and enable the desired functionality by writing simple subaspects like PersonBeanMakerAspect or JavaRanchBeanMakerAspect.
    You will need to be familiar with the AspectJ syntax to really understand what�s going on. Basically, we add the default implementation to BeanSupport interface and advise all set*() methods of classes implementing BeanSupport to fire an event.

    Here is a boring implementation of the Person class, for completeness:

    I hope this helps.
    -Ramnivas
    [ September 18, 2003: Message edited by: Ramnivas Laddad ]
     
    Ernest Friedman-Hill
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Great! I'll have to try this out soon!
     
    Onion rings are vegetable donuts. Taste this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic