1 package com.jdon.util; 2 3 import java.lang.reflect.*; 4 5 11 12 public final class ObjectCreator { 13 16 private ObjectCreator() { 17 } 18 19 25 public static Object createObject(String className) throws Exception { 26 return createObject(Class.forName(className)); 27 } 28 29 35 public static Object createObject(Class classObject) throws Exception { 36 Object object = null; 37 return classObject.newInstance(); 38 } 39 40 47 public static Object createObject(String className, Object [] params) throws 48 Exception { 49 return createObject(Class.forName(className), params); 50 } 51 52 59 public static Object createObject(Class classObject, Object [] params) throws 60 Exception { 61 Constructor[] constructors = classObject.getConstructors(); 62 Object object = null; 63 for (int counter = 0; counter < constructors.length; counter++) { 64 try { 65 object = constructors[counter].newInstance(params); 66 } catch (Exception e) { 67 if (e instanceof InvocationTargetException) 68 ( (InvocationTargetException) e).getTargetException().printStackTrace(); 69 } 71 } 72 if (object == null) 73 throw new InstantiationException (); 74 return object; 75 } 76 } | Popular Tags |