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.github.dozermapper.core.converters.StringConstructorConverter.java

public Object convert(Class destClass, Object srcObj) {
    String result = (String) stringConverter.convert(destClass, srcObj);
    try {/*from   w w w .j  a v a  2 s  .  c  om*/
        Constructor constructor = destClass.getConstructor(String.class); // TODO Check, but not catch
        return constructor.newInstance(result);
    } catch (NoSuchMethodException e) {
        // just return the string
        return result;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}

From source file:com.linkedin.cubert.operator.CubeOperator.java

private static Object instantiateObject(Class<?> cls, JsonNode constructorArgs)
        throws InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException,
        InvocationTargetException, NoSuchMethodException {
    if (constructorArgs == null || constructorArgs.isNull())
        return cls.newInstance();

    Object[] args = new Object[constructorArgs.size()];
    Class<?>[] argClasses = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
        args[i] = JsonUtils.asObject(constructorArgs.get(i));
        argClasses[i] = args[i].getClass();
    }//from   w ww. j  ava2 s  .  co m

    return cls.getConstructor(argClasses).newInstance(args);
}

From source file:net.gplatform.sudoor.server.security.model.filter.CustomizeRequestFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;

    logger.debug("CustomizeRequestFilter Start to Customize");
    logger.debug("Original request -> {}", request);

    ServletRequest newRequestObject = null;
    try {/*from   ww  w .  ja v  a  2 s  .  c  om*/
        Class newRequestClass = Class.forName(requestFullName);
        Constructor newRequestConstructor = newRequestClass.getConstructor(HttpServletRequest.class);
        newRequestObject = (ServletRequest) newRequestConstructor.newInstance(request);
    } catch (Exception e) {
        logger.error("Can not customize request", e);
    }

    if (newRequestObject == null) {
        logger.debug("Execute Chain with original request");
        chain.doFilter(request, response);
    } else {
        logger.debug("Execute Chain with new request -> {}", newRequestObject);
        chain.doFilter(newRequestObject, response);
    }

}

From source file:fitnesse.authentication.PasswordFile.java

public PasswordCipher instantiateCipher(String cipherClassName) {
    try {// w w w .  j ava2  s  . com
        Class<?> cipherClass = Class.forName(cipherClassName);
        Constructor<?> constructor = cipherClass.getConstructor(new Class[] {});
        cipher = (PasswordCipher) constructor.newInstance();
    } catch (InstantiationException e) {
        throw new TodoException(e);
    } catch (IllegalAccessException e) {
        throw new TodoException(e);
    } catch (InvocationTargetException e) {
        throw new TodoException(e);
    } catch (ClassNotFoundException e) {
        throw new TodoException(e);
    } catch (NoSuchMethodException e) {
        throw new TodoException(e);
    }
    return cipher;
}

From source file:com.netflix.paas.config.base.DynamicProxyArchaeusConfigurationFactory.java

@SuppressWarnings({ "unchecked", "static-access" })
public <T> T get(Class<T> configClass, DynamicPropertyFactory propertyFactory,
        AbstractConfiguration configuration) throws Exception {
    final Map<String, Supplier<?>> methods = ConfigurationProxyUtils.getMethodSuppliers(configClass,
            propertyFactory, configuration);

    if (configClass.isInterface()) {
        Class<?> proxyClass = Proxy.getProxyClass(configClass.getClassLoader(), new Class[] { configClass });

        return (T) proxyClass.getConstructor(new Class[] { InvocationHandler.class })
                .newInstance(new Object[] { new InvocationHandler() {
                    @Override// www.  j  a va 2s  . c  o  m
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                        return supplier.get();
                    }
                } });
    } else {
        final Enhancer enhancer = new Enhancer();
        final Object obj = (T) enhancer.create(configClass, new net.sf.cglib.proxy.InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                if (supplier == null) {
                    return method.invoke(proxy, args);
                }
                return supplier.get();
            }
        });

        ConfigurationProxyUtils.assignFieldValues(obj, configClass, propertyFactory, configuration);
        return (T) obj;
    }
}

From source file:com.voa.weixin.mission.MissionManager.java

