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:com.alibaba.druid.support.logging.LogFactory.java

@SuppressWarnings("unchecked")
private static void tryImplementation(String testClassName, String implClassName) {
    if (logConstructor != null) {
        return;/*  w w w .j  a  v  a2  s.c om*/
    }

    try {
        Resources.classForName(testClassName);
        Class implClass = Resources.classForName(implClassName);
        logConstructor = implClass.getConstructor(new Class[] { String.class });

        Class<?> declareClass = logConstructor.getDeclaringClass();
        if (!Log.class.isAssignableFrom(declareClass)) {
            logConstructor = null;
        }

        try {
            if (null != logConstructor) {
                logConstructor.newInstance(LogFactory.class.getName());
            }
        } catch (Throwable t) {
            logConstructor = null;
        }

    } catch (Throwable t) {
        // skip
    }
}

From source file:ireport_5_6_0.view.SaveContributorUtils.java

public static List<JRSaveContributor> createBuiltinContributors(JasperReportsContext context, Locale locale,
        ResourceBundle resourceBundle) {
    ArrayList<JRSaveContributor> contributors = new ArrayList<JRSaveContributor>(DEFAULT_CONTRIBUTORS.length);
    for (String contributorClassName : DEFAULT_CONTRIBUTORS) {
        try {//ww w  . j ava2s .c o m
            Class<?> saveContribClass = JRClassLoader.loadClassForName(contributorClassName);
            Constructor<?> constructor = saveContribClass.getConstructor(CONSTRUCTOR_SIGNATURE);
            JRSaveContributor saveContrib = (JRSaveContributor) constructor.newInstance(context, locale,
                    resourceBundle);
            contributors.add(saveContrib);
        } catch (Exception e) {
            // shouldn't happen, but log anyway
            log.warn("Error creating save contributor of type " + contributorClassName, e);
        }
    }
    return contributors;
}

From source file:TestSample.java

public static void run(Class which, String[] args) {
    TestSuite suite = null;/*from   w w w .ja v a  2 s.  c o  m*/
    if (args.length != 0) {
        try {
            java.lang.reflect.Constructor ctor;
            ctor = which.getConstructor(new Class[] { String.class });
            suite = new TestSuite();
            for (int i = 0; i < args.length; i++) {
                suite.addTest((TestCase) ctor.newInstance(new Object[] { args[i] }));
            }
        } catch (Exception e) {
            System.err.println("Unable to instantiate " + which.getName() + ": " + e.getMessage());
            System.exit(1);
        }

    } else {
        try {
            Method suite_method = which.getMethod("suite", new Class[0]);
            suite = (TestSuite) suite_method.invoke(null, null);
        } catch (Exception e) {
            suite = new TestSuite(which);
        }
    }
    junit.textui.TestRunner.run(suite);
}

From source file:Main.java

public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>... types) {
    if ((targetClass == null) || (types == null)) {
        return null;
    }/*from  w w w . j  a v  a  2  s.  c  o m*/

    try {
        return targetClass.getConstructor(types);
    } catch (Exception e) {
        if (DEBUG) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:com.alibaba.druid.support.logging.LogFactory.java

@SuppressWarnings("unchecked")
public static synchronized void selectLog4JLogging() {
    try {/*w  ww  .j a va2s.co  m*/
        Resources.classForName("org.apache.log4j.Logger");
        Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Log4jImpl");
        logConstructor = implClass.getConstructor(new Class[] { String.class });
    } catch (Throwable t) {
        //ignore
    }
}

From source file:it.greenvulcano.gvesb.http.ProtocolFactory.java

/**
 *
 * @param value// w w w .jav  a2s .  c  o  m
 * @param cls
 * @return
 */
private static Object cast(String value, Class<?> cls) throws Exception {
    Class<?>[] types = { String.class };
    Constructor<?> constr = cls.getConstructor(types);
    Object[] params = { value };
    return constr.newInstance(params);
}

From source file:dk.dma.navnet.messages.TransportMessage.java

public static TransportMessage parseMessage(String msg) throws IOException {
    TextMessageReader pr = new TextMessageReader(msg);
    int type = pr.takeInt();
    try {/*from  w  w w.  ja  v  a2  s .  c  o m*/
        Class<? extends TransportMessage> cl = MessageType.getType(type);
        TransportMessage message = cl.getConstructor(TextMessageReader.class).newInstance(pr);
        message.rawMessage = msg;// for debugging purposes
        return message;
    } catch (ReflectiveOperationException e) {
        throw new IOException(e);
    }
}

From source file:com.alibaba.druid.support.logging.LogFactory.java

@SuppressWarnings("unchecked")
public static synchronized void selectJavaLogging() {
    try {//from   w ww. j av  a  2s . co m
        Resources.classForName("java.util.logging.Logger");
        Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Jdk14LoggingImpl");
        logConstructor = implClass.getConstructor(new Class[] { String.class });
    } catch (Throwable t) {
        //ignore
    }
}

From source file:com.doculibre.constellio.utils.connector.ConnectorPropertyInheritanceResolver.java

@SuppressWarnings("unchecked")
public static <T extends Object> T newInheritedClassPropertyInstance(ConnectorInstance connectorInstance,
        String propertyName, Class[] paramTypes, Object[] args) {
    T inheritedClassPropertyInstance;/*  ww  w . j a  v  a2s . c o m*/
    String propertyValue = getStringPropertyValue(connectorInstance, propertyName);
    if (StringUtils.isEmpty(propertyValue)) {
        propertyValue = getStringPropertyValue(connectorInstance.getConnectorType(), propertyName);
    }
    if (StringUtils.isNotEmpty(propertyValue)) {
        PluginAwareClassLoader pluginAwareClassLoader = new PluginAwareClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(pluginAwareClassLoader);
            Class<T> propertyValueClass = (Class<T>) Class.forName(propertyValue);
            Constructor<T> constructor = propertyValueClass.getConstructor(paramTypes);
            inheritedClassPropertyInstance = constructor.newInstance(args);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } finally {
            Thread.currentThread().setContextClassLoader(pluginAwareClassLoader.getDefaultClassLoader());
        }
    } else {
        inheritedClassPropertyInstance = null;
    }
    return inheritedClassPropertyInstance;
}

From source file:com.bluecloud.ioc.classloader.ClassHandler.java

/**
 * <h3></h3> ?Class Instance
 * //from   w  w w  .j a v a2s  . com
 * @param className
 *            ??
 * @param parameterTypes
 *            ??
 * @param initargs
 *            ??
 * @return classNameClassObject
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws SecurityException
 * @throws IllegalArgumentException
 */
public static Object createObject(String className, Class<?>[] parameterTypes, Object[] initargs)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException,
        SecurityException, InvocationTargetException, NoSuchMethodException {
    Class<?> clazz = (Class<?>) getClass(className, false);
    return clazz.getConstructor(parameterTypes).newInstance(initargs);
}