admin管理员组

文章数量:1130349

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?

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?

Share Improve this question asked Dec 11, 2024 at 11:00 NahojNahoj 15810 bronze badges 3
  • 1 The order doesn't matter. The generated code is identical. Lombok's annotations are @Retention(RetentionPolicy.Source) so none of them end up in the bytecode. – f1sh Commented Dec 11, 2024 at 11:08
  • 1 Annotations do not take anything into account, as they are not active in any way. Annotation processors do things. So the order in which they run may matter (in case of hacks like Lombok, as normally, annotation processor only produce new code, to be processed in the next phase, rather than manipulating the existing code). – Holger Commented Dec 11, 2024 at 11:59
  • 1 Some time ago I answered a more general question. TL;DR: Typically, the order does not matter. However, the order is determinable at runtime and for annotation processors, but I don't know any examples where this has an effect (and I would consider it bad practice if it would.) – Jan Rieke Commented Dec 12, 2024 at 9:23
Add a comment  | 

1 Answer 1

Reset to default 5

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.

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?

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?

Share Improve this question asked Dec 11, 2024 at 11:00 NahojNahoj 15810 bronze badges 3
  • 1 The order doesn't matter. The generated code is identical. Lombok's annotations are @Retention(RetentionPolicy.Source) so none of them end up in the bytecode. – f1sh Commented Dec 11, 2024 at 11:08
  • 1 Annotations do not take anything into account, as they are not active in any way. Annotation processors do things. So the order in which they run may matter (in case of hacks like Lombok, as normally, annotation processor only produce new code, to be processed in the next phase, rather than manipulating the existing code). – Holger Commented Dec 11, 2024 at 11:59
  • 1 Some time ago I answered a more general question. TL;DR: Typically, the order does not matter. However, the order is determinable at runtime and for annotation processors, but I don't know any examples where this has an effect (and I would consider it bad practice if it would.) – Jan Rieke Commented Dec 12, 2024 at 9:23
Add a comment  | 

1 Answer 1

Reset to default 5

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.

本文标签: