我有一個匹配器,並且我想讓起訴我的對象是正確的類型。例如。長或字符串。我如何獲得Hamcrest的Matcher類?
void expect(String xpath, Matcher matcher) {
String actual = fromXpath(xpath);
// convert actual into correct type for matcher
assertThat(actual, matcher);
}
我想要的方法如Matcher.getType
。所以我可以做類似
if (matcher.getType().equals(Long.class)) {
long actual = Long.parseString(fromXpath(xpath));
}
但我看不到我的生活如何得到匹配。
你可能想看看http://stackoverflow.com/questions/3403909/get-generic-type-運行時間爲 我認爲你所要求的是參數類型信息。在你匹配Matcher的情況下,我不知道你是否能夠做到。 –
我認爲這就是Java中的Type Erasure,我錯了嗎?http://docs.oracle.com/javase/tutorial/java/generics/erasure.html – wliao