ClassConverter implements PropertyConverter<Class<?>> { @Override public Class<?> convert(String value, ConversionContext ctx) { if(value==null){ return null; } ctx.addSupportedFormats(getClass(),"<fullyQualifiedClassName>"); String trimmed = Objects.requireNonNull(value).trim(); try { return Class.forName(trimmed, false, Thread.currentThread().getContextClassLoader()); } catch(Exception e) { LOG.finest("Class not found in context CL: " + trimmed); } try { return Class.forName(trimmed, false, ClassConverter.class.getClassLoader()); } catch(Exception e) { LOG.finest("Class not found in ClassConverter's CL: " + trimmed); } try { return Class.forName(trimmed, false, ClassLoader.getSystemClassLoader()); } catch(Exception e) { LOG.finest("Class not found in System CL (giving up): " + trimmed); return null; } }  @Override Class<?> convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testConvert_Class() throws Exception { ClassConverter converter = new ClassConverter(); assertThat(BigDecimal.class).isEqualTo(converter.convert("java.math.BigDecimal", context)); }
@Test public void testConvert_Class_WithSpaces() throws Exception { ClassConverter converter = new ClassConverter(); assertThat(BigDecimal.class).isEqualTo(converter.convert(" java.math.BigDecimal\t", context)); }
@Test public void testConvert_Class_WithSpacesBefore() throws Exception { ClassConverter converter = new ClassConverter(); assertThat(BigDecimal.class).isEqualTo(converter.convert(" java.math.BigDecimal", context)); }
@Test public void testConvert_Class_WithSpacesAfter() throws Exception { ClassConverter converter = new ClassConverter(); assertThat(BigDecimal.class).isEqualTo(converter.convert("java.math.BigDecimal ", context)); }
@Test public void testConvert_NotPresent() throws Exception { ClassConverter converter = new ClassConverter(); assertThat(converter.convert("", context)).isNull(); assertThat(converter.convert(null, context)).isNull(); }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Class.class)).build(); ClassConverter converter = new ClassConverter(); converter.convert("", context); assertThat(context.getSupportedFormats()).contains("<fullyQualifiedClassName> (ClassConverter)"); }