2

我有一个字段设置切入点,这似乎符合我的预期。其定义如下

before(Object newval): set(@Serviced private * *.*) && args(newval)

上述内容旨在捕获:每当设置带有@Serviced 注释的私有字段属性时,请致电我的之前建议。

一切似乎都工作正常,除了我的代码中的一种情况,它通过 java 反射设置了一个与上述匹配的变量(即通过 java.lang.reflect.Field.set(....)。

有什么想法我也可以抓住那个“集合”吗?

谢谢

4

1 回答 1

5

正如您所注意到的,set()切入点无法拦截反射场的变化。但是,如果您控制(即可以将方面编织到)调用Field.set*(..)方法的代码,您也可以通过使用反射来解决该问题。这是一个完整的、可编译的代码示例,说明了该解决方案:

示例注释:

package de.scrum_master.app;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Serviced {}

带有 main 方法的示例实体类:

package de.scrum_master.app;

public class Person {
    @Serviced private int id;
    @Serviced private String name;
    private String country;

    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getCountry() { return country; }
    public void setCountry(String country) { this.country = country; }

    public void setIdReflective(int id) throws Exception {
        Person.class.getDeclaredField("id").setInt(this, id);
    }

    public void setNameReflective(String name) throws Exception {
        Person.class.getDeclaredField("name").set(this, name);
    }

    public void setCountryReflective(String country) throws Exception {
        Person.class.getDeclaredField("country").set(this, country);
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", country=" + country + "]";
    }

    public static void main(String[] args) throws Exception {
        Person person = new Person();
        person.setId(11);
        person.setName("Tin Man");
        person.setCountry("Oz");
        System.out.println("Before reflective setters: " + person);
        person.setIdReflective(22);
        person.setNameReflective("Cowardly Lion");
        person.setCountryReflective("The Land of Oz");
        System.out.println("After reflective setters:  " + person);
    }
}

如您所见,只有三分之二的私有字段具有@Serviced注释。对所有三个字段调用 Setter 两次:一次正常,一次通过反射。

截取正常和反射场变化的方面:

package de.scrum_master.aspect;

import de.scrum_master.app.Serviced;
import java.lang.reflect.Field;

public aspect ServicedFieldChangeInterceptor {
    before(Object newValue):
        set(@Serviced private * *) && args(newValue)
    {
        System.out.println(thisJoinPointStaticPart + " -> " + newValue);
    }

    before(Object newValue, Field field):
        call(public void Field.set*(Object, *)) && args(*, newValue) && target(field)
    {
        if (field.getAnnotation(Serviced.class) == null)
            return;
        System.out.println(thisJoinPointStaticPart + " -> " + field + ", " + newValue);
    }
}

运行时的示例输出Person.main

set(int de.scrum_master.app.Person.id) -> 11
set(String de.scrum_master.app.Person.name) -> Tin Man
Before reflective setters: Person [id=11, name=Tin Man, country=Oz]
call(void java.lang.reflect.Field.setInt(Object, int)) -> private int de.scrum_master.app.Person.id, 22
call(void java.lang.reflect.Field.set(Object, Object)) -> private java.lang.String de.scrum_master.app.Person.name, Cowardly Lion
After reflective setters:  Person [id=22, name=Cowardly Lion, country=The Land of Oz]

输出清楚地表明,这两个建议都只对带有注释的字段“做某事”(在这种情况下将信息打印到标准输出)@Serviced,而跳过其他字段。虽然set()切入点是静态应用的,但反射式切入点需要动态确定目标字段是否具有匹配的注释。

于 2014-05-04T14:47:17.493 回答