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:edu.cornell.mannlib.vedit.controller.EditFrontController.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String controllerName = request.getParameter("controller") + "RetryController";
    if (controllerName == null || controllerName.length() == 0) {
        log.error("doPost() found no controller parameter");
    }/*from w w w  . j  a  v a 2s .  c  o m*/
    Class controller = null;
    Object controllerInstance = null;
    try {
        controller = Class.forName(CONTROLLER_PKG + "." + controllerName);
        try {
            controllerInstance = controller.getConstructor((Class[]) null).newInstance((Object[]) null);
            ((HttpServlet) controllerInstance).init(getServletConfig());
        } catch (Exception e) {
            String errMsg = "doPost() could not instantiate specific " + "controller " + controllerName;
            log.error(errMsg, e);
            throw new RuntimeException(errMsg, e);
        }
    } catch (ClassNotFoundException e) {
        String errMsg = "doPost() could not find controller " + CONTROLLER_PKG + "." + controllerName;
        log.error(errMsg);
        throw new RuntimeException(errMsg);
    }
    Class[] args = new Class[2];
    args[0] = HttpServletRequest.class;
    args[1] = HttpServletResponse.class;
    try {
        Method meth = controller.getDeclaredMethod("doGet", args);
        Object[] methArgs = new Object[2];
        methArgs[0] = request;
        methArgs[1] = response;
        try {
            meth.invoke(controllerInstance, methArgs);
        } catch (IllegalAccessException e) {
            String errMsg = "doPost() encountered IllegalAccessException " + "while invoking " + controllerName;
            log.error(errMsg, e);
            throw new RuntimeException(errMsg, e);
        } catch (InvocationTargetException e) {
            String errMsg = "doPost() encountered InvocationTargetException " + "while invoking "
                    + controllerName;
            log.error(errMsg, e);
            throw new RuntimeException(errMsg, e);
        }
    } catch (NoSuchMethodException e) {
        log.error("could not find doPost() method in " + controllerName);
        throw new RuntimeException("could not find doPost() method in " + controllerName);
    }
}

From source file:it.cnr.icar.eric.client.admin.AdminShellFactory.java

/**
* Creates the instance of the pluginClass
*//* ww w .  j a  v  a 2s .com*/
private Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    log.debug("adminShellClass = " + pluginClass);

    Class<?> theClass = Class.forName(pluginClass);

    //try to invoke constructor using Reflection,
    //if this fails then try invoking getInstance()
    try {
        Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
        plugin = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        log.warn(rb.getString(AdminShell.ADMIN_SHELL_RESOURCES_PREFIX + "noAccessibleConstructor"));

        Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
        plugin = factory.invoke(null, new Object[0]);
    }

    return plugin;
}

From source file:com.microrisc.simply.init.SimpleInitObjectsFactory.java

/** 
 * Creates message convertor./*from   w  w  w.ja v  a 2 s. com*/
 * @param protoMapping protocol mapping to use
 * @param configuration input configuration
 * @return message convetor
 * @throws java.lang.Exception if an error has occured during creating of 
 *         message convertor 
 */
protected MessageConvertor createMessageConvertor(ProtocolMapping protoMapping, Configuration configuration)
        throws Exception {
    String msgConvClassName = configuration.getString("protocolLayer.messageConvertor.class");
    Class msgConvClass = Class.forName(msgConvClassName);
    java.lang.reflect.Constructor constructor = msgConvClass.getConstructor(ProtocolMapping.class);
    return (MessageConvertor) constructor.newInstance(protoMapping);
}

From source file:com.freetmp.common.util.ClassUtils.java

public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) {
    Assert.notNull(clazz, "Class must not be null");
    try {//www. ja va 2 s  .  com
        return clazz.getConstructor(paramTypes);
    } catch (NoSuchMethodException ex) {
        return null;
    }
}

From source file:com.flozano.socialauth.AbstractProvider.java

