vendredi 14 avril 2017

Using aspectj to set a variable even when is set via reflection

I have an aspect which intercept a setter to a field which works well, but when the value is assigned from Gson library por example, it doesn't work.

This is the aspect:

public aspect AnnotationAspect {

    pointcut hasAnnotation(Annotation annotation) : @annotation(annotation);

    Object around(Annotation annotation, String word) : hasAnnotation(annotation) && set(String *) && args(word) {
        Object result = null;
        try {
            result = proceed(annotation, "intercepted");
        } catch (RuntimeException ex) {
            throw ex;
        }
        return result;
    }
}

And I have this class:

public class JavaEntity {

    @Annotation
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

When I do something like this it does not intercept the assignment:

JavaEntity entity = new Gson().fromJson("{name: 'name'}", JavaEntity.class);

Is there anyway to intercept that? Thank you!





Aucun commentaire:

Enregistrer un commentaire