Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Static crosscutting

> For a reusable version of bean support, please look at
> a solution that I posted to a JavaRanch forum:
>
>
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=14&t=000304


Thanks. I had a look at your code and implemented a similar solution,
which unfortunately doesn't quite work. One of my requirements is that
the interface that allows addition of listeners must be useable from
editors that are not aspect-aware. Therefore, after adding the
possibility to register change listeners to a class called Taxon, I
can't require developers to write:

  taxon.addPropertyChangeListener(...);

because this would only compile with ajc. Or this:

  taxon.ajc$...$pcs.addPropertyChangeListener(...);

not only because it is way too ugly, but it would also fail if the
imported library happens to not be postprocessed. On the other hand
everybody could do this:

  if (object instanceof BeanSupport)
    ((BeanSupport) taxon).addPropertyChangeListener(...);

Any suggestions how to accomplish this? Here is my approach, which works
except that after applying it I get a compile time error ("The method
addPropertyChangeListener is undefined for the type BeanSupport"). The
BeanSupport interface is a normal interface that declares methods such
as addPropertyChangeListener.

package org.expasy.aspects;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.expasy.models.util.BeanSupport;
import org.expasy.models.taxonomy.Taxon;

public aspect PropertyChange
{
  declare parents: Taxon implements BeanSupport;

  private PropertyChangeSupport BeanSupport.pcs =
    new PropertyChangeSupport(this);

  pointcut setter(BeanSupport source, Object value):
    execution(void BeanSupport+.set*(*))
      && target(source) && args(value);

  after(BeanSupport source, Object value): setter(source, value)
  {
    String property = thisJoinPointStaticPart.getSignature()
      .getName().substring(3).toLowerCase();
    source.firePropertyChange(property, null, value);
 }

  public void
BeanSupport.addPropertyChangeListener(PropertyChangeListener listener)
  {
    pcs.addPropertyChangeListener(listener);
  }

  public void
BeanSupport.removePropertyChangeListener(PropertyChangeListener
listener)
  {
    pcs.removePropertyChangeListener(listener);
  }

  public void BeanSupport.firePropertyChange(String name, Object
original, Object modified)
  {
    pcs.firePropertyChange(name, original, modified);
  }
}


--
Eric Jain




Back to the top