1 package eu.etaxonomy.cdm.aspectj;
3 import java.lang.reflect.Field;
4 import java.lang.reflect.InvocationTargetException;
6 import org.apache.log4j.Level;
7 import org.apache.log4j.Logger;
8 import org.aspectj.lang.Signature;
9 import eu.etaxonomy.cdm.model.common.CdmBase;
13 * Aspect class that adds a firePropertyChange call to all setter methods
14 * which names start with "set" and that belong to subclasses form CdmBase
15 * CdmBase defines the rest of the ProeprtyChangeSupport like listener registration
17 public aspect PropertyChangeAspect {
18 static Logger logger = Logger.getLogger(PropertyChangeAspect.class);
20 pointcut execSetter(CdmBase cb): target(cb) && execution(void CdmBase+.set*(..) );
21 // /** *********** OLD ***********************/
22 // pointcut callSetter( CdmBase b ) : call( * CdmBase+.set*(..) ) && target( b );
26 * Around aspect that will be weaven into the original setter methods of the CdmBase derived classes
28 void around( CdmBase cb ) : execSetter( cb ) {
29 //logger.setLevel(Level.DEBUG);
30 // get property that is set by setter method
31 Field property = getFieldOfSetter( thisJoinPointStaticPart.getSignature() );
32 property.setAccessible(true);
33 String propertyName = property.getName();
34 //logger.debug("execSetter: The property is ["+propertyName+"]");
36 // use property attribute directly, not through get method.
37 // get method might modify things, like setting a UUID when called for the first time.
38 // Also get methods for booleans start with "is" or "has"
39 Object oldValue = property.get(cb);
41 cb.firePropertyChange( propertyName, oldValue, property.get(cb));
42 } catch (NoSuchMethodException e) {
45 }catch (IllegalArgumentException e) {
48 }catch (IllegalAccessException e) {
51 } catch (InvocationTargetException e) {
60 * Return the Field object that belongs to the signature of a setter method
61 * Removes first 3 characters of method name to find property name
63 private Field getFieldOfSetter( Signature signature ){
65 String propertyName = "";
66 //logger.debug( "Getting the field name of setter ["+signature.getName() + "]" );
68 String methodName = signature.getName();
69 propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
70 field = signature.getDeclaringType().getDeclaredField( propertyName );
71 }catch( NoSuchFieldException e ){
73 propertyName = "is"+propertyName.substring(0, 1).toUpperCase()+ propertyName.substring(1);
74 field = signature.getDeclaringType().getDeclaredField( propertyName );
75 }catch( NoSuchFieldException nsfe ){
76 nsfe.printStackTrace();
82 * Fires a propertyChange Event
83 * @param cb CdmBase that fires that Event
84 * @param property the property's name
85 * @param oldval the old value
86 * @param newval the new value
88 void fireLoggingPropertyChange( CdmBase cb,
93 //logger.debug( "PropertyChangeEvent: property [" + property + "], old value [" + oldval + "], new value [" + newval + "]");
94 // call firePropertyChange in original class. Method defined in CdmBase superclass!
95 cb.firePropertyChange( property,
96 ( oldval == null ) ? oldval : oldval.toString(),
97 ( newval == null ) ? newval : newval.toString());