javaannotationslombok

Should Lombok annotations be written before others on a class, method, etc.?


I believe I read somewhere that Lombok annotations should typically be placed before any others on a class or other code element, because they should be applied first. That way, code generated by Lombok can be taken into account by other annotations.

ChatGPT concurs. However, I can't find a more authoritative source anymore.

Is there one?


Solution

  • If the question is: Does it matter, does the order of annotations make a difference, the answer is an obvious no. ChatGPT is wrong, and that kind of question is a very bad one to ask ChatGPT 1

    If the question is: Should one, that's a style/opinion question and thus not appropriate for SO.

    What can matter is the order in which Annotation Processors run. This order is not decided by the order of your annotations, and not even by the order of your -proc: command line options. It's more or less arbitrary.

    In cases like using lombok and mapstruct together, where order does matter (lombok has to run first), mapstruct and lombok try to work together to figure out the problematic situation where mapstruct arbitrarily runs first, and have MS delay itself to the next round to restore the proper order.


    [1] The thing you need to know about LLMs is that they basically keep searching until an answer arrives. They aren't capable of telling you they don't know. If they don't know, they end up fabricating an answer out of whole cloth, and it'll look authoritative even when it is utter fabulation - and that's just a fraction of a percentage of all there is to know about hallucinations. This is extremely oversimplified; just be aware that 'ChatGPT told me something' doesn't mean that's correct, especially when you ask an objective question that nobody asked yet on the internet.