Created
September 30, 2010 14:28
-
-
Save mattnworb/604664 to your computer and use it in GitHub Desktop.
answer to http://stackoverflow.com/questions/3831004/java-generics-and-wildcards-how-to-make-this-code-compile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GenericsTest { | |
public void doesNotCompile() { | |
Container<String> container = new Container<String>(); | |
assertThat(container, hasSomethingWhich(is("foo"))); | |
} | |
public static class Container<T> { | |
public boolean hasSomethingMatching(Matcher<? super T> matcher) { | |
T something = null; // here is some application logic | |
return matcher.matches(something); | |
} | |
} | |
public static <T> Matcher<Container<T>> hasSomethingWhich(final Matcher<? super T> matcher) { | |
return new TypeSafeMatcher<Container<T>>() { | |
@Override | |
protected boolean matchesSafely(Container<T> container) { | |
return container.hasSomethingMatching(matcher); | |
} | |
@Override | |
public void describeTo(Description description) { | |
// TODO Auto-generated method stub | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment