Example usage for java.lang.reflect Constructor newInstance

List of usage examples for java.lang.reflect Constructor newInstance

Introduction

In this page you can find the example usage for java.lang.reflect Constructor newInstance.

Prototype

@CallerSensitive
@ForceInline 
public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException 

Source Link

Document

Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.

Usage

From source file:ch.cyberduck.core.DeserializerFactory.java

public Deserializer create(final T dict) {
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }/*from  ww w .  jav  a  2 s.  c o m*/
    try {
        final Class<Deserializer> name = (Class<Deserializer>) Class.forName(clazz);
        final Constructor<Deserializer> constructor = ConstructorUtils.getMatchingAccessibleConstructor(name,
                dict.getClass());
        return constructor.newInstance(dict);
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException
            | ClassNotFoundException e) {
        throw new FactoryException(e.getMessage(), e);
    }
}

From source file:com.eyeq.pivot4j.ui.condition.DefaultConditionFactory.java

/**
 * @see com.eyeq.pivot4j.ui.condition.ConditionFactory#createCondition(java.lang.String)
 *///from ww  w  . java  2  s  .  c  om
@Override
public Condition createCondition(String name) {
    if (name == null) {
        throw new NullArgumentException("name");
    }

    Condition condition = null;

    Class<? extends Condition> type = types.get(name);

    if (type != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Instantiating a new condition : " + type);
        }

        try {
            Constructor<? extends Condition> constructor = type.getConstructor(ConditionFactory.class);
            condition = constructor.newInstance(this);
        } catch (Exception e) {
            throw new PivotException(e);
        }
    }

    if (condition == null && logger.isWarnEnabled()) {
        logger.warn("Unknown condition name : " + name);
    }

    return condition;
}

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  ww  .  ja v a  2s.com
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:org.dawnsci.persistence.json.function.FunctionBean.java

/**
 * Method that converts a function bean to an IFunction using reflection
 * //w  ww  .j  a  v  a 2  s . com
 * @return IFunction
 * @throws ClassNotFoundException
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
@JsonIgnore
public IFunction getIFunction() throws ClassNotFoundException, NoSuchMethodException, SecurityException,
        InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    IFunction function = null;
    IParameter[] params = getParameters();
    Class<?> clazz = Class.forName(getType());
    // If a Jexl expression
    if (clazz.equals(JexlExpressionFunction.class)) {
        Constructor<?> constructor = clazz.getConstructor(String.class);
        function = (IFunction) constructor.newInstance((String) getName());
        for (int i = 0; i < params.length; i++) {
            ((JexlExpressionFunction) function).setParameter(i, params[i]);
        }
    } else { // For all other cases try to return an instance of IFunction with parameters
        Constructor<?> constructor = clazz.getConstructor(IParameter[].class);
        function = (IFunction) constructor.newInstance((Object) params);
    }
    return function;
}

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

/**
* Creates the instance of the pluginClass
*//*from w  w w .jav a  2s. c om*/
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;
}

From source file:com.aerospike.config.AppConfiguration.java

public AppConfiguration() throws CloudFoundryEnvironmentException {
    CloudFoundryEnvironment environment = new CloudFoundryEnvironment(System::getenv);

    if (environment.getServiceNames().size() > 0) {
        for (String serviceName : environment.getServiceNames()) {
            System.out.println("SERVICE NAME: " + serviceName);
            CloudFoundryService service = environment.getService(serviceName);
            if (labelToConfigurationClass.containsKey(service.getLabel())) {
                String className = labelToConfigurationClass.get(service.getLabel());
                try {
                    Class<?> clazz = Class.forName(className);
                    Constructor<?> constructor = clazz.getConstructor(Map.class);
                    IServiceConfiguration config = (IServiceConfiguration) constructor
                            .newInstance(environment.getService(serviceName).getCredentials());
                    services.put(serviceName, config);
                    System.out.println("ADDED CLASS : " + className);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Error creating class " + className);
                }//from   w  ww  .  j a  va  2s  . c o m
            }
        }
    }
}

From source file:it.cnr.icar.eric.common.spi.LifeCycleManagerFactory.java

/**
* Creates the instance of the pluginClass
*//*from  w ww  .j av  a2 s.  co  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.warn("No accessible constructor. " +
        //    "invoking getInstance() instead.");

        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.common.spi.QueryManagerFactory.java

/**
* Creates the instance of the pluginClass
*///from  w  ww. j  av a  2 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.warn("No accessible constructor. " +
        //    "invoking getInstance() instead.");

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

    return plugin;
}

From source file:it.cnr.icar.eric.server.event.EventManagerFactory.java

/**
* Creates the instance of the pluginClass
*///  w ww  .j a  v a  2  s  . c om
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.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:com.meidusa.amoeba.net.packet.AbstractPacket.java

private T constractorBuffer(int bufferSize) {
    T buffer = null;//from   w  ww  .  ja  v  a 2  s  . c  o  m
    try {
        Constructor<T> constractor = getPacketBufferClass().getConstructor(int.class);
        buffer = constractor.newInstance(bufferSize);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return buffer;
}