Custom hashCode and equals method on managed class does not work

software-model

(Daniel Lacasse) #1

Suppose the following simplistic code in build.gradle:

@Managed
abstract class Foo {
  @Override
  int hashCode() {
    return 42
  }
}

class Rules extends RuleSource {
  @Model
  void foo(Foo model) {}
}

apply plugin: Rules

model {
  tasks {
    bar(Task) {
      doLast {
        println $.foo.getClass()
        assert 42 == $.foo.hashCode()
      }
    }
  }
}

Invoking gradlew bar will result in printing class Foo$NodeView for the println statement and asserting.

It seems the hashCode and equals method implemented inside the Managed class NodeView doesn’t correctly forward to the abstract class Foo (or any similar case). This is problematic in a sense it express an unexpected behavior (transparency) of the model between the @Managed class and the usage of those class in the code. For example, this prevent from implementing model class to be used inside HashMap like you could do with normal Java coding.

Is this the expected behavior? Will the model move into the direction of being more transparent?

Thanks for the comment!

Daniel