Suppress unchecked warnings in generated builders#247
Conversation
| .build()) | ||
| .addAnnotation( | ||
| AnnotationSpec.builder(SuppressWarnings.class) | ||
| .addMember("value", "$S", "unchecked") |
There was a problem hiding this comment.
At a minimum I think we need a comment here justifying why we add this to all builders... I just ran it locally and got:
> Task :conjure-java-core:compileIntegrationInputJava FAILED
/Users/dfox/conjure-java/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantOptionalExample.java:107: warning: [unchecked] unchecked cast
this.item = (Optional<Object>) Preconditions.checkNotNull(item, "item cannot be null");
^
required: Optional<Object>
found: Optional<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
error: warnings found and -Werror specified
1 error
1 warning
|
It feels pretty gross to be adding the Could we have a stab at adding it to exactly the right methods? @JsonSetter("item")
+ @SuppressWarnings("unchecked")
public Builder item(Optional<?> item) {
this.item = (Optional<Object>) Preconditions.checkNotNull(item, "item cannot be null");
return this;
}Also, if we want conjure-java to pass without warnings then I think this PR needs to also enable |
|
Rather than adding suppressions to generated code, should we provide an |
|
TBH I'd really like to keep conjure-lib as minimal as possible. If we could target these annotations accurately then most generated code wouldn't be affected, it would only be for people using |
|
Closing out as I don't think it's really OK to suppress warnings on all builders - lmk if you want to have another stab @pkoenig10! |
Before this PR
Conjure generated projects with covariant optional setters can not be compile with
-Xlint:unchecked. This is because the improvements made in #9 requires casting optionals in certain cases.After this PR
Conjure generated builders now suppress unchecked warnings. This seems reasonable since we control the code generation here.
We could attempt to be more targeted and only add this annotation to covariant optional setters but: