Not all annotations are legal on both fields and methods, but using resolution to try to figure this out is too expensive and complicated.
We did come up with the 'text file' tech (a text file listing a bunch of types, to which we can easily accept new commits, this sounds acceptable to us for storing 'domain knowledge' on such topics as: Which annotations like to be copied over to where), that might apply here: List the JSR 305 annotations so that lombok knows specifically to copy these to the params of methods and the generated getters.
So, let's get going on this. We need the FQN of each and every annotation type that is relevant, and we need to know a few things about it. Specifically:
* Should an annotation on the field copy to the param on the setter, and the param on the constructor (and the param on the builder, if present, and on the wither)?
* Should an annotation on the field copy to the getter?
* Should the annotation then be removed from the field? This may never work right (I'm afraid javac will just abort right off the bat without ever invoking lombok if you put an annotation that is explicitly defined not to be legal on fields, on a field).
* What does it 'mean'? Specifically, is there behaviour that should influence lombok? For example, right now any annotation called NonNull, no matter the package, imparts the null check behaviour onto lombok, but for the generate-nullcheck-in-any-method feature (where lombok inject if (x == null) throw new NPE("x") on the first line of a method if the param is annotated with lombok's own NonNull). We don't look at NotNull because JPA uses that, and NotNull in JPA land means something completely different. We can use the text file tech to the same purpose here: Have a listing of which annotations actually mean: Ensure that this field is never null, throw exceptions if that would ever happen.
Something like:
annotations.txt:
lombok.NonNull = NULL-PARAM-CHECK NULL-PROPERTY-CHECK COPY-TO-PARAM COPY-TO-GETTER
javax.annotation.Nonnull = NULL-PROPERTY-CHECK COPY-TO-PARAM COPY-TO-GETTER
javax.annotation.Nullable = COPY-TO-PARAM COPY-TO-GETTER
javax.annotation.RegEx = COPY-TO-PARAM COPY-TO-GETTER
javax.annotation.Inject = COPY-TO-PARAM REMOVE
Here PROPERTY-CHECK means that lombok generates nullcheckers in the builder, constructor, and setter, and PARAM-CHECK means lombok will interpret the presence of this annotation on any parameter anywhere as indicating the need to generate an if (x == null) line at the top of that method / constructor.
The COPY string identifies that this annotation needs to be copied over. (COPY-PARAM is copy to builder, constructor, setter, and wither, and COPY-GETTER means to copy to getter). REMOVE means to remove it from fields, though, as I said, that may not actually ever work.
We don't use JSR305 that much (though I know Roel finds this stuff interesting and has looked at it before, and the same goes for me), so preferably someone more familiar with the framework thinks this through and contributes the text file defining the appropriate definitions for all the JSR305 (and any others you can think of).
We can resolve types, kind of, in a cheap fashion. Our type resolver can answer questions of the form:
"This reference to Nonnull here, is that shorthand for javax.annotation.Nonnull?", and lombok can answer that question without resolution (by analysing the import and package statements in the source file), only getting it wrong when you have a star import on javax.annotation and a package-local type named Nonnull (lombok would say that is javax.annotation.Nonnull, but under those conditions, it isn't). Thus, we can 'find' the named annotations without resolution, if we have a text file that lists complete names.