There's nothing special here--just a single method, isModified(), indicating whether an object has
been modified.
Creating a Mixin
The next step is to create the code that implements IsModified and that is introduced to the objects;
this is referred to as a mixin. As we mentioned earlier, it is much simpler to create mixins by
subclassing DelegatingIntroductionInterceptor than to create one by directly implementing the
IntroductionInterceptor interface. Our mixin class, IsModifiedMixin, subclasses
DelegatingIntroductionInterceptor and also implements the IsModified interface. This is shown in
Listing 7-7.
Listing 7-7. The IsModifiedMixin Class
package com.apress.prospring3.ch7.introductions;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
public class IsModifiedMixin extends DelegatingIntroductionInterceptor
implements IsModified {
private boolean isModified = false;
private Map<Method, Method> methodCache = new HashMap<Method, Method>();
public boolean isModified() {
return isModified;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
if (!isModified) {
if ((invocation.getMethod().getName().startsWith("set"))
&& (invocation.getArguments().length == 1)) {
// invoke the corresponding get method to see
// if the value has actually changed
Method getter = getGetter(invocation.getMethod());
if (getter != null) {
// modification check is unimportant
// for write only methods
Object newVal = invocation.getArguments()[0];
Object oldVal = getter.invoke(invocation.getThis(), null);
if((newVal == null) && (oldVal == null)) {
isModified = false;
} else if((newVal == null) && (oldVal != null)) {
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home