Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] inter-type declaration problem

Hi,

today I tried for the first time in my life to use inter-type feature. 
And I did it (well, almost...)

I use AspectJ 1.5.3 with annotations.

What I wanted to do is to add a field and get/set method to few classes. 
I did it like this and it works (the snippet below shows it for one 
class only, but that's enough to see what's going on):

@Aspect
public class CSFUrlableInterType {
   public static class CSFUrlableImpl
         implements CSFUrlable {

         private String urlParams;

         /* @see pl.eo.apps.mops.form.CSFUrlable#getUrlParams() */
         public String getUrlParams() {
             return urlParams;
         }

         /* @see 
pl.eo.apps.mops.form.CSFUrlable#setUrlParams(java.lang.String) */
         public void setUrlParams( String urlParams ) {
             this.urlParams = urlParams;
         }

     }

     @DeclareParents(value = "pl.eo.apps.mops.form.FormClientDetails", 
defaultImpl = CSFUrlableImpl.class)
     private CSFUrlable csfUrlableFormClientDetails;
}

It works ! Now I can do sth like this in my controllers:
   FormClientDetails formClientDetails = new FormClientDetails();
   ...
   ClientSearchForm csForm = new ClientSearchForm();
   csForm.setCrudDAO( crudDAO );
   csForm.buildFromRequest( request );
   // formClientDetails doesn't have setUrlParams method, but thanks to 
aspects it has it !
   ( (CSFUrlable) formClientDetails ).setUrlParams( csForm.getUrlParams() );
which is exactly what I wanted !

But I realised, that I'll have to put the last four lines seen above in 
many controllers. So I decided to put it into aspect as well. I tried to 
do it like this:
  @Around("execution(protected * 
x.y.z.CurrentContactDetails.formBackingObject(HttpServletRequest)) && 
args(request)")
     public Object addCsfUrlParamsToFormCurrentContactDetails( 
ProceedingJoinPoint thisJoinPoint, HttpServletRequest request ) {
         ClientSearchForm csForm = new ClientSearchForm();
         csForm.setCrudDAO( ( (CurrentContactDetails) ( 
thisJoinPoint.getThis() ) ).getCrudDAO() );
         csForm.buildFromRequest( request );
         String urlParams = csForm.getUrlParams();
         log.info( "url params: " + urlParams );
         Object obj = null;
         try {
             obj = thisJoinPoint.proceed();
             log.info( "obj: " + obj );
             FormClientDetails fcd = (FormClientDetails) obj;
             log.info( "fcd: " + fcd );
             CSFUrlable csf = (CSFUrlable) fcd;
             log.info( "csf: " + csf );
             ( (CSFUrlable) obj ).setUrlParams( urlParams );
             log.info( "url params: " + ( (CSFUrlable) obj 
).getUrlParams() );
         }
         catch ( Throwable e ) {
             e.printStackTrace();
         }
         return obj;
     }

After executing it, log says:
   url params: f_fname=a
   obj: pl.eo.apps.mops.form.FormClientDetails@ed3007
   fcd: pl.eo.apps.mops.form.FormClientDetails@ed3007
   csf: pl.eo.apps.mops.form.FormClientDetails@ed3007
   url params: null // WHY ?! WHY ?!

Everything goes fine till this line:
( (CSFUrlable) obj ).setUrlParams( urlParams );
but then... I set the value of the field and it looks fine, but then I 
read it and get null instead of expected value.

I tried to use debugger (under Eclipse) but it's hard to see anything 
(or I don't know what to look at).

Please, help - I'm lost. I feel the great things are so close, but I 
need help to make the next step.

yours sincerely
Tomek Kaczanowski
http://kaczanowscy.pl/tomek


Back to the top