0

I'm creating a custom Metric Aspect so that I don't have to do METRIC.aboutTo("save", "order") in the code. My organization uses custom metric libraries.

So, I want to create a custom metric annotation and aspect to do this for me.

I'm looking to be able to add the annotation @PostgressMetric to both the class and the method. It would look something like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface PostgressMetric {

    String value() default "";
    String table() default "";

}

If the metric is on the class, then I want to use those values as "defaults". So you could have something like this:

@Repository
@PostgressMetric(table = "order")
public class OrderRepository {

   @PostgressMetric("save")
   public void save(Order order) {
   }
}

This is also a valid setup:

@Repository
public class OrderRepository {

   @PostgressMetric(table="order", value="save")
   public void save(Order order) {
   }
}

and of course this is also a valid setup:

@Repository
@PostgressMetric(table="order", value="save")
public class OrderRepository {

   public void save(Order order) {
   }
}

Now, in my aspect I want to get the annotation from the class, then get the annotation from the method. If the class has the annotation then get the table and value and use those as default values. If the method has the annotation then if the values aren't blank, use them to override the default values specified in the class annotation.

Is there a standard way to get the two annotations, or is it just a brute force get the class values and then get the method values and override them?

  • Do go through this [answer](https://stackoverflow.com/a/53452483/4214241) and explanation of that answer [here](https://stackoverflow.com/questions/61430240/how-does-this-execution-pointcut-expression-work) . With Spring AOP , kriegaex's answer is the better approach for your question – R.G May 08 '22 at 04:35
  • Welcome to SO. Please be advised to provide feedback and not just remain silent when someone comments or answers your question, trying to help you. It is a matter of basic courtesy. – kriegaex May 14 '22 at 09:03
  • This was not usefull for us because it didn't give a method to get both the class/type level annotation and the method annotation. So we resolved the issue by passing in the method annotation into the aspect and interrogating for the class annotation and if the method annotation was null we used the class annotation otherwise we blend them with the class annotation being the default. There doesn't seem to be a mechanism I have found to include both annotations. – user2076898 Jul 16 '22 at 00:41
  • Instead of using reflection, why don't you use two advice methods, one for method and one for class level annotation? You can easily get each without reflection. Then pass it on to a helper method from both. The latter would contain the common code to avoid duplication. Very simple. – kriegaex Jul 16 '22 at 03:04
  • By the way, not replying for two months and not showing your own aspect code is not helpful. Maybe work on your way of asking questions first, before disqualifying someone else's comment as "not useful". How can anyone help you improve code you are unwilling to even share? Can you debug invisible code? I suggest that you write your own answer, presenting your workaround, and someone here might be able to help you improve it by writing a better answer. Give it a try. – kriegaex Jul 16 '22 at 03:31

0 Answers0