Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Detecting null values of a field

Andy, Thanks for the response.

-S-

On Mon, Jan 5, 2009 at 9:28 PM, Andy Clement <andrew.clement@xxxxxxxxx> wrote:
My first observation would be to change it so that you aren't advising your advice.  Within the advice you access 'e' and that matches "get(* TestClass.e)"  at that point this() is the aspect - and so the classcastexceptions occur because the cast to TestClass fails and that makes the output confusing.  If I add a bit to the pointcut "&& !within(NullAspect)" and run it (I'm only using the first piece of advice here) I get:

C:\aspectj163>java TestClass
O is: Event@9304b1
It is NOT null
O is: null
It is null
O is: Event@190d11
It is NOT null

Is that the output you want? If it isn't - please say what you are expecting.

cheers,
Andy.

2009/1/4 100ji <itz100ji@xxxxxxxxx>
Hi all,

In my tracing application, I only want to trace a field when it's value is non-null. But for some reason when I compare the value of the field in the advice, it doesn't seem to work. I understand that null is not an object, but I can't seem to find any documentation that says that null should be treated specially. Can anyone point out what I am missing? The code below illustrates what I am doing:

---------------
public class TestClass {
   
    Event e;
   
    TestClass() {
        e = new Event();
    }
   
    public void setNull() {
        e = null;
    }
   
    public void createE() {
        e = new Event();
    }
   
    public static void main(String[] args) {
        TestClass t = new TestClass();
       
        t.isNull();
        t.setNull();
        t.isNull();
        t.createE();
        t.isNull();
    }

    public void isNull() {
        if (e == null) {
            System.out.println("It is null");
        } else {
            System.out.println("It is NOT null");
        }
    }
}

----------------------


public class Event {
    public Integer number;
}


--------------------

public privileged aspect NullAspect {
    pointcut p1(Object o) : this(o) && get(* TestClass.e);
   
    before(Object o) : p1(o) {
        try {
            System.out.println("O is: " + ((TestClass)o).e);
        } catch (NullPointerException ne){
            System.out.println(ne);
            System.out.println("o is null");
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("o is not null");
        }
    }
   
    // I have also tried:

    before(Object o) : p1(o) {
       if (o != null) {
            System.out.println("o is not null");         
      } else {
            System.out.println("o is null");
    }
}
----------------------

TIA,
-S-


_______________________________________________
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



Back to the top