1 package eu.etaxonomy.cdm.aspectj;
3 import java.lang.reflect.Field;
4 import java.lang.reflect.InvocationTargetException;
6 import org.apache.log4j.Logger;
7 import org.aspectj.lang.Signature;
8 import eu.etaxonomy.cdm.model.common.CdmBase;
12 * Aspect class that adds a firePropertyChange call to all setter methods
13 * which names start with "set" and that belong to subclasses form CdmBase
14 * CdmBase defines the rest of the ProeprtyChangeSupport like listener registration
16 public aspect PropertyChangeAspect {
17 static Logger logger = Logger.getLogger(PropertyChangeAspect.class);
19 // pointcut execAdder(CdmBase cb): target(cb) && execution(void CdmBase+.add*(..) ); //once implemented we may want to remove addToSetWithChangeEvent and remove... from CdmBase
23 * Around aspect that will be weaven into the original setter methods of the CdmBase derived classes
25 after() returning(CdmBase cb): call(CdmBase+.new(..)) {
26 //logger.warn(" new instance aop " + cb.getClass().getName());
27 cb.fireOnCreateEvent(cb);
30 pointcut execSetter(CdmBase cb): target(cb) && execution(void CdmBase+.set*(..) );
31 // /** *********** OLD ***********************/
32 // pointcut callSetter( CdmBase b ) : call( * CdmBase+.set*(..) ) && target( b );
38 * Around aspect that will be weaven into the original setter methods of the CdmBase derived classes
40 void around( CdmBase cb ) : execSetter( cb ) {
41 //logger.setLevel(Level.DEBUG);
42 // get property that is set by setter method
43 Field property = getFieldOfSetter( thisJoinPointStaticPart.getSignature() );
47 property.setAccessible(true);
48 String propertyName = property.getName();
49 //logger.debug("execSetter: The property is ["+propertyName+"]");
51 // use property attribute directly, not through get method.
52 // get method might modify things, like setting a UUID when called for the first time.
53 // Also get methods for booleans start with "is" or "has"
54 Object oldValue = property.get(cb);
56 Object newValue = property.get(cb);
57 // logger.error ("Prop: " + propertyName);
58 // logger.warn("OLD:" + oldValue);
59 // logger.warn("New:" + newValue);
60 if (! isPersistentSet(newValue) && ! isPersistentSet(oldValue) ){
61 cb.firePropertyChange( propertyName, oldValue, newValue);
63 } catch (NoSuchMethodException e) {
66 }catch (IllegalArgumentException e) {
69 }catch (IllegalAccessException e) {
72 } catch (InvocationTargetException e) {
79 private boolean isPersistentSet(Object value){
81 //logger.debug("(null) is not PS");
83 }else if (value.getClass().getSimpleName().equals("PersistentSet")){
84 logger.debug("Don't throw event for setter of Persistent Set");
87 //logger.warn(value.getClass().getSimpleName() + " is is not PS");
95 * Return the Field object that belongs to the signature of a setter method
96 * If no matching attribute can be found return null instead of throwing an NoSuchFieldException
97 * Removes first 3 characters of method name to find property name
99 private Field getFieldOfSetter( Signature signature ){
101 String propertyName = "";
102 //logger.debug( "Getting the field name of setter ["+signature.getName() + "]" );
104 String methodName = signature.getName();
105 propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
106 field = signature.getDeclaringType().getDeclaredField( propertyName );
107 }catch( NoSuchFieldException e ){
109 propertyName = "is"+propertyName.substring(0, 1).toUpperCase()+ propertyName.substring(1);
110 field = signature.getDeclaringType().getDeclaredField( propertyName );
111 }catch( NoSuchFieldException nsfe ){
112 // can't find any matching attribute. catch error and return null
120 * Fires a propertyChange Event
121 * @param cb CdmBase that fires that Event
122 * @param property the property's name
123 * @param oldval the old value
124 * @param newval the new value
126 void fireLoggingPropertyChange( CdmBase cb,
131 //logger.debug( "PropertyChangeEvent: property [" + property + "], old value [" + oldval + "], new value [" + newval + "]");
132 // call firePropertyChange in original class. Method defined in CdmBase superclass!
133 cb.firePropertyChange( property,
134 ( oldval == null ) ? oldval : oldval.toString(),
135 ( newval == null ) ? newval : newval.toString());