(no commit message)
authorAndreas Müller <a.mueller@bgbm.org>
Tue, 22 Jan 2008 19:22:08 +0000 (19:22 +0000)
committerAndreas Müller <a.mueller@bgbm.org>
Tue, 22 Jan 2008 19:22:08 +0000 (19:22 +0000)
.gitattributes
cdmlib-model/src/main/java/eu/etaxonomy/cdm/aspectj/PropertyChangeAspect.aj [new file with mode: 0644]

index c9be4989d2c10f8d939befd758e165990f5fcf1f..6d16e633a2438159dc1f8c4531f393ac67c1455d 100644 (file)
@@ -69,6 +69,7 @@ cdmlib-commons/src/test/java/eu/etaxonomy/cdm/common/CdmUtilsTest.java -text
 cdmlib-commons/src/test/java/eu/etaxonomy/cdm/common/XmlHelpTest.java -text
 cdmlib-commons/src/test/java/eu/etaxonomy/cdm/test/suite/CdmTestSuite.java -text
 cdmlib-commons/src/test/java/eu/etaxonomy/cdm/test/unit/CdmUnitTestBase.java -text
+cdmlib-model/src/main/java/eu/etaxonomy/cdm/aspectj/PropertyChangeAspect.aj -text
 cdmlibrary/INSTALL.TXT -text
 cdmlibrary/pom.xml -text
 cdmlibrary/src/changes/changes.xml -text
diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/aspectj/PropertyChangeAspect.aj b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/aspectj/PropertyChangeAspect.aj
new file mode 100644 (file)
index 0000000..8215402
--- /dev/null
@@ -0,0 +1,106 @@
+package eu.etaxonomy.cdm.aspectj;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.aspectj.lang.Signature;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+
+/**
+ * @author markus
+ * Aspect class that adds a firePropertyChange call to all setter methods 
+ * which names start with "set" and that belong to subclasses form CdmBase
+ * CdmBase defines the rest of the ProeprtyChangeSupport like listener registration 
+ */
+public aspect PropertyChangeAspect {
+       static Logger logger = Logger.getLogger(PropertyChangeAspect.class);
+       
+       pointcut execSetter(CdmBase cb): target(cb) && execution(void CdmBase+.set*(..) );
+//     /** *********** OLD ***********************/
+//     pointcut callSetter( CdmBase b ) : call( * CdmBase+.set*(..) ) && target( b );
+
+       /**
+        * @param cb
+        * Around aspect that will be weaven into the original setter methods of the CdmBase derived classes
+        */
+       void around( CdmBase cb ) : execSetter( cb )  {
+               //logger.setLevel(Level.DEBUG);
+               // get property that is set by setter method
+               Field property = getFieldOfSetter( thisJoinPointStaticPart.getSignature() );
+               if (property==null){
+                       proceed( cb );
+               }else{                  
+                       property.setAccessible(true);
+                       String propertyName = property.getName();
+                       //logger.debug("execSetter: The property is ["+propertyName+"]");
+                       try {
+                               // use property attribute directly, not through get method.
+                               // get method might modify things, like setting a UUID when called for the first time.
+                               // Also get methods for booleans start with "is" or "has"
+                               Object oldValue = property.get(cb);
+                               proceed( cb );
+                               cb.firePropertyChange( propertyName, oldValue, property.get(cb));
+                       } catch (NoSuchMethodException e) {
+                               e.printStackTrace();
+                               proceed( cb );
+                       }catch (IllegalArgumentException e) {
+                               e.printStackTrace();
+                               proceed( cb );
+                       }catch (IllegalAccessException e) {
+                               e.printStackTrace();
+                               proceed( cb );
+                       } catch (InvocationTargetException e) {
+                               e.printStackTrace();
+                               proceed( cb );
+                       }
+               }
+       }
+
+
+               /**
+                * @param signature
+                * Return the Field object that belongs to the signature of a setter method
+                * If no matching attribute can be found return null instead of throwing an NoSuchFieldException
+                * Removes first 3 characters of method name to find property name
+                */
+               private Field getFieldOfSetter( Signature signature ){
+                       Field field = null;
+                       String propertyName = "";
+                       //logger.debug( "Getting the field name of setter ["+signature.getName() + "]" );
+                       try{
+                               String methodName = signature.getName();
+                               propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
+                               field = signature.getDeclaringType().getDeclaredField( propertyName );
+                       }catch( NoSuchFieldException e ){
+                               try{
+                                       propertyName = "is"+propertyName.substring(0, 1).toUpperCase()+ propertyName.substring(1);
+                                       field = signature.getDeclaringType().getDeclaredField( propertyName );
+                               }catch( NoSuchFieldException nsfe ){
+                                       // cant find any matching attribute. catch error and return null
+                                       return null;
+                               }
+                       }
+                       return field;
+               }       
+               /**
+                * Fires a propertyChange Event
+                * @param cb  CdmBase that fires that Event
+                * @param property the property's name
+                * @param oldval the old value
+                * @param newval the new value
+                 */
+               void fireLoggingPropertyChange( CdmBase cb,
+                               String property,
+                               Object oldval,
+                               Object newval) {
+
+                       //logger.debug( "PropertyChangeEvent: property [" + property + "], old value [" + oldval + "], new value [" + newval + "]");
+                       // call firePropertyChange in original class. Method defined in CdmBase superclass!
+                       cb.firePropertyChange( property,
+                                       ( oldval == null ) ? oldval : oldval.toString(),
+                                       ( newval == null ) ? newval : newval.toString());
+               }
+               
+}