(no commit message)
[cdmlib.git] / cdmlibrary / src / main / java / eu / etaxonomy / cdm / aspectj / PropertyChangeAspect.aj
1 package eu.etaxonomy.cdm.aspectj;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.InvocationTargetException;
5
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;
10
11 /**
12 * @author markus
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
16 */
17 public aspect PropertyChangeAspect {
18 static Logger logger = Logger.getLogger(PropertyChangeAspect.class);
19
20 pointcut execSetter(CdmBase cb): target(cb) && execution(void CdmBase+.set*(..) );
21 // /** *********** OLD ***********************/
22 // pointcut callSetter( CdmBase b ) : call( * CdmBase+.set*(..) ) && target( b );
23
24 /**
25 * @param cb
26 * Around aspect that will be weaven into the original setter methods of the CdmBase derived classes
27 */
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+"]");
35 try {
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);
40 proceed( cb );
41 cb.firePropertyChange( propertyName, oldValue, property.get(cb));
42 } catch (NoSuchMethodException e) {
43 e.printStackTrace();
44 proceed( cb );
45 }catch (IllegalArgumentException e) {
46 e.printStackTrace();
47 proceed( cb );
48 }catch (IllegalAccessException e) {
49 e.printStackTrace();
50 proceed( cb );
51 } catch (InvocationTargetException e) {
52 e.printStackTrace();
53 proceed( cb );
54 }
55 }
56
57
58 /**
59 * @param signature
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
62 */
63 private Field getFieldOfSetter( Signature signature ){
64 Field field = null;
65 String propertyName = "";
66 //logger.debug( "Getting the field name of setter ["+signature.getName() + "]" );
67 try{
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 ){
72 try{
73 propertyName = "is"+propertyName.substring(0, 1).toUpperCase()+ propertyName.substring(1);
74 field = signature.getDeclaringType().getDeclaredField( propertyName );
75 }catch( NoSuchFieldException nsfe ){
76 nsfe.printStackTrace();
77 }
78 }
79 return field;
80 }
81 /**
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
87 */
88 void fireLoggingPropertyChange( CdmBase cb,
89 String property,
90 Object oldval,
91 Object newval) {
92
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());
98 }
99
100 }