Example usage for java.lang Class getConstructor

List of usage examples for java.lang Class getConstructor

Introduction

In this page you can find the example usage for java.lang Class getConstructor.

Prototype

@CallerSensitive
public Constructor<T> getConstructor(Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

Usage

From source file:egovframework.rte.fdl.string.EgovObjectUtil.java

/**
 * ? ?  ?? ?? ? . ) Class <?>//from   ww w  .  ja  v  a  2s.c  o m
 * clazz = EgovObjectUtil.loadClass(this.mapClass);
 * Constructor <?> constructor =
 * clazz.getConstructor(new Class
 * []{DataSource.class, String.class}); Object []
 * params = new Object []{getDataSource(),
 * getUsersByUsernameQuery()};
 * this.usersByUsernameMapping =
 * (EgovUsersByUsernameMapping)
 * constructor.newInstance(params);
 * @param className
 * @return
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws Exception
 */
public static Object instantiate(String className, String[] types, Object[] values)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception {
    Class<?> clazz;
    Class<?>[] classParams = new Class[values.length];
    Object[] objectParams = new Object[values.length];

    try {
        clazz = loadClass(className);

        for (int i = 0; i < values.length; i++) {
            classParams[i] = loadClass(types[i]);
            objectParams[i] = values[i];
        }

        Constructor<?> constructor = clazz.getConstructor(classParams);
        return constructor.newInstance(values);

    } catch (ClassNotFoundException e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is can not instantialized.");
        throw new ClassNotFoundException();
    } catch (InstantiationException e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is can not instantialized.");
        throw new InstantiationException();
    } catch (IllegalAccessException e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is not accessed.");
        throw new IllegalAccessException();
    } catch (Exception e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is not accessed.");
        throw new Exception(e);
    }
}

From source file:info.magnolia.module.admininterface.dialogs.ConfiguredDialog.java

/**
 * @param paragraph/*from  w w  w . j a v a2s . com*/
 * @param configNode2
 * @param request
 * @param response
 * @param class1
 * @return
 */
public static ConfiguredDialog getConfiguredDialog(String name, Content configNode, HttpServletRequest request,
        HttpServletResponse response, Class defaultClass) {
    // get class name
    String className = null;
    try {
        Class handlerClass = defaultClass;
        try {
            className = configNode.getNodeData("class").getString(); //$NON-NLS-1$
            if (StringUtils.isNotEmpty(className)) {
                handlerClass = Class.forName(className);
            }
        } catch (Exception e) {
            log.error(MessageFormat.format("Unable to load class {0}", new Object[] { className })); //$NON-NLS-1$
        }
        Constructor constructor = handlerClass.getConstructor(new Class[] { String.class,
                HttpServletRequest.class, HttpServletResponse.class, Content.class });
        return (ConfiguredDialog) constructor.newInstance(new Object[] { name, request, response, configNode });
    } catch (Exception e) {
        log.error("can't instantiate dialog: ", e); //$NON-NLS-1$
        return null;
    }
}

From source file:com.mh.commons.utils.Reflections.java

/**
 * /* w  ww.  ja  va  2  s .  c  o  m*/
 * @param className
 * @param args
 * @return
 * @throws ClassNotFoundException
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws IllegalArgumentException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static Object newInstance(String className, Object[] args)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
        InstantiationException, IllegalAccessException, InvocationTargetException {
    Class newoneClass = Class.forName(className);
    Class[] argsClass = new Class[args.length];
    for (int i = 0, j = args.length; i < j; i++) {
        argsClass[i] = args[i].getClass();
    }
    Constructor cons = newoneClass.getConstructor(argsClass);
    return cons.newInstance(args);
}

From source file:com.qmetry.qaf.automation.ui.UiDriverFactory.java

private static WebDriver getDriverObj(Class<? extends WebDriver> of, Capabilities capabilities, String urlStr) {
    try {//  w ww. ja  va 2s.c o  m
        Constructor<? extends WebDriver> constructor = of.getConstructor(Capabilities.class);
        return constructor.newInstance(capabilities);
    } catch (Exception e) {
        if (e.getCause() != null && e.getCause() instanceof WebDriverException) {
            throw (WebDriverException) e.getCause();
        }
        try {
            return of.newInstance();
        } catch (Exception e1) {
            try {

                Constructor<? extends WebDriver> constructor = of.getConstructor(URL.class, Capabilities.class);

                return constructor.newInstance(new URL(urlStr), capabilities);
            } catch (InvocationTargetException e2) {
                throw new WebDriverException(e2);
            } catch (InstantiationException e2) {
                throw new WebDriverException(e2);
            } catch (IllegalAccessException e2) {
                throw new WebDriverException(e2);
            } catch (IllegalArgumentException e2) {
                throw new WebDriverException(e2);
            } catch (MalformedURLException e2) {
                throw new WebDriverException(e2);
            } catch (NoSuchMethodException e2) {
                throw new WebDriverException(e2);
            } catch (SecurityException e2) {
                throw new WebDriverException(e2);
            }
        }
    }
}

From source file:de.julielab.jcore.utility.JCoReAnnotationTools.java

/**
 * returns an annotation object (de.julielab.jcore.types.annotation) of the type specified by fullEntityClassName.
 * This is done by means of dynamic class loading and reflection.
 * //from w w  w.j av a  2 s  .c  o m
 * @param aJCas
 *            the jcas to which to link this annotation object
 * @param fullAnnotationClassName
 *            the full class name of the new annotation object
 * @return
 */
public static Annotation getAnnotationByClassName(JCas aJCas, String fullAnnotationClassName)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
        InstantiationException, IllegalAccessException, InvocationTargetException {

    Class[] parameterTypes = new Class[] { JCas.class };
    Class myNewClass = Class.forName(fullAnnotationClassName);
    Constructor myConstructor = myNewClass.getConstructor(parameterTypes);
    Annotation anno = (Annotation) myConstructor.newInstance(aJCas);
    return anno;
}

From source file:com.igormaznitsa.upom.UPomModel.java

private static Map cloneMap(final Map map) throws Exception {
    final Class mapClass = map.getClass();
    final Constructor constructor = mapClass.getConstructor(Map.class);
    return (Map) constructor.newInstance(map);
}

From source file:Main.java

static public Object newInstance(String className, Object... args) throws Exception {
    Class<?> newoneClass = Class.forName(className);
    Class<?>[] argsClass = null;

    if (args != null && args.length > 0) {
        argsClass = new Class<?>[args.length];

        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
            if (argsClass[i] == Integer.class) {
                argsClass[i] = int.class;
            } else if (argsClass[i] == Boolean.class) {
                argsClass[i] = boolean.class;
            }//w w  w.  j  a  v a  2 s .c o  m
        }
    }

    Constructor<?> cons = newoneClass.getConstructor(argsClass);

    return cons.newInstance(args);
}

