001 package org.picocontainer.converters; 002 003 import org.picocontainer.converters.Converter; 004 005 import java.lang.reflect.Constructor; 006 import java.lang.reflect.InvocationTargetException; 007 008 /** 009 * Converts a value to an object via its single-String constructor. 010 */ 011 public class NewInstanceConverter implements Converter<Object> { 012 private Constructor<?> c; 013 014 public NewInstanceConverter(Class<?> clazz) { 015 try { 016 c = clazz.getConstructor(String.class); 017 } catch (NoSuchMethodException e) { 018 } 019 } 020 public Object convert(String paramValue) { 021 if ( c == null ){ 022 return null; 023 } 024 try { 025 return c.newInstance(paramValue); 026 } catch (IllegalAccessException e) { 027 } catch (InvocationTargetException e) { 028 } catch (InstantiationException e) { 029 } 030 return null; 031 } 032 }