assertj-assertions-generator icon indicating copy to clipboard operation
assertj-assertions-generator copied to clipboard

Handle generics of real numbers

Open gvauvert opened this issue 10 years ago • 3 comments

The generation of asserts for generics does not handle reals (Double or Float) as expected: Double/Float are processed as Objects, while they should be processed as reals. Ex:

class Of<T> {
  T value;
}
class OfDouble extends Of<Double> {
}

Actual result:

class OfDoubleAssert extends AbstractAssert<OfDoubleAssert , OfDouble> {
  public OfDoubleAssert hasValue(Comparable avg) {
     ...
  }
}

Expected result:

class OfDoubleAssert extends AbstractAssert<OfDoubleAssert , OfDouble> {
  public OfDoubleAssert hasValue(Comparable avg) {
     ...
  }

  public Double hasValueCloseTo(Double value, Double offset) {
     ...
  }

}

Thanks !

gvauvert avatar Sep 07 '15 10:09 gvauvert

I haven't done any experiment but I think that is due to your generic classes, at runtime generic types are lost, since the assertions generator introspects classes, it is not aware that it should deal with Double.

If you were to write your OfDoublewithout generics, it should works fine (I'm not saying you can or you should, your design probably uses generics).

I'm gonna investigate the issue anyway to confirm my assumption and see if can find the correct type at runtime anyway (I have my doubts though it is possible).

I have priority work to do on assertj-core 3.2.0 so I won't work soon on this issue (just to be clear, don't expect quick feedback on this one).

joel-costigliola avatar Sep 07 '15 16:09 joel-costigliola

You are right, generic information is lost at runtime (for compatibility reasons), but can be found by reflection, even if it is not trivial to handle hierarchies: http://www.artima.com/weblogs/viewpost.jsp?thread=208860 https://github.com/jhalterman/typetools

gvauvert avatar Sep 08 '15 06:09 gvauvert

Supporting generic is really complicated see: https://github.com/joel-costigliola/assertj-assertions-generator/issues/92#issuecomment-325288696.

joel-costigliola avatar Aug 28 '17 09:08 joel-costigliola