Git Product home page Git Product logo

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

AutoValue: Ignore Hash Equals Extension Build Status

An extension for Google's AutoValue that omits @IgnoreHashEquals field values from hashCode() and equals().

Basic Usage

Include the extension in your project, define an @IgnoreHashEquals annotation, and apply it to any fields that you wish to be ignored from the generated hashCode and equals implementation.

@Retention(SOURCE)
@Target({METHOD, PARAMETER, FIELD})
public @interface IgnoreHashEquals {
}
@AutoValue
public abstract class User {
  public abstract String id();
  @IgnoreHashEquals public abstract String name();
}

When you call hashCode() or equals() any properties with @IgnoreHashEquals will be ignored from the calculation.

@IncludeHashEquals Usage

Include the extension in your project, define an @IncludeHashEquals annotation, and apply it to any fields that you wish to be included from the generated hashCode and equals implementation.

@Retention(SOURCE)
@Target({METHOD, PARAMETER, FIELD})
public @interface IncludeHashEquals {
}
@AutoValue
public abstract class User {
  @IncludeHashEquals public abstract String id();
  public abstract String name();
}

When you call hashCode() or equals() any properties without @IncludeHashEquals will be ignored from the calculation.

Download

Add a Gradle dependency:

apt 'com.github.reggar:auto-value-ignore-hash-equals:1.1.4'

(Using the android-apt plugin)

or Maven:

<dependency>
  <groupId>com.github.reggar</groupId>
  <artifactId>auto-value-ignore-hash-equals</artifactId>
  <version>1.1.4</version>
  <scope>provided</scope>
</dependency>

Snapshots of the development version are available in Sonatype's snapshots repository.

Notes

This library is heavily inspired by Square's AutoValue: Redacted Extension.

License

Copyright 2017 Robert Eggar.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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

Contributors

reggar avatar snclever avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

snclever zhuinden

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.