Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Advice around with fields

Thank you Adrian, now it is clear. 
 
Ulises J. M.
 
 
-----Original Message-----
From: Adrian Colyer <adrian_colyer@xxxxxxxxxx>
To: aspectj-users@xxxxxxxxxxx
Sent: Sun, 26 Jun 2005 11:03:48 +0100
Subject: Re: [aspectj-users] Advice around with fields


The advice *is* running after the get join point for c.i. Your System.out.println however runs after the get of c.i, and the building of a string buffer to print it.

The _expression_ System.out.println("=" + c.i + "=");

is a shorthand way of saying something like this (Java 5 example):

StringBuilder sb = new StringBuilder();
sb.append("=");
sb.append(c.i);
sb.append("=");
System.out.println(sb.toString());

(try compiling Campo.java with a java 5 compiler and then using javap -verbose to look at the result)

If now interleave the advice  you get:
 
sb.append("=");
sb.append(
            ==> before advice
        ==> around advice {
                    c.i
                  }
            ==> after advice
);
sb.append("=");
System.out.println(sb.toString());

which should explain the output you are seeing.

If you change your program to :

System.out.print("=");
System.out.print(c.i);
System.out.println("=");

you should see the output you were originally expecting.

[As an aside, it's interesting to note that there is no advantage in handcoding StringBuffer/Builder calls in many simple cases any more since the code the compiler generates is just as good.]
-- Adrian
Adrian_Colyer@xxxxxxxxxx



ujuarez71@xxxxxxxxxxxx
Sent by: aspectj-users-bounces@xxxxxxxxxxx
23/06/2005 01:01
Please respond to
aspectj-users@xxxxxxxxxxx

To
aspectj-users@xxxxxxxxxxx
cc
Subject
[aspectj-users] Advice around with fields





Hi.
 
I have the next code for my class:
 
public class Campo {
int i = 5;

public static void main(String[] args) {
 Campo c = new Campo();
 System.out.println("=" + c.i + "=");
}
}

 
and the following aspect:
 
public aspect CampoAspect {
pointcut campo():
 get(int Campo.i);

before(): campo() {
 System.out.println("before: " + thisJoinPoint);
}
int around(): campo() {
 System.out.println("around: " + thisJoinPoint);
 return 34;
}
after(): campo() {
 System.out.println("after : " + thisJoinPoint);
}
}

 
The output is:
 
before: get(int Campo.i)
around: get(int Campo.i)
after : get(int Campo.i)
=34=

 
I confused. All advices work "before", could you tell me why? What's incorrect?
I thought my output would be:
 
=before: get(int Campo.i)
around: get(int Campo.i)34
after : get(int Campo.i)=

 
Regards.
Ulises J. M.

Switch to Netscape Internet Service.
As low as $9.95 a month -- Sign up today at
http://isp.netscape.com/register
 
Netscape. Just the Net You Need.
 
New! Netscape Toolbar for Internet Explorer
Search from anywhere on the Web and block those annoying pop-ups.
Download now at
http://channels.netscape.com/ns/search/install.jsp _______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Switch to Netscape Internet Service.
As low as $9.95 a month -- Sign up today at http://isp.netscape.com/register
 
Netscape. Just the Net You Need.
 
New! Netscape Toolbar for Internet Explorer
Search from anywhere on the Web and block those annoying pop-ups.
Download now at http://channels.netscape.com/ns/search/install.jsp

Back to the top