自定义Java字段注释:获取字段值

时间:2018-10-12 09:40:47

标签: java android annotations

我正在尝试制作一个自定义注释以添加到类中的字段中。

我似乎找不到很多在字段上运行方法的方式。我对注解非常陌生,因此我什至可能会做错此事。

例如:

@Data
public class BookingForm {
    private static final String DEBUG = "Booking Form";
    @NameInput
    private String name;
    @EmailInput
    private String emailAddress;
    @PhoneNumberInput
    private String phoneNumber;
    private String bookingType;
    private String bookingDate;
    private String numberOfGuests;
    private String message;
    private BookingsPackage bookingsPackage;

    public BookingForm(String name, String emailAddress, String phoneNumber, String bookingType, String bookingDate, String numberOfGuests, String message, BookingsPackage bookingsPackage) throws InvalidFieldsException {

        this.name = name;
        this.emailAddress = emailAddress;
        this.phoneNumber = phoneNumber;
        this.bookingType = bookingType;
        this.bookingDate = bookingDate;
        this.numberOfGuests = numberOfGuests;
        this.message = message;
        this.bookingsPackage = bookingsPackage;
    }
} 

该类是通过注入字段构造的...然后,注释应使用@EmailInput等字段值运行方法:

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

    //Get the value of the annotated field and run a method(s).
    //E.g. If email does not pass regex -> Throw custom exception.

}

这可能吗?我无法理解如何获取@interface中已注释的字段的值。

我认为也许有一种方法可以使用“ @Form”自定义注释对类进行注释,该自定义注释会获取所有已注释的字段并运行某种“开关盒” ...

Th!

0 个答案:

没有答案