AspectJ not capture calls from jar file

2020-08-01 05:44发布

问题:

I want to do specific action when System.currentTimeMillis() is called. I use AspectJ as below to do that.

public aspect CurrentTimeInMillisMethodCallChanger {
    long around(): 
    call(public static native long java.lang.System.currentTimeMillis()) {
        //provide my own implementation 
    }
}

This program works fine when System.currentTimeMillis() is called in any method of the application.

However, when System.currentTimeMillis() is called from method inside a jar file then around is not executed. I used within(packageofJarFile.*), but it does not work.

I am using Eclipse.

Please let me know what I need to add to the above code to support call from (specific) jar file as well ?

回答1:

You need to set up the AspectJ plugin for Eclipse (AJDT), so that it weaves library code as well, not just your own class files. You can do this by going to Project Properties -> AspectJ Build -> Inpath tab and add the jar files you want to be weaved by your aspects. Here is a screenshot of the dialog: The library classes weaved like this will be dumped in your output classes folder unless you set up an Output jar on the third tab of the same properties page.