Java XML String Transform createObject(String classname)

Here you can find the source of createObject(String classname)

Description

create an object using reflection Examples JDReflectionUtil.createObject("com.ibm.db2.jcc.DB2XADataSource") callMethod_V(ds, "setTranslateHex", "character");

License

Open Source License

Declaration


public static Object createObject(String classname) throws Exception 

Method Source Code

//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 ");
        }

    }
}

Related

  1. convertPlist(File info_plist_file, String script_url, Map script_parameters)
  2. convertResult(StreamResult result, StringWriter writer)
  3. convertValidatorResult(Result result, StringWriter writer)
  4. createElement(Node node, String name, String value, Map attributes)
  5. createFullReportLog(String sessionLogDir)
  6. createOrUpdateConnection(String fileName, String directory, String communityId, String serviceName, String serviceUrl, String defaultVersion, Logger log)
  7. createSettingsXml(Map props)
  8. createSource(final String message)
  9. createSource(String msg)