Skip to main content

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

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-


Back to the top