Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] simple question

I agree that after returning advice should work.  I tested your code and it
works for me.

I think the discrepancy may be centered around "target" pointcuts.

I've modified your example slightly in a way that breaks it using a target
pointcut to capture the join point context.  Your example captured the
JComponent object just using after returning advice.  As I read the
Production Aspects section of the online documentation, this code should
work.  Have I read it wrong or misunderstood something?

import javax.swing.JTextField;
import javax.swing.JComponent;

public aspect AddToolTip  {
   pointcut newJ(JComponent c):
       call(JComponent+.new(..)) && target(c); // <-- This uses target to
capture context

   after(JComponent c) returning : newJ(c) {
       c.setToolTipText("OK");
       System.out.println("set tool tip");
   }

   public static void main(String args[]) {
       JTextField comp = new JTextField();
       System.out.println("text is "+comp.getToolTipText());

   }
}

Thanks in advance Ron (or whoever answers this post).

-----Original Message-----
From: Ron Bodkin [mailto:rbodkin@xxxxxxxxxxxxxx]
Sent: Monday, September 29, 2003 7:38 PM
To: aspectj-users@xxxxxxxxxxx
Subject: RE: [aspectj-users] simple question


David,

After returning advice should work, if you bind the result using after
returning (JComponent c), rather than trying to use target (which is always
null for a ctor).

Another possible problem could be if you had some other advice interacting
with this one, though that seems less likely.

If, in fact, the after returning approach didn't work, it would be a bug and
it would be helpful to post a standalone example that illustrates it.

Here's an example where after returning does work:

import javax.swing.JTextField;

public aspect AddToolTip {
   pointcut newJ():
       call(javax.swing.JComponent+.new(..));

   after() returning (javax.swing.JComponent c) : newJ() {
       c.setToolTipText("OK");        
       System.out.println("set tool tip");
   }

   public static void main(String args[]) {
       JTextField comp = new JTextField();
       System.out.println("text is "+comp.getToolTipText());

   }
}

Ron Bodkin
Chief Technology Officer
New Aspects of Security
m: (415) 509-2895

> ------------Original Message-------------
> From: "Costakos,David" <costakod@xxxxxxxx>
> To: "'aspectj-users@xxxxxxxxxxx'" <aspectj-users@xxxxxxxxxxx>
> Date: Mon, Sep-29-2003 12:45 PM
> Subject: RE: [aspectj-users] simple question
> 
> Having tried this out, I wasn't about to make this happen with before or
> after or after returning advice.
> 
> Around advice seemed to work though:
> 
>   pointcut newJ():
>     call(JComponent+.new(..));
> 
>   Object around(): newJ() {
>     Object o = proceed();
>     ((JComponent)o).setToolTipText("My Tip");
>     System.err.println("Set");
>     return o;
>   }
> 
> If anyone could explain why only around advice seemed to work, I'd
> appreciate it!
> 
> Thanks,
> 
> Dave.
> 
> -----Original Message-----
> From: Ron Bodkin [mailto:rbodkin@xxxxxxxxxxxxxx]
> Sent: Monday, September 29, 2003 1:16 PM
> To: aspectj-users@xxxxxxxxxxx
> Subject: Re: [aspectj-users] simple question
> 
> 
> Hi Dmitry,
> 
> You surely don't want before advice here; the given object hasn't been
> initialized at that point (and target is null). For a more complete
> discussion of the topic of construction join points and how to access
newly
> constructed objects, see the FAQ entries 10.6 and 10.7 at
>
http://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectj-home/doc/faq
> .html#q:initializationjoinpoints
> 
> Here's a stab at how you might write your aspect. If you need to set the
> tool tip "during" construction and not after, you probably will want to
> write a pointcut based on being withincode of your constructors.
> 
> public aspect AddToolTip {
>     pointcut newJ():
>         call(javax.swing.JComponent+.new(..));
> 
>     after() returning (javax.swing.JComponent c) : newJ(){
>         c.setToolTipText("OK");        
>     }
> }
> 
> Ron Bodkin
> Chief Technology Officer
> New Aspects of Security
> m: (415) 509-2895
> 
> > ------------Original Message-------------
> > From: =?koi8-r?Q?=22=E4=CD=C9=D4=D2=C9=CA=22=20?= <dmrzh@xxxxxxx>
> > To: aspectj-users@xxxxxxxxxxx
> > Date: Sun, Sep-28-2003 11:50 PM
> > Subject: [aspectj-users] simple question
> > 
> > I want write aspect for set ToolTip text for all comonents, but this
picks
> out no  join points.
> > Help me please.
> > 
> > import javax.swing.JComponent;
> > public aspect AddToolTip {
> > 	pointcut newJ(javax.swing.JComponent c):
> > 		call(javax.swing.JComponent+.new(..))&& target(c);
> > 	before(javax.swing.JComponent c) : newJ(c){
> > 		c.setToolTipText("OK");		
> > 	}
> > }
> > 
> > 
> > import javax.swing.*;
> > import java.awt.*;
> > public class MyFrame extends JFrame{
> > 	public static void main(String[] args){
> > 		MyFrame f=new MyFrame();		
> > 		f.setSize(300,300);
> > 		JPanel cp=new JPanel(new FlowLayout());
> > 		JButton b=new JButton("Ok");
> > 		cp.add(b);
> > 		f.setContentPane(cp);
> > 		f.show();
> > 	}
> > 
> > }
> > 
> > Dmitriy.
> > 
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > http://dev.eclipse.org/mailman/listinfo/aspectj-users
> > 
> > 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 
> 
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top