4

Is it possible to set pointcut on native method call with AspectJ? I tried following aspect:

public aspect EmailAspect {
    pointcut conn() : call(* java.net.PlainSocketImpl.socketConnect(..));
    before() : conn() {
        System.out.println("Connecting");
    }
}

But it doesn't work. I also didn't find much relevant info via googling (except this http://blog.jayway.com/2007/02/16/static-mock-using-aspectj, however it's not clear if it's possible & how to do it).

I tried to debug my test code (which only connects to some TCP localhost port) in Eclipse and eclipse stopped at socketConnect() breakpoint.

Thank you.

UPDATE

This is probably the cause:

AspectJ - Load-time weaving, privileged aspect and pointcut on private method of JRE class

Community
  • 1
  • 1
batto
  • 171
  • 1
  • 2
  • 10

2 Answers2

2

PlainSocketImpl is in a jar provided by the JDK, right? You may find the question about weaving into jars helpful. Apparently after you weave in your aspect, you need to remember to run using the woven jars rather than the original.

Community
  • 1
  • 1
Atreys
  • 3,741
  • 1
  • 17
  • 27
  • Sorry, I forgot to mention that I don't want to use source code weaving because I don't want to include whole JDK in my compiled code (I use load-time weaving). I think the problem is that socketConnect() is package private and aspect is not privileged. – batto Jun 07 '11 at 17:06
  • @batto That sounds like a likely culprit. Apologies, I've plenty of aspect theory but not the practical experience. – Atreys Jun 07 '11 at 17:11
1

If this e-mail is true, once a method is declared native, Java will write bytecode that jumps directly to the underlying C++/C machine code. This means there is nothing to intercept for AspectJ, regardless whether it is compile time, post-compile time, or load-time weaving.

Nick
  • 5,765
  • 5
  • 27
  • 36