当对象状态改变时调用方法

时间:2013-05-03 06:20:25

标签: java aop eclipse-plugin bcel

我提供了一个注释@validateName,任何人都可以在其代码中包含这些注释。假设有一个编码

class Person {
    @validateName
    private String name;
    ....
}

然后他们可以调用NameValidator.validate(personObject)或类似的方法来验证该字段。

我想确保name字段始终处于有效状态,即每当注释变量发生变化时(无论它在类的内部还是外部发生变化),我都要自动调用validate()方法。
我愿意编写一个挂钩到Eclipse的插件,并在编译阶段调用它。请提供一些指示,我可以从哪里开始寻找解决方案 (我想我必须实现某种AOP或者应该使用BCEL或其他东西来修改字节码。我不确定,因为我没有尝试过这两种方法。)

1 个答案:

答案 0 :(得分:1)

使用AspectJ,您可以这样做:

<强>注释

package de.scrum_master.aop.app;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateName {}

驱动程序类

package de.scrum_master.aop.app;

public class Application {
    private int id;
    @ValidateName
    private String firstName;
    @ValidateName
    private String lastName;
    private String placeOfBirth;

    public Application(int id, String firstName, String lastName, String placeOfBirth) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.placeOfBirth = placeOfBirth;
    }

    @Override
    public String toString() {
        return "Application [id=" + id + ", firstName=" + firstName
                + ", lastName=" + lastName + ", placeOfBirth=" + placeOfBirth
                + "]";
    }

    public static void main(String[] args) {
        System.out.println(new Application(1, "Galileo", "Galilei", "Pisa, Italy"));
        System.out.println(new Application(2, "Isaac", "Newton", "Woolsthorpe-by-Colsterworth, United Kingdom"));
        System.out.println(new Application(3, "Albert", "Einstein", "Ulm, Germany"));
        System.out.println(new Application(4, "Werner", "Heisenberg", "Würzburg, Germany"));
    }
}

验证方面

package de.scrum_master.aop.aspect;

import java.util.Random;
import de.scrum_master.aop.app.ValidateName;

public aspect NameValidator {
    void validate(String name) {
        if (new Random().nextBoolean())
            throw new RuntimeException("Invalid name " + name);
    }

    void around(String name) : set(@ValidateName * *.*) && args(name) {
        //System.out.println(thisJoinPointStaticPart);
        System.out.print("Validating name " + name);
        try {
            validate(name);
            System.out.println(" -> OK");
            proceed(name);
        }
        catch (Exception e) {
            name = name.toUpperCase();
            System.out.println(" -> " + e.getMessage() + " -> replaced by " + name);
            proceed(name);
        }
    }
}

正如您所看到的,我的验证器随机失败了。所有案例的50%基于伪随机值。如果是这样,它只是用大写版本替换“无效”名称。输出看起来像这样的变体:

Validating name Galileo -> OK
Validating name Galilei -> Invalid name Galilei -> replaced by GALILEI
Application [id=1, firstName=Galileo, lastName=GALILEI, placeOfBirth=Pisa, Italy]
Validating name Isaac -> Invalid name Isaac -> replaced by ISAAC
Validating name Newton -> Invalid name Newton -> replaced by NEWTON
Application [id=2, firstName=ISAAC, lastName=NEWTON, placeOfBirth=Woolsthorpe-by-Colsterworth, United Kingdom]
Validating name Albert -> OK
Validating name Einstein -> Invalid name Einstein -> replaced by EINSTEIN
Application [id=3, firstName=Albert, lastName=EINSTEIN, placeOfBirth=Ulm, Germany]
Validating name Werner -> OK
Validating name Heisenberg -> Invalid name Heisenberg -> replaced by HEISENBERG
Application [id=4, firstName=Werner, lastName=HEISENBERG, placeOfBirth=Würzburg, Germany]