0

I have multiple classes A, B and C.
All should have a private UUID uuid;.
But creating a class D that A, B and C can extend, is no option.
Can I create an annotation in Java, that adds this attribute to the class that I write the annotation to?

not possible because in this project I can not change the core and there are many linear inheritances and java does not support tree like inheritances:

public class D{
    private UUID uuid;
}
public class A extends D{}
public class B extends D{}
public class C extends D{}

So I hope something like the following is possible:

? the interface for creating the annotation D ?

@D
public class A{}

@D
public class B{}

@D
public class C{}

How can I achieve this with an annotation?
What must the interface look like?

toddeTV
  • 1,447
  • 3
  • 20
  • 36
  • @Michael The question does not appear to be asking how to add an annotation at runtime, but how to write an annotation whose effect is to add an attribute to the class. – khelwood Feb 19 '19 at 13:23
  • @khelwood Fair enough. Re opened. – Michael Feb 19 '19 at 13:29
  • Whith can't you create an abstract class? – Ctorres Feb 19 '19 at 13:32
  • 1
    Annotations are metadata. An abstract class is fine, and I would strongly advise you to use that. That said, if you are truly determined to do it this way then I would fork Project Lombok and manipulate the AST to generate these automatically. A lot of the work has been done for you but it still will not be easy. – Michael Feb 19 '19 at 13:34
  • @Michael so you mean I should stick to my first code example? (and making D abstract). But in general linear inheritance, so that A, B and C extends D? Or did I misunderstood it? – toddeTV Feb 19 '19 at 13:36
  • 1
    Yes. Use your first example. – Michael Feb 19 '19 at 13:47

0 Answers0