Skip to content

Inner builder List attribute compiler warning "unchecked call as a member of the raw type" #1668

@jdoylei

Description

@jdoylei

Using useInnerClassBuilders, if I have a JSON schema array property that becomes a List Java attribute, the builder class has a withXXX method like this:

   public static ApplicationBuilderBase builder() {
      return new ApplicationBuilder();
   }

   public static class ApplicationBuilder extends ApplicationBuilderBase<Application> {
      public ApplicationBuilder() {
      }
   }

   public abstract static class ApplicationBuilderBase<T extends Application> {
   ...
      public ApplicationBuilderBase withDeploymentArtifacts(List<DeploymentArtifact> deploymentArtifacts) {
         this.instance.deploymentArtifacts = deploymentArtifacts;
         return this;
      }

But it's difficult to use it without a compiler warning. E.g. this statement:

        Application application = Application
                .builder()
                .withApplicationDescription("x")
                .withDeploymentArtifacts(List.of( 
                DeploymentArtifact
                        .builder()
                        .withDeploymentArtifactName("x")
                        .build(),
                DeploymentArtifact
                        .builder()
                        .withDeploymentArtifactName("y")
                        .build()
        ))
                .build();

Emits this compiler warning:

lib/src/main/java/com/User.java:[53,41] unchecked call to withDeploymentArtifacts(java.util.List<DeploymentArtifact>) as a member of the raw type Application.ApplicationBuilderBase

I can use a cast to avoid the warning, but it's pretty awkward to parenthesize correctly, and I think it would stop being readable if you had to add multiple casts for multiple withXXX calls in a row:

        Application application = ((Application.ApplicationBuilder)Application
                .builder()
                .withApplicationDescription("x")) // Here is where you close the parens
                .withDeploymentArtifacts(List.of( 
                DeploymentArtifact
                        .builder()
                        .withDeploymentArtifactName("x")
                        .build(),
                DeploymentArtifact
                        .builder()
                        .withDeploymentArtifactName("y")
                        .build()
        ))
                .build();

Any advice is appreciated!

Thanks for the great plugin!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions