简体   繁体   中英

Is implementing an Annotation a bad idea in Java?

Currently I have been thinking about the question however, I couldn't find a proper answer. Use case at hand is to create an implementation class for a particular custom annotation so that in the runtime I can simply generate it instead of a POJO.

For instance:

Annotation:

@interface CustomAnnotation {
    String date();
}

At this stage I need a bean which happens to have the same fields as the annotation. Here I have two options either implement the annotation and create it in runtime or create a class to carry the information.

A) Implementation of Annotation:

public class CustomAnnotationImpl implements CustomAnnotation {

private final String date;

public CustomAnnotationImpl(String date) {
    this.date = date;
}


@Override
public String date() {
    return this.date;
}

@Override
public Class<? extends Annotation> annotationType() {
    return CustomAnnotation.class;
}
}

B)

public class CustomBean {

    private final String date;

    public CustomAnnotationImpl(String date) {
        this.date = date;
    }

    public String getDate() {
        return this.date;
    }
}

Also keep in my mind that the bean and annotation will be always in sync meaning that bean actually will be always a copy of the annotation.

My question is that what would be the advantages and drawbacks of those, if any? I'm asking this because simply I haven't seen implementation of annotation myself.

I do not understand 100% your question, but it looks like other people already ask something like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM