0

I have an interface and a bunch of classes implementing that interface. I want to add @Priority(xxx) annotation to all these classes with changing value as depicted below.


public interface IMyInterface{}

Before :

public classA implements IMyInterface{}
public classB implements IMyInterface{}

What I need:

@Priority(1)
public classA implements IMyInterface{}

@Priority(2)
public classB implements IMyInterface{}

What i tried so far:

declare @type: com.package.* : @Priority(999);

I am able to add annotations with same value with above approach but i need to add different values.

Any leads on how to achieve this would be very helpful.

  • What kind of values you want to add? – Daksharaj kamal Jan 06 '23 at 06:25
  • integer values to control order of beans. – girijanandan nucha Jan 06 '23 at 09:59
  • You are on the right track already, having found out about `declare @type`. You just need to use multiple declare statements for multiple types of annotations you want to weave. You forgot to mention the most important detail of your situation: How can you technically distinguish the target classes (I mean, their priorities) from one another? By class name? Package? Any other characteristics which enable you to distinguish them? Answer that question, then maybe I can help you with a more precise answer. – kriegaex Jan 06 '23 at 11:19
  • Hi @kriegaex. Thank you for your quick response. I would be calculating the priority based on some information present in the class itself. To be more precise there will be a function in each class, I will calculate the priority based on its return value. – girijanandan nucha Jan 06 '23 at 13:40
  • 1
    Then you cannot work with annotations, because annotation values must be constants and determined during compile time. If you want to calculate the priority during runtime, just do so and assign priorities dynamically to whichever tool you use. Don't use a hammer to drill a hole, it simply is the wrong tool. Focus on the goal at hand, rather than obsessing about a technical way you dreamed up how to achieve it. This is yet another example of the [XY problem](https://meta.stackexchange.com/a/66378/309898). Many unexperienced developers (and too many experienced ones) step into this trap. – kriegaex Jan 06 '23 at 14:12
  • @kriegaex: Thanks for the answer. I will try to solve this problem with correct tool. – girijanandan nucha Jan 10 '23 at 08:21

0 Answers0