⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectcreator.java

📁 以前做的一个j2ee的项目
💻 JAVA
字号:
package gov.gdlt.ssgly.taxcore.taxblh.gzda.service.xmlinterface.impl;

import gov.gdlt.ssgly.taxcore.comm.bizdelegate.SsglyDelegate;
import gov.gdlt.ssgly.taxcore.comm.bizinterface.ISsglyService;
import java.lang.reflect.*;
public final class ObjectCreator {

        /**
 *
 */
public ObjectCreator() {
        super();
        // TODO Auto-generated constructor stub
}
/**
* Instantaite an Object from a given class name
* @param className full qualified name of the class
* @return the instantaited Object
* @exception java.lang.Exception if instantiation failed
*/
public static Object createObject(String className)
    throws Exception
{
    return createObject( Class.forName(className));
}

/**
* Instantaite an Object instance
* @param classObject Class object representing the object type to be instantiated
* @return the instantaied Object
* @exception java.lang.Exception if instantiation failed
*/
public static Object createObject(Class classObject)
    throws Exception
{
    Object object = null;
    return  classObject.newInstance();
}

/**
* Instantaite an Object instance, requires a constructor with parameters
* @param className full qualified name of the class
* @param params an array including the required parameters to instantaite the object
* @return the instantaited Object
* @exception java.lang.Exception if instantiation failed
*/
public static Object createObject(String className, Object[] params)
    throws Exception
{
     return createObject( Class.forName(className), params);
}

/**
* Instantaite an Object instance, requires a constractor with parameters
* @param classObject, Class object representing the object type to be instantiated
* @param params an array including the required parameters to instantaite the object
* @return the instantaied Object
* @exception java.lang.Exception if instantiation failed
*/
public static Object createObject(Class classObject, Object[] params)
    throws Exception
{
    Constructor[] constructors = classObject.getConstructors();
    Object object = null;
    for(int counter = 0; counter<constructors.length; counter++)
    {
      try
      {
         object = constructors[counter].newInstance(params);
      }
      catch(Exception e)
      {
        if(e instanceof InvocationTargetException)
          ((InvocationTargetException)e).getTargetException().printStackTrace();
              //do nothing, try the next constructor
      }
    }
    if(object == null)
              throw new InstantiationException();
    return object;
}

public static Object getObjectFromXml(String id) throws Exception{

       //String classname=ReadXml.getInstance().readInit(id,"class");
       //ISsglyService serve=new SsglyDelegate();
       String classname=id;
       //System.out.println("classname="+classname);
       return createObject(classname);
}
public static void main(String[] args) {

       Object Oclass=null;
       try {
                Oclass = ObjectCreator.getObjectFromXml("select");
       } catch (Exception e) {
             // TODO 自动生成 catch 块
             e.printStackTrace();
       }



}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -