arashja / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

hasEntry matcher needs proper variance declarations #103

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The return type of the hasEntry matcher is invariant on it's type
parameters which prevents javac from inferring compatible types. This
prevents writing

  public void testFoo() {
    Map<String, Number> m = new HashMap<String,Number>();
    Integer foo = new Integer(6);
    m.put("foo", foo);
    MatcherAssert.assertThat(m, Matcher.hasEntry("foo", foo));
  }

instead, you have to write

  MatcherAssert.assertThat(m, Matcher.<String,Number>hasEntry("foo", foo));

or 

  MatcherAssert.assertThat(m, Matcher.hasEntry("foo", (Number)foo));

If the signature for hasEntry were instead

  public static <K,V> Matcher<Map<? super K,? super V>> hasEntry(K k, V v)

the original example would work fine. This same problem likely applies to
other collection matchers like hasItem, hasKey, etc.

Original issue reported on code.google.com by gere...@gmail.com on 7 Jan 2010 at 9:50

GoogleCodeExporter commented 8 years ago
tag Java

Original comment by t.denley on 19 May 2012 at 8:12