gabrielittner/auto-value-with

The generated class uses an inexistent constructor when using AutoValue's Builder

Closed this issue · 1 comments

Given the example below:

import com.google.auto.value.AutoValue;

@AutoValue
abstract class Animal {
  abstract String name();
  abstract int numberOfLegs();
  abstract Animal withName(String name);
  abstract Builder toBuilder();

  static Builder builder() {
    return new AutoValue_Animal.Builder();
  }

  @AutoValue.Builder
  abstract static class Builder {
    abstract Builder name(String value);
    abstract Builder numberOfLegs(int value);
    abstract Animal build();
  }
}

(Usage of toBuilder is documented here: https://github.com/google/auto/blob/master/value/userguide/builders-howto.md#to_builder )

We're getting the following error when the auto-value-with class gets generated:

error: constructor AutoValue_Animal in class AutoValue_Animal cannot be applied to given types;
      return new AutoValue_Animal(
             ^
  required: String,int,Builder
  found: String,int
  reason: actual and formal argument lists differ in length

error: constructor $AutoValue_Animal in class $AutoValue_Animal cannot be applied to given types;
    super(name, numberOfLegs, toBuilder);
    ^
  required: String,int
  found: String,int,Builder
  reason: actual and formal argument lists differ in length

Looking at the generated code this is what we get:

final class AutoValue_Animal extends $AutoValue_Animal {
  AutoValue_Animal(String name, int numberOfLegs, Animal.Builder toBuilder) {
    super(name, numberOfLegs, toBuilder);
  }

  @Override
  public final AutoValue_Animal withName(String name) {
    return new AutoValue_Animal(name, numberOfLegs(), toBuilder());
  }
}

So seems like it's trying to pass in the Builder as constructor parameter but it shouldn't.

This is a known issue in AutoValue itself google/auto#281 Take a look at the comments for a workaround.