private void init() {
    Document doc = null;//from  ww w.j a  v a 2  s . c  o  m
    try {
        doc = WeixinUtils.getDocumentResource("weixin.mission.xml");

    } catch (Exception e) {
        logger.warn("can't find or parser weixin.mission.xml:" + e.getMessage());
    }
    if (doc == null)
        return;

    List<Element> missionEs = doc.getRootElement().elements("mission");

    for (Element missionE : missionEs) {
        String name = missionE.elementText("name");
        String period = missionE.elementText("period");
        String delay = missionE.elementText("delay");
        String className = missionE.elementText("class");

        try {
            Class clz = Class.forName(className);
            Constructor<Mission> constructor = clz.getConstructor(String.class);

            Mission mission = constructor.newInstance(name);

            mission.setDelay((Integer.valueOf(delay) * 1000));
            mission.setPeriod((Integer.valueOf(period) * 1000));

            missions.put(name, mission);

            timer.schedule(mission, mission.getDelay(), mission.getPeriod());
        } catch (Exception e) {
            logger.warn("mission can't be instance : " + name);
            logger.warn("cause by " + e.getMessage());
        }
    }

}

From source file:com.otterca.common.crypto.CryptoConfiguration.java

/**
 * Get utility class.//  w ww .  j  av a2  s .  c o  m
 * 
 * @return
 */
@Bean
public synchronized X509CertificateUtil getX509CertificateUtil() {
    if (x509CertUtil == null) {
        String classname = bundle.getString("X509CertificateUtil.classname");
        try {
            LOG.debug("using {} for X509CertificiateUtil class", classname);
            Class<?> c = this.getClass().getClassLoader().loadClass(classname);
            x509CertUtil = (X509CertificateUtil) c.getConstructor(Void.class).newInstance();
        } catch (ClassNotFoundException e) {
            LOG.warn("unable to load class {}", classname, e);
        } catch (NoSuchMethodException e) {
            LOG.warn("unable to load class {}", classname, e);
        } catch (InvocationTargetException e) {
            LOG.warn("unable to load class {}", classname, e);
        } catch (IllegalAccessException e) {
            LOG.warn("unable to load class {}", classname, e);
        } catch (InstantiationException e) {
            LOG.warn("unable to load class {}", classname, e);
        }
    }
    return x509CertUtil;
}

From source file:com.netflix.paas.config.base.JavaAssistArchaeusConfigurationFactory.java

@SuppressWarnings({ "unchecked", "static-access" })
public <T> T get(Class<T> configClass, DynamicPropertyFactory propertyFactory,
        AbstractConfiguration configuration) throws Exception {
    final Map<String, Supplier<?>> methods = ConfigurationProxyUtils.getMethodSuppliers(configClass,
            propertyFactory, configuration);

    if (configClass.isInterface()) {
        Class<?> proxyClass = Proxy.getProxyClass(configClass.getClassLoader(), new Class[] { configClass });

        return (T) proxyClass.getConstructor(new Class[] { InvocationHandler.class })
                .newInstance(new Object[] { new InvocationHandler() {
                    @Override//www  .  j a v  a 2s  . c om
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                        if (supplier == null) {
                            return method.invoke(proxy, args);
                        }
                        return supplier.get();
                    }
                } });
    } else {
        final Enhancer enhancer = new Enhancer();
        final Object obj = (T) enhancer.create(configClass, new net.sf.cglib.proxy.InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                if (supplier == null) {
                    return method.invoke(proxy, args);
                }
                return supplier.get();
            }
        });

        ConfigurationProxyUtils.assignFieldValues(obj, configClass, propertyFactory, configuration);
        return (T) obj;
    }
}

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

@Override
public <T> T getPlugin(final Class<T> clazz) throws Exception {
    Class<? extends Plugin> plugin = pluginsMap.get(clazz);
    Constructor<? extends Plugin> cons = plugin.getConstructor(ProviderSupport.class);
    ProviderSupport support = new ProviderSupport(getOauthStrategy());
    Plugin obj = cons.newInstance(support);
    return (T) obj;
}

From source file:org.malaguna.cmdit.service.CommandRunner.java

/**
 * Dada una clase de un comando, instancia el mismo apropiadamente 
 * /*w  ww.  j ava 2s. co  m*/
 * @param clazz
 * @return
 */
protected Command createCommand(Class<? extends Command> clazz) {
    Command comand = null;

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

    return comand;
}