Here you can find the source of createObject(String classname)
public static Object createObject(String classname) throws Exception
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Constructor; public class Main { /**//from w w w . j av a 2s. co m * create an object using reflection * Examples * * JDReflectionUtil.createObject("com.ibm.db2.jcc.DB2XADataSource") * * callMethod_V(ds, "setTranslateHex", "character"); */ public static Object createObject(String classname) throws Exception { Class objectClass1 = Class.forName(classname); Class[] noArgTypes = new Class[0]; Object[] noArgs = new Object[0]; Object newObject = null; try { Constructor constructor = objectClass1.getConstructor(noArgTypes); newObject = constructor.newInstance(noArgs); } catch (java.lang.reflect.InvocationTargetException ite) { handleIte(ite); } return newObject; } /** * create an object using reflection * Examples * * JDReflectionUtil.createObject("com.ibm.db2.app.DB2RowId", testArray) * */ public static Object createObject(String classname, byte[] arg) throws Exception { Class objectClass1 = Class.forName(classname); Class[] oneArgTypes = new Class[1]; oneArgTypes[0] = Class.forName("[B"); Object[] oneArgs = new Object[1]; oneArgs[0] = arg; Object newObject = null; try { Constructor constructor = objectClass1.getDeclaredConstructor(oneArgTypes); //@pdc find protected constructor also constructor.setAccessible(true); //@PDA allo call of protected. newObject = constructor.newInstance(oneArgs); } catch (java.lang.reflect.InvocationTargetException ite) { handleIte(ite); } return newObject; } /** * create an object using reflection * Examples * * JDReflectionUtil.createObject("javax.xml.transform.stax.StAXSource", "javax.xml.stream.XMLStreamReader", xmlStreamReader); * */ public static Object createObject(String classname, String parameterClass, Object arg) throws Exception { Class objectClass1 = Class.forName(classname); Class[] oneArgTypes = new Class[1]; oneArgTypes[0] = Class.forName(parameterClass); Object[] oneArgs = new Object[1]; oneArgs[0] = arg; Object newObject = null; try { Constructor constructor = objectClass1.getDeclaredConstructor(oneArgTypes); constructor.setAccessible(true); newObject = constructor.newInstance(oneArgs); } catch (java.lang.reflect.InvocationTargetException ite) { handleIte(ite); } return newObject; } public static void handleIte(java.lang.reflect.InvocationTargetException ite) throws Exception { Throwable target = ite.getTargetException(); if (target instanceof Exception) { throw (Exception) target; } else { target.printStackTrace(); throw new Exception("Throwable " + target.toString() + " encountered. See STDOUT for stack trace "); } } }