Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] null thisJoinPointStaticPart when advising constructor execution

What I'm trying to do is use AspectJ to build a variant of coverage tool.

It is something similar to JTestMe (which also uses AspectJ).

This tool is supposed to run on any Java application that has JUnit tests, so I can't control or change the code to avoid this type of problem.

 

Does this mean I can't always collect coverage data from execution of constructors?

 

Thanks,

Hagai

 

-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Andy Clement
Sent: Friday, March 19, 2010 9:38 PM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] null thisJoinPointStaticPart when advising constructor execution

 

Here is some pure Java code that shows what you are hitting:

---

public class A {

  public static void main(String []argv) {

    new C();

  }

  static String get() { return "abc";}

}

 

class B {

  static C b = new C();

}

 

class C extends B {

  final static String str = A.get();

 

  public C() {

    System.out.println(str);

  }

}

---

when run it prints:

null

abc

 

This is because a condition of initializing C is that B is

initialized.  B's static initializer will call back to C - which

hasn't had the initializer run yet, so the field 'str' is null.  If I

replace 'str' with thisJoinPointStaticPart, then that is the problem

AspectJ is triggering with the code it is creating.

 

Andy

 

On 18 March 2010 14:35, Hagai Cibulski <hagaic@xxxxxxxxx> wrote:

> Here is a variation on your program that recreates the problem:

> 

> 

> 

> aspect Test {

> 

>       public static void main(String[] argv) {

> 

>             new C();

> 

>       }

> 

> 

> 

>       pointcut methodUnderTestExecution1() :

> 

>             (execution(* *(..)) || execution(new(..))) && !within(Test);

> 

> 

> 

>       before(): methodUnderTestExecution1() {

> 

>             Object sig = thisJoinPointStaticPart.getSignature(); // null L

> 

>             System.out.println(sig);

> 

>       }

> 

> }

> 

> 

> 

> class B {

> 

>       static C b = new C();

> 

> }

> 

> class C extends B {

> 

> }

> 

> 

> 

> -----Original Message-----

> From: aspectj-users-bounces@xxxxxxxxxxx

> [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Andy Clement

> Sent: Thursday, March 18, 2010 10:20 PM

> To: aspectj-users@xxxxxxxxxxx

> Subject: Re: [aspectj-users] null thisJoinPointStaticPart when advising

> constructor execution

> 

> 

> 

> Can you tell me what kind of constructors lead to that being null?  A

> 

> simple (complete) program that fails would be great.  This program:

> 

> 

> 

> ---

> 

> aspect Test {

> 

>   public static void main(String []argv) {

> 

>     new C();

> 

>   }

> 

> 

> 

>  pointcut methodUnderTestExecution1() :

> 

>             (execution(* *(..)) || execution(new(..))) && !within(Test);

> 

> 

> 

>   before(): methodUnderTestExecution1() {

> 

>         Object sig = thisJoinPointStaticPart.getSignature();

> 

>       System.out.println(sig);

> 

>   }

> 

> 

> 

> }

> 

> 

> 

> class C { }

> 

> ---

> 

> 

> 

> works fine and prints C().

> 

> 

> 

> cheers

> 

> Andy

> 

> 

> 

> On 18 March 2010 13:09, Hagai Cibulski <hagaic@xxxxxxxxx> wrote:

> 

>> Hi All!

> 

>> 

> 

>> Could use some help here

> 

>> 

> 

>> 

> 

>> 

> 

>> I'm tracing methods execution stemming from JUnit tests using AspectJ,

>> using

> 

>> the following pointcut:

> 

>> 

> 

>>       pointcut methodUnderTestExecution() :

> 

>> 

> 

>>             execution(* *(..))

> 

>> 

> 

>>             && mutFilter();

> 

>> 

> 

>> 

> 

>> 

> 

>> before(): methodUnderTestExecution() {

> 

>> 

> 

>>             Signature sig = thisJoinPointStaticPart.getSignature();

> 

>> 

> 

>>                         …

> 

>> 

> 

>> So far, that works fine!

> 

>> 

> 

>> 

> 

>> 

> 

>> However, when trying to add constructor execution to the pointcut, I get

> 

>> some unexpected behaviors:

> 

>> 

> 

>> 

> 

>> 

> 

>>       pointcut methodUnderTestExecution1() :

> 

>> 

> 

>>             (execution(* *(..)) || execution(new(..)))

> 

>> 

> 

>>             && mutFilter();

> 

>> 

> 

>> 

> 

>> 

> 

>> before(): methodUnderTestExecution1() {

> 

>> 

> 

>>             Signature sig = thisJoinPointStaticPart.getSignature();

> 

>> 

> 

>>             // thisJoinPointStaticPart == null L

> 

>> 

> 

>>                         …

> 

>> 

> 

>> 

> 

>> 

> 

>> For some constructors in the application under test the advice causes

> 

>> NullPointerException because thisJoinPointStaticPart == null

> 

>> 

> 

>> 

> 

>> 

> 

>> Anyone?

> 

>> 

> 

>> 

> 

>> 

> 

>> 

> 

>> 

> 

>> Thanks,

> 

>> 

> 

>> Hagai

> 

>> 

> 

>> _______________________________________________

> 

>> 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

> 

> _______________________________________________

> 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