CharConverter implements PropertyConverter<Character> { @Override public Character convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(),"\\'<char>\\'", "<char>", "<charNum>"); if(value==null){ return null; } String trimmed = value.trim(); if(trimmed.isEmpty()){ return null; } if(trimmed.startsWith("'")) { try { if (trimmed.length() == 1){ return '\''; } trimmed = trimmed.substring(1, trimmed.length() - 1); if (trimmed.isEmpty()) { return null; } return trimmed.charAt(0); } catch (Exception e) { LOG.finest("Invalid character format encountered: '" + value + "', valid formats are 'a', 101 and a."); return null; } } try { Integer val = Integer.parseInt(trimmed); return (char) val.shortValue(); } catch (Exception e) { LOG.finest("Character format is not numeric: '" + value + "', using first character."); return trimmed.charAt(0); } }  @Override Character convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Character.class)).build(); CharConverter converter = new CharConverter(); converter.convert("", context); assertThat(context.getSupportedFormats()).contains("<char> (CharConverter)", "\\'<char>\\' (CharConverter)"); }