12

As you know, the annotation driven programming is more and more incorporated in the majority of frameworks that we are using nowadays (i.e. Spring, Lombok etc).

Besides, we need sometimes to create our custom annotation. (i.g. Logging enter/exit traces for all public methods of a given class using an aspect - @LogAroundMethods)

Hence, a given class can hold plenty of annotations.

@LogAroundMethod // My custom annotation
@Slf4j // Lombok annotation
@Component // Spring annotation
public class ClientNotificationProxy {
//Code
}

@LogAroundMethod // My custom annotation
@Configuration // Spring annotation
@ConditionalOnClass(NotificationSender.class) // Spring annotation
@EnableConfigurationProperties(MessagingProperties.class) // Spring annotation
@Import({ MongoConfiguration.class, SpringRetryConfiguration.class }) // Spring annotation
public class StarterClientAutoConfiguration {
// Code 
}
  • What's the recommended order of annotations ?
  • Is there any impact or benefit from a specific order ?
Katy
  • 1,023
  • 7
  • 19
  • 2
    It is more the order of _annotation scanners_ that might effect things; every scanner looking for its own annotations. The bizar: 5 annotations and then `{}`. – Joop Eggen Jan 03 '20 at 16:49
  • @JoopEggen, thank you for comment. The class contains several lines which I have removed from the question. (Since we are focusing on annotation) – Katy Jan 03 '20 at 16:53
  • Since the Java Reflection API used to get annotations of a class doesn't guarantee any particular order for the returned annotations, the **question is moot**. – Andreas Jan 03 '20 at 17:00
  • 2
    That is incorrect, Andreas. https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/reflect/AnnotatedElement.html explains how the order is computed. – Jan Rieke Jan 03 '20 at 19:22

3 Answers3

8

In almost all cases the answer is No, the order has no effect.

But in fact it is a bit more complicated.

  1. Considering annotations that are processed by annotation processors, it was already stated in the other answers that it more depends on the order in which the processors run. However, the processors have access to the AST, which allows them to determine the order of annotations in the source code. So theoretically annotation processors can make the generated code dependent on the order, but I don't know any example for this and would call this bad practice.

  2. When getting the annotation of an element during runtime, you also have access to the order. The docs have more info on how the order is determined. So again, an implementation could make its behaviour dependent on the order. Again I would consider this bad practice. The only exception may be repeatable annotations, where I can think of use cases where this could be reasonable.

If there is any dependence on the order of annotations, which is very unlikely, this should be made very clear in the JavaDoc of the annotation.

So typically you are free to order them as you like. I don't know of any style guide on the order of annotations, so just make it reasonable for you.

Jan Rieke
  • 7,027
  • 2
  • 20
  • 30
  • Thank you for answer. I was looking for a documentation explaining how Java manages annotations but are you sure that all frameworks reuse the oracle implementation? Because from my understanding, this handles Java specific annotations (e.g. Inject, Documented etc..) – Katy Jan 03 '20 at 21:02
  • 1
    According to the JLS, there is no difference between predefined and user-defined annotations. If you use the Oracle compiler and JVM, you can be sure it will yield to the spec. Of course other JVMs/compilers may behave differently, but that would violate the JLS. – Jan Rieke Jan 04 '20 at 01:17
4

Is there any impact or benefit from a specific order ?

Not that I am aware of. Remember how annotations work: some piece of code "looks" at your code, and checks for the presence of an annotation. Meaning: it "gets" an array of annotations, and checks whether the one it cares about is in that array. So order should be irrelevant.

Of course, when we talk about annotations that have compile-time effects, the story might be different. Such annotations have effects on the compilation process itself, so worst case, order of annotations changes the compilation process.

What's the recommended order of annotations?

The one that works for you. Meaning: sit down with your team, and ask yourself "do we prefer a specific order". If so, write that down, and have people follow that rule.

Real world example: we use annotations to "describe" the "properties" of our "objects". At some point, we saw that we often forgot annotation X when adding new properties. Because properties were written down in random order, thus hard to process manually (and we have plenty of different annotations, sometimes 5 to 10 on a single property).

Our "solution": annotations must be sorted alphabetically (when used for such "property" definitions). And we even have a unit test that checks for that sorting order. Since then: all "property" definitions follow the same rule. And that works nicely for us, because everybody comes with the same expectation and mindset.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

"Is there any impacts or benefits from a specific order ?" - Not as far as I know of, no. As was pointed out by Joop Eggen, the order of annotation processors or annotation scanners it the important part.


"What's the recommended order of annotations ?" - If one is able to identify a pattern of annotations commonly used together, I would recommend to group them in a new annotation and use the new annotation instead. One can achieve this by creating a new publice @interface MyAnnotation and annotate this new annotation with the annotations one wants to group. See this answer for an example.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • I appreciate your suggestion for regrouping annotations. it will enhance code readablility. – Katy Jan 03 '20 at 16:58