Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] around aspects on fields

Hi, Somik,

You're having the problems you describe because you're using
the target pointcut, and it doesn't look like you want to.
For example, here's one of your pointcuts:

    pointcut getVarInt(int whatever): 
        get(* Person.age) && target(whatever);

This matches any get of the Person.age field when the object
you're getting it from (the target) is an int, and publishes
that int as "whatever". 

Clearly, this isn't what you mean.  In fact, if you look at
your code, you never even use "whatever" -- you don't care
what the target object is, you just want the events to be
triggered on a field get.  Try this simpler aspect:

    aspect A {
        pointcut getVar(): get(* Person.name);
        pointcut getVarInt(): get(* Person.age);

        int around(): getVarInt() {
            System.out.println("in around(int)");
            return -1;
        }
        Object around(): getVar() {
            System.out.println("in around");
            return "HELLO";
        }
    }

If you really wanted to expose the target object (which is
definitely a Person object), try this:

    aspect A {
        pointcut getVar(Person whatever): 
            get(* Person.name) && target(whatever);
        pointcut getVarInt(Person whatever): 
            get(* Person.age) && target(whatever);

        int around(Person whatever): getVarInt(whatever) {
            System.out.println("in around(int) on" 
                               + whatever);
            return -1;
        }
        Object around(Person whatever): getVar(whatever) {
            System.out.println("in around on"
                               + whatever);
            return "HELLO";
        }
    }

Let me know if this doesn't work for you.

-erik

-----Original Message-----
From: Somik Raha [mailto:somik@xxxxxxxxxxxxxxxxxxx] 
Sent: Wednesday, 22 January, 2003 10:20 pm
Subject: [aspectj-users] around aspects on fields

Hi,

I was experimenting with aspectJ - couldnt get it to run on
Eclipse 2.1, so got the older 2.0. I was trying this :

    aspect A {
        pointcut getVar(Object whatever): 
            get(* Person.name) && target(whatever);
        pointcut getVarInt(int whatever): 
            get(* Person.age) && target(whatever);

        int around(int whatever): getVarInt(whatever) {
            System.out.println("in around(int)");
            return -1;
        }
        Object around(Object whatever): getVar(whatever) {
            System.out.println("in around");
            return "HELLO";
        }
    }

where the Person class is :

    public class Person {
        public int age;
        public String name;
    }

Here is a sample testcase which I started with, in order to
"exercise" the aspects :

    public class VarAspectTest extends TestCase {
        public VarAspectTest(String arg0) {
            super(arg0);
        }
        public void testVarGet() {
            System.out.println("Accessing Person.name..." 
                               + new Person().name);
            System.out.println("Accessing Person.age..." 
                               + new Person().age);
        }
    }

The result seems weird- although I am able to get "HELLO" as
the name, the age shows up as 0 - i.e. the aspect on the int
member wasn't triggered.

Next, I tried :

    pointcut getVar(Object whatever): 
        get(* Person.String) && target(whatever);
    pointcut getVarInt(int whatever): 
        get(* Person.int) && target(whatever);

This too had the same results. I'm actually looking for a
way to process all string and int members. If I do this :

    pointcut getVar(Object whatever): 
        get(* Person.*) && target(whatever);

I am able to trap everything, but the primitive types give a
class-cast exception. So, another question is - is it
possible to handle primitive types in aspectJ ?

I am new to aspects, so I might probably be doing something
wrong.  Thanks in advance for any suggestions.

Regards,
Somik


Back to the top