Home | History | Annotate | Download | only in internal
      1 package org.testng.internal;
      2 
      3 import org.testng.IObjectFactory;
      4 import org.testng.TestNGException;
      5 
      6 import java.lang.reflect.Constructor;
      7 
      8 /**
      9  * Default factory for test creation.
     10  * Note that if no constructor is found matching the specified parameters,
     11  * this factory will try to invoke a constructor that takes in a string object
     12  *
     13  * @author Hani Suleiman
     14  *         Date: Mar 6, 2007
     15  *         Time: 12:00:27 PM
     16  * @since 5.6
     17  */
     18 public class ObjectFactoryImpl implements IObjectFactory {
     19 
     20   /**
     21    *
     22    */
     23   private static final long serialVersionUID = -4547389328475540017L;
     24 
     25   @Override
     26   public Object newInstance(Constructor constructor, Object... params) {
     27     try {
     28       constructor.setAccessible(true);
     29       return constructor.newInstance(params);
     30     }
     31     catch (IllegalAccessException ex) {
     32       return ClassHelper.tryOtherConstructor(constructor.getDeclaringClass());
     33     }
     34     catch (InstantiationException ex) {
     35       return ClassHelper.tryOtherConstructor(constructor.getDeclaringClass());
     36     }
     37     catch(Exception ex) {
     38       throw new TestNGException("Cannot instantiate class "
     39           + (constructor != null
     40                 ? constructor.getDeclaringClass().getName()
     41                 : ": couldn't find a suitable constructor"),
     42                     ex);
     43     }
     44   }
     45 }
     46