Skip to content
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

cast warning with -Xlint enabled (becomes error with -Werror) #1491

Open
mikewacker opened this issue Nov 23, 2023 · 2 comments
Open

cast warning with -Xlint enabled (becomes error with -Werror) #1491

mikewacker opened this issue Nov 23, 2023 · 2 comments

Comments

@mikewacker
Copy link

Here is a minimalistic interface that will repro the issue.

@Value.Immutable
@JsonSerialize(as = ImmutableExample.class)
@JsonDeserialize(as = ImmutableExample.class)
public interface Example {

    final class Builder extends ImmutableExample.Builder {}
}

Here's the warning(/error with -Werror enabled):

warning: [cast] redundant cast to ImmutableExample
    return (ImmutableExample) builder.build();

You need both the JSON annotations and the Builder class to trigger this warning.


Here's the offending generated snippet:

  @Deprecated
  @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
  static ImmutableExample fromJson(Json json) {
    Example.Builder builder = new Example.Builder();
    return (ImmutableExample) builder.build();
  }

builder.build() actually returns an ImmutableExample, not an Example. Hence, the cast is redundant.

If you remove the Builder class from Example, the snippet looks like this instead:

  @Deprecated
  @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
  static ImmutableExample fromJson(Json json) {
    ImmutableExample.Builder builder = ImmutableExample.builder();
    return builder.build();
  }

See also #672; @SuppressWarnings({"all"}) does not suppress -Xlint warnings for some reason.

In this case, it should be fairly easy to fix this, and the cast warning is probably not one you want to disable. (Whereas the processing warning in #672 is often one that gets disabled anyway.)

@mikewacker
Copy link
Author

There is a workaround:

Apply overshadowImplementation = true as part of a @Value.Style. (Which may be a good idea anyway.)

@xenoterracide
Copy link

same or related, and overshadow didn't help

/home/xeno/IdeaProjects/spring-app-commons/module/jpa-security/build/generated/sources/annotationProcessor/java/testFixtures/com/xenoterracide/model/security/UserTestDataBuilder.java:54: warning: [cast] redundant cast to Optional<String>
    this.name = (Optional<String>) Objects.requireNonNull(name, "name");
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Value.Style(typeBuilder = "*TestDataBuilder", newBuilder = "create", jakarta = true, overshadowImplementation = true)
final class UserTestDataBuilders {

  private UserTestDataBuilders() {}

  @Builder.Factory
  static User user(@Nonnull Optional<String> name

perhaps the simplest upfront way would simply be to add a SuppressWarnings("cast") to the top of the generated class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants