-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TypeSpec#annotationBuilder
overload with retention and target parameters
#66
Comments
I can try to provide a pull request for this in case this won't require signing the CLA. |
@Marcono1234 Inside the methods, we can perform the checks that are similar to what records implementation already does (checking for a valid javapoet/javapoet/src/main/java/com/palantir/javapoet/TypeSpec.java Lines 731 to 737 in e02bae1
|
Might be an option as well, except that for code completion it might be confusing to see these methods when building non annotation types. Since you most likely always want to set 'retention' and 'target', I think the API should help / encourage you to specify them, without having to call additional methods (?). Personally I think the record example might not be a good one, it might be better there as well to have Though maybe there are also use cases where the |
Fair enough. However, breaking the "null hostility" still bothers me, and having a bunch of overloads sounds like a pain. What do you think about creating factory methods in On a side note, after looking at the TypeSpec rec = TypeSpec.recordBuilder("KeyValue")
.addModifiers(Modifier.PUBLIC)
.addRecordComponent(keyComponent)
.addRecordComponent(valueComponent)
.compactConstructor(MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addCode(
CodeBlock.builder()
.beginControlFlow("if ($N.indexOf(':') != -1)", keyComponent)
.addStatement("throw new $T()", ClassName.get(IllegalArgumentException.class))
.endControlFlow()
.build())
.build())
.addSuperinterface(Cloneable.class)
.build(); |
Supporting
Maybe only
Separate
I am not sure whether I prefer this approach here or FabricMC's one. But one advantage with the approach here is that it matches more closely how records work: The record has an implicit canonical constructor with the same parameters as the record components. |
Problem
When creating an annotation type using
TypeSpec#annotationBuilder
, you also often want to specify the@Retention
and@Target
of it. It seems currently you have to do that manually.Suggested feature
It would be useful if there was an
annotationBuilder
overload which takes retention and target values, for example:Open questions:
null
be treatedShould it be forbidden, or should the corresponding meta annotation (e.g.
@Target
) be omitted, so the implicit default is used by the Java compiler?Should probably be forbidden, rest of the API seems to be "null hostile" (?)
targets
list be treatedShould the
@Target
be omitted? Or should a@Target({})
be generated? Which seems valid and only allows usage in other container-like annotations, but does not permit directly annotating something?targets
be aList
orSet
Set
would prevent duplicate values but it would have to make sure the iteration order is deterministic (for example by wrapping asEnumSet
in case it isn't anEnumSet
already)(Maybe also an overload with
List<String> targets
so that you can use the name of aElementType
added in a future JDK version, which is newer than the one you are building on. Not sure how common that is though; could maybe be added later if there is demand for it.)The text was updated successfully, but these errors were encountered: