0

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.

Ducaz035
  • 3,054
  • 2
  • 25
  • 45
  • Why does it need to be an annotation exactly? – Herr Derb Jan 04 '18 at 13:55
  • It doesn't need to be but I already have the annotation and annotate java classes with it and for other case I need to use more or less the same values as the annotation but I wonder if there is something wrong with the approach at all. – Ducaz035 Jan 04 '18 at 13:56
  • 1
    duplicate https://stackoverflow.com/questions/3341930/use-cases-for-implementing-annotations#3354409 – Rodolfo Jan 04 '18 at 14:01
  • @rodolfo how it's a duplicate? I don't wanna know how to do it, I wanna know what's the advantages of it and drawbacks. – Ducaz035 Jan 04 '18 at 14:04
  • @Ducaz035 advantages and drawbacks compared to what ? a POJO ? – Rodolfo Jan 04 '18 at 14:08
  • @rodolfo Is there an disadvantage to use an implemented annotation as a POJO? – Ducaz035 Jan 04 '18 at 14:10
  • How would you use it could you give an example? – Rodolfo Jan 04 '18 at 14:12
  • https://stackoverflow.com/questions/466184/when-to-use-inheritance just don't. Inheritance is rarely a good idea if you just want to reuse some attributes. – Cyril Jan 04 '18 at 14:28
  • @rodolfo I just updated the question, please let me know if that's still not understandable. – Ducaz035 Jan 04 '18 at 14:36
  • @Ducaz035 until java 7 you just need to declare your annotation and use it. – Rodolfo Jan 04 '18 at 19:25
  • @Ducaz035 sorry still not clear what do you mean by "... so that in the runtime I can simply generate it instead of a POJO. " what you you want to generate at runtime? the annotation ? and why ? – Rodolfo Jan 04 '18 at 19:48

1 Answers1

0

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

Rodolfo
  • 491
  • 5
  • 13
  • Sorry but they are not related with my question. Please check the answers of those question. I'm more interested in using implemented annotation as POJOs – Ducaz035 Jan 04 '18 at 14:11
  • I see so you whant to generate the whole pojo at runtime using the annotations? – Rodolfo Jan 04 '18 at 14:28