View Javadoc

1   package net.sf.josceleton.commons.reflect;
2   
3   import java.lang.reflect.Constructor;
4   
5   import net.sf.josceleton.commons.exception.InvalidArgumentException;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  class DynamicInstantiatorImpl implements DynamicInstantiator {
11  	
12  	private static final Log LOG = LogFactory.getLog(DynamicInstantiatorImpl.class);
13  	
14  	DynamicInstantiatorImpl() {
15  		// package-private constructor to reduce visibility
16  	}
17  
18  	/** {@inheritDoc} from {@link DynamicInstantiator} */
19  	@Override public final <T> T create(final ClassAdapter<T> clazz, final Object... arguments) {
20  		LOG.debug("instantiate(clazz.name=" + clazz.getName() + ", arguments.length=" + arguments.length + ")");
21  		
22  		for (int i = 0; i < arguments.length; i++) {
23  			final Object currentArgument = arguments[i];
24  			if(currentArgument == null) {
25  				throw InvalidArgumentException.newNotNull("arguments[]@" + i);
26  			}
27  		}
28  		
29  		final Constructor<T> constructor = ReflectUtil.findConstructor(clazz, arguments);
30  		
31  	    try {
32  			return constructor.newInstance(arguments);
33  			// !!! bad design; simple exception handling ...
34  			//     done to increase coverage and avoid having to test for each and every exception ;)
35  		} catch (final Exception e) {
36  			throw DynamicInstantiationException.newForInstantiation(clazz, arguments, e);
37  		}
38  	}
39  
40  }