Skip to content
Advertisement

Is it possible to exclude field-sets in AspectJ

Is it possible to exclude fiel-sets in AspectJ pointcuts, so instrumentation does not stumble over final fields in Java 11?

When weaving the following aspect (the full example is here: https://github.com/DaGeRe/aspect-final-example):

JavaScript

into

JavaScript

by java -cp target/test-0.1-SNAPSHOT.jar -javaagent:../aspect/target/aspectjtest-0.1-SNAPSHOT.jar de.test.MainWithError I get

JavaScript

when I execute it with OpenJDK 11 (when setting everything to Java 8, it works fine). When removing the final modifier from FinalFieldConstructorExample and the && noSet() from the pointcut, it works fine and the output contains

JavaScript

Therefore, I suppose the set-call (having a getKind of field-set, which seems not to be present in OpenJDK 8) to a static field is the reason of the problem. Is there any way of excluding it from AspectJ instrumentation (or working around the problem)? The documentation (https://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html#primitive-pointcuts) states that get can be used in a Pointcut, but I did not find a way to specify final, and even if I add noSet, it seems to be somehow touched and the error appears.

Advertisement

Answer

I think you are hitting AspectJ issue #563709. The error message is the same and so is the fact that it works on Java 8 but not 11 (probably 9+).

So as a workaround for now you want to avoid around-advising constructors. Either you exclude them via

JavaScript

or, considering the fact that your advice only does something before proceed(), just change the advice type:

JavaScript

If for whatever reason you need @Around and cannot refactor it into a @Before + @After pair generally, you can keep it with the above exclusion of constructor executions and add a separate @Before + @After advice pair just for the constructors.


Update:

Excluding constructors or using only @Before works, but is not usable for my use case (method execution duration monitoring)

Well, then how about this workaround, globally replacing @Around by pairs of @Before + @After? You may even notice that your log now also shows additional preinitialization and initialization pointcuts which formerly were not captured by the around advice because for those pointcut types around is not supported. Here is my MCVE:

JavaScript
JavaScript
JavaScript

The console log would look like this:

JavaScript

P.S.: Are you aware of the fact that for woven methods and constructors you are logging both call and execution for the same method/constructor?

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement