Question

I am trying to get the name of the private field of an object. When i use the same point-cut for the public fields of my package it works just right. My aspect is set to privileged. I want the fields where the classes' objects are updated. Pointcut:

pointcut priv(Object v): target (v)&& !(call(new(..)))  && set(private * *.*) 

Advice:

after(Object v): priv(v)

i get an exception on this line within my pointcut Exception:

Object dt = dts.getDeclaringType().getField(dts.getName());
Was it helpful?

Solution

First of all, your pointcut does not make sense because a set pointcut can never be the same as a call pointcut, so you do not need to exclude it. If you intend to exclude member assignments in constructors, which is what I suppose, you need something like !cflow(execution(*.new(..))) to exclude control flows within constructors. This is just an example, you may need to specify your exclusion criterion more precisely if it can happen that a constructor calls a method where members are changed, which probably you do not want to exclude etc.

Secondly, I think it is rather ugly to call a bound target v, I would rather call it target or t, if anything. But if you intend to bind the value to v, not the target, you must use args(v) anyway.

Now your actual problem: As the Javadoc explicitly says, getField only works on public fields. So if you need to access a private field, please use getDeclaredField.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top