Parsers implements ParserOracle { @Override public <T> Parser<T> get(TypeToken<T> type) throws IllegalArgumentException { Parser<?> parser; Class<?> explicitClass = type.getRawType(); while (((parser = registry.get(explicitClass)) == null) && (explicitClass != null)) { explicitClass = explicitClass.getSuperclass(); } checkArgument(parser != null, "No parser found for " + type); @SuppressWarnings("unchecked") Parser<T> parserT = (Parser<T>) parser; return parserT; } Parsers(Map<Class<?>, Parser<?>> parsers); @Override Parser<T> get(TypeToken<T> type); static final Splitter MULTI_VALUE_SPLITTER; }
@Test public void testParseTypeFamily() { assertNotNull(defaultParsers.get(TypeToken.of(String.class))); class Credentials extends Pair<String, String> { public Credentials(String first, String second) { super(first, second); } } Parser parser = defaultParsers.get(TypeToken.of(Credentials.class)); assertNotNull(parser); assertSame(parser, defaultParsers.get(TypeToken.of(Pair.class))); }
@Test(expected = IllegalArgumentException.class) public void testNoParser() { class NoParserForMe { } assertNull(defaultParsers.get(TypeToken.of(NoParserForMe.class))); }