From source file:com.cenrise.test.azkaban.Utils.java

/**
 * Call the class constructor with the given arguments
 *
 * @param c The class/* w  ww  . j a  v  a2 s .  c  o  m*/
 * @param args The arguments
 * @return The constructed object
 */
public static Object callConstructor(final Class<?> c, final Class<?>[] argTypes, final Object[] args) {
    try {
        final Constructor<?> cons = c.getConstructor(argTypes);
        return cons.newInstance(args);
    } catch (final InvocationTargetException e) {
        throw getCause(e);
    } catch (final IllegalAccessException e) {
        throw new IllegalStateException(e);
    } catch (final NoSuchMethodException e) {
        throw new IllegalStateException(e);
    } catch (final InstantiationException e) {
        throw new IllegalStateException(e);
    }
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.common.vbb.impl.util.VisualVariableHelper.java

private static EClassifier getEDataType(EPackage vvEPackage, Class<?> clazz) {
    for (EClassifier eClassifier : EcorePackage.eINSTANCE.getEClassifiers()) {
        if (eClassifier instanceof EDataType && eClassifier.getInstanceClass().equals(clazz)) {
            return eClassifier;
        }/*ww  w.jav a 2  s. c  om*/
    }
    if (vvEPackage.getEClassifier(clazz.getSimpleName()) == null) {
        try {
            if (clazz.getConstructor(String.class) != null) {
                EDataType dataType = EcoreFactory.eINSTANCE.createEDataType();
                dataType.setName(clazz.getSimpleName());
                dataType.setInstanceClass(clazz);
                vvEPackage.getEClassifiers().add(dataType);
            }
        } catch (SecurityException e) {
            LOGGER.error("Could not convert class to EDataType " + clazz.getName() + ".", e);
        } catch (NoSuchMethodException e) {
            LOGGER.error("Could not convert for class to EDataType " + clazz.getName() + ".", e);
        }
    }
    return vvEPackage.getEClassifier(clazz.getSimpleName());
}

From source file:com.igormaznitsa.upom.UPomModel.java

private static Collection cloneCollection(final Collection collection) throws Exception {
    final Class collectionClass = collection.getClass();
    final Constructor constructor = collectionClass.getConstructor(Collection.class);
    return (Collection) constructor.newInstance(collection);
}