Git Product home page Git Product logo

auto-value-ignore-hash-equals's Issues

Consider annotation to only use a field in the hashcode and equals

Same idea, but the other way around, something like @OnlyHashEquals or @HashEqualsId to only mark the fields to appear in the hashCode and equals. This would be really useful specially when dealing with models that already have an id field so you wouldn't have to put "@IgnoreHashEquals" in all other fields.

@REggar If you agree to include it here I could make a PR today :)

Generated equals doesn't repect nested classes

When you have a nested class annotated with @AutoValue and @IgnoreHashEquals or @IncludeHashEquals the generated code doesn't compile.

Example:

public class Parent {
    @AutoValue
    static abstract class Test {
        abstract String a();
        @IgnoreHashEquals
        abstract String b();
    }
}

generates

final class AutoValue_Parent_Test extends $AutoValue_Parent_Test {
  AutoValue_Parent_Test(String a, String b) {
    super(a, b);
  }

  @Override
  public final boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (o instanceof Test) {
      Test that = (Test) o;
      return (this.a().equals(that.a()));
    }
    return false;
  }

  @Override
  public final int hashCode() {
    int h = 1;
    h *= 1000003;
    h ^= this.a().hashCode();
    return h;
  }
}

The problematic part is ... if (o instanceof Test) { Test that = (Test) o; ... which has to be ... if (o instanceof Parent.Test) { Parent.Test that = (Parent.Test) o; ....

@REggar I will make a pull request fixing this issue if you don't object.

Breaks generated code when creating a derived builder

@AutoValue
public abstract class Request {

    @IgnoreHashEquals public abstract int getId();

    public static Builder builder() {
        return new AutoValue_Request.Builder();
    }

    public abstract Builder toBuilder();

    @AutoValue.Builder
    public abstract static class Builder {

        public abstract Builder setId(int newId);

        public abstract Request build();
    }
}

The above example generates an incorrect class. The toBuilder method is mistaken for a field, but should be ignored.

error: incompatible types: missing return value

abstract class $AutoValue_AgendaKey extends $$AutoValue_AgendaKey {
  $AutoValue_AgendaKey(int tabIndex) {
    super(tabIndex);
  }

  @Override
  public final boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (o instanceof AgendaKey) {
      AgendaKey that = (AgendaKey) o;
      return; // <-- this is the error
    }
    return false;
  }

  @Override
  public final int hashCode() {
    int h = 1;
    return h;
  }
}

using @IgnoreHashEquals annotation, this is the generated file, and it does not compile. I have only 1 property in this object and it is ignored.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.