@Override
public final void registerPlugins() throws Exception {
    LOG.info("Loading plugins");
    List<String> pluginsList = getPluginsList();
    if (pluginsList != null && !pluginsList.isEmpty()) {
        for (String s : pluginsList) {
            LOG.info("Loading plugin :: " + s);
            Class<? extends Plugin> clazz = Class.forName(s).asSubclass(Plugin.class);
            // getting constructor only for checking
            Constructor<? extends Plugin> cons = clazz.getConstructor(ProviderSupport.class);
            Class<?> interfaces[] = clazz.getInterfaces();
            for (Class<?> c : interfaces) {
                if (Plugin.class.isAssignableFrom(c)) {
                    pluginsMap.put(c.asSubclass(Plugin.class), clazz);
                }/* www .j a v a2s .co m*/
            }
        }
    }
}

From source file:it.cnr.icar.eric.server.plugin.AbstractPluginManager.java

/**
* Creates the instance of the pluginClass
*//*from w  w  w  .  j  a va2s  .c  om*/
protected Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    if (log.isDebugEnabled()) {
        log.debug("pluginClass = " + pluginClass);
    }

    if (pluginClass != null) {
        Class<?> theClass = Class.forName(pluginClass);

        //try to invoke zero arg constructor using Reflection, 
        //if this fails then try invoking getInstance()
        try {
            Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
            plugin = constructor.newInstance(new Object[0]);
        } catch (Exception e) {
            Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
            plugin = factory.invoke((java.lang.Class[]) null, new Object[0]);
        }
    }
    return plugin;
}

From source file:it.cnr.icar.eric.server.common.AbstractFactory.java

/**
 * Creates the instance of the pluginClass
 *
 * @param pluginClass class name of instance to create
 * @return an instance of the specified class
 * @exception Exception if an error occurs
 *///w w  w .j  av  a 2s  . c  o  m
protected Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    if (log.isDebugEnabled()) {
        log.debug("pluginClass = " + pluginClass);
    }

    Class<?> theClass = Class.forName(pluginClass);

    //try to invoke constructor using Reflection, 
    //if this fails then try invoking getInstance()
    try {
        Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
        plugin = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        log.warn(ServerResourceBundle.getInstance()
                .getString("message.NoAccessibleConstructorInvokingGetInstanceInstead"));

        Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
        plugin = factory.invoke(null, new Object[0]);
    }

    return plugin;
}

From source file:es.urjc.mctwp.bbeans.AbstractBean.java

/**
 * Return new instance of command identified by clazz
 * //from  w ww.j a  v a  2s. c om
 * @param clase
 * @return
 */
protected Command getCommand(Class<?> clazz) {
    Command command = null;

    //Build a new command using Web Application Context (wac)
    try {
        Constructor<?> c = clazz.getConstructor(BeanFactory.class);
        command = (Command) c.newInstance(wac);
    } catch (Exception e) {
        logger.error("Error creating new command [" + clazz.toString() + "] : " + e.getMessage());
    }

    return command;
}

From source file:com.speed.ob.Obfuscator.java

public void execute() {
    LOGGER.info("Starting obfuscation");
    for (Class<? extends ObfuscatorTransform> clazz : transforms) {
        try {//from www .  jav  a 2  s  .  c om
            ObfuscatorTransform transform = clazz.getConstructor(Config.class).newInstance(config);
            LOGGER.info("Processing transform: " + clazz.getCanonicalName());
            if (fHandler != null) {
                transform.addLogHandler(fHandler);
            }
            transform.setLogLevel(level, false);
            LOGGER.info("Running: " + clazz.getCanonicalName());
            transform.run(store, config);
            LOGGER.info("Finished processing: " + clazz.getCanonicalName());
            transform.results();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:it.cnr.icar.eric.server.repository.RepositoryManagerFactory.java

/**
* Creates the instance of the pluginClass
*//*  w w  w.j a  va2 s.c o  m*/
private Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    if (log.isDebugEnabled()) {
        log.debug("pluginClass = " + pluginClass);
    }

    Class<?> theClass = Class.forName(pluginClass);

    //try to invoke constructor using Reflection, 
    //if this fails then try invoking getInstance()
    try {
        Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
        plugin = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        //log.trace(ServerResourceBundle.getInstance().getString("message.NoAccessibleConstructor"));

        Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
        plugin = factory.invoke(null, new Object[0]);
    }

    return plugin;
}