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:jp.sf.fess.solr.plugin.suggest.util.SolrConfigUtil.java

public static List<SuggestFieldInfo> getSuggestFieldInfoList(final SuggestUpdateConfig config) {
    final List<SuggestFieldInfo> list = new ArrayList<SuggestFieldInfo>();

    for (final SuggestUpdateConfig.FieldConfig fieldConfig : config.getFieldConfigList()) {
        try {//  w  ww  .  j  a va  2 s. c  o m
            final List<String> fieldNameList = Arrays.asList(fieldConfig.getTargetFields());
            final SuggestUpdateConfig.TokenizerConfig tokenizerConfig = fieldConfig.getTokenizerConfig();

            //create tokenizerFactory
            TokenizerFactory tokenizerFactory = null;
            if (tokenizerConfig != null) {
                final Class<?> cls = Class.forName(tokenizerConfig.getClassName());
                final Constructor<?> constructor = cls.getConstructor(Map.class);
                tokenizerFactory = (TokenizerFactory) constructor.newInstance(tokenizerConfig.getArgs());
                try {
                    final Class[] params = new Class[] { ResourceLoader.class };
                    final Method inform = cls.getDeclaredMethod("inform", params);
                    final Object[] args = new Object[] { new FilesystemResourceLoader() };
                    inform.invoke(tokenizerFactory, args);
                } catch (final NoSuchMethodException e) {
                    //ignore
                } catch (final Exception e) {
                    logger.warn("Failed to execute inform of tokenizer.", e);
                }
            }

            //create converter
            final SuggestIntegrateConverter suggestIntegrateConverter = new SuggestIntegrateConverter();
            for (final SuggestUpdateConfig.ConverterConfig converterConfig : fieldConfig
                    .getConverterConfigList()) {
                final SuggestReadingConverter suggestReadingConverter = SuggestUtil
                        .createConverter(converterConfig.getClassName(), converterConfig.getProperties());
                suggestIntegrateConverter.addConverter(suggestReadingConverter);
            }
            if (tokenizerFactory != null) {
                suggestIntegrateConverter.setTokenizerFactory(tokenizerFactory);
            }
            suggestIntegrateConverter.start();

            //create normalizer
            final SuggestIntegrateNormalizer suggestIntegrateNormalizer = new SuggestIntegrateNormalizer();
            for (final SuggestUpdateConfig.NormalizerConfig normalizerConfig : fieldConfig
                    .getNormalizerConfigList()) {
                final SuggestNormalizer suggestNormalizer = SuggestUtil
                        .createNormalizer(normalizerConfig.getClassName(), normalizerConfig.getProperties());
                suggestIntegrateNormalizer.addNormalizer(suggestNormalizer);
            }
            suggestIntegrateNormalizer.start();

            final SuggestFieldInfo suggestFieldInfo = new SuggestFieldInfo(fieldNameList, tokenizerFactory,
                    suggestIntegrateConverter, suggestIntegrateNormalizer);
            list.add(suggestFieldInfo);
        } catch (final Exception e) {
            throw new FessSuggestException(
                    "Failed to create Tokenizer." + fieldConfig.getTokenizerConfig().getClassName(), e);
        }
    }
    return list;
}

From source file:it.unimi.di.big.mg4j.document.PropertyBasedDocumentFactory.java

public static PropertyBasedDocumentFactory getInstance(final Class<?> klass,
        final Reference2ObjectMap<Enum<?>, Object> metadata) throws InstantiationException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    return (PropertyBasedDocumentFactory) (klass.getConstructor(new Class[] { Reference2ObjectMap.class })
            .newInstance(new Object[] { metadata }));
}

From source file:com.dubsar_dictionary.Dubsar.FAQActivity.java

@SuppressWarnings("all")
private static boolean setProxyICS(WebView webview, String host, int port) {
    try {//from   w w  w  .  j a va 2 s. co  m
        Log.d(LOG_TAG, "Setting proxy with 4.0 API.");

        Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
        Class params[] = new Class[1];
        params[0] = Class.forName("android.net.ProxyProperties");
        Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

        Class wv = Class.forName("android.webkit.WebView");
        Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
        Object mWebViewCoreFieldInstance = getFieldValueSafely(mWebViewCoreField, webview);

        Class wvc = Class.forName("android.webkit.WebViewCore");
        Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
        Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldInstance);

        Class bf = Class.forName("android.webkit.BrowserFrame");
        Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
        Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);

        Class ppclass = Class.forName("android.net.ProxyProperties");
        Class pparams[] = new Class[3];
        pparams[0] = String.class;
        pparams[1] = int.class;
        pparams[2] = String.class;
        Constructor ppcont = ppclass.getConstructor(pparams);

        updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));

        Log.d(LOG_TAG, "Setting proxy with 4.0 API successful!");
        return true;
    } catch (Exception ex) {
        Log.e(LOG_TAG, "failed to set HTTP proxy: " + ex);
        return false;
    }
}

From source file:com.dubsar_dictionary.Dubsar.FAQActivity.java

/**
 * Set Proxy for Android 4.1 and above./*from ww w .java2s . c o m*/
 */
@SuppressWarnings("all")
private static boolean setProxyJBPlus(WebView webview, String host, int port) {
    Log.d(LOG_TAG, "Setting proxy with >= 4.1 API.");

    try {
        Class wvcClass = Class.forName("android.webkit.WebViewClassic");
        Class wvParams[] = new Class[1];
        wvParams[0] = Class.forName("android.webkit.WebView");
        Method fromWebView = wvcClass.getDeclaredMethod("fromWebView", wvParams);
        Object webViewClassic = fromWebView.invoke(null, webview);

        Class wv = Class.forName("android.webkit.WebViewClassic");
        Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
        Object mWebViewCoreFieldInstance = getFieldValueSafely(mWebViewCoreField, webViewClassic);

        Class wvc = Class.forName("android.webkit.WebViewCore");
        Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
        Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldInstance);

        Class bf = Class.forName("android.webkit.BrowserFrame");
        Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
        Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);

        Class ppclass = Class.forName("android.net.ProxyProperties");
        Class pparams[] = new Class[3];
        pparams[0] = String.class;
        pparams[1] = int.class;
        pparams[2] = String.class;
        Constructor ppcont = ppclass.getConstructor(pparams);

        Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
        Class params[] = new Class[1];
        params[0] = Class.forName("android.net.ProxyProperties");
        Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

        updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));
    } catch (Exception ex) {
        Log.e(LOG_TAG, "Setting proxy with >= 4.1 API failed with error: " + ex.getMessage());
        return false;
    }

    Log.d(LOG_TAG, "Setting proxy with >= 4.1 API successful!");
    return true;
}

From source file:net.sf.zekr.common.util.CollectionUtils.java

@SuppressWarnings("unchecked")
public static List fromString(String strList, String delim, Class clazz) throws InstantiationException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (clazz == null)
        return Arrays.asList(strList.split(delim));

    List list = new ArrayList();
    if (!StringUtils.isBlank(strList)) {
        String[] strs = strList.split(delim);
        for (int i = 0; i < strs.length; i++) {
            list.add(clazz.getConstructor(new Class[] { String.class })
                    .newInstance(new Object[] { strs[i].trim() }));
        }//from ww w.  j  av a2s  .com
    }
    return list;
}

From source file:org.bremersee.common.spring.autoconfigure.ObjectMapperAutoConfiguration.java

private static AnnotationIntrospector findAnnotationIntrospectorPair() {
    try {//from  w ww .j  a  v  a  2  s  . c  o  m
        LOG.info("Trying to add a pair of annotation introspectors ('"
                + JacksonAnnotationIntrospector.class.getSimpleName() + " + "
                + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ").");
        @SuppressWarnings("unchecked")
        Class<? extends AnnotationIntrospector> cls = (Class<? extends AnnotationIntrospector>) Class
                .forName(JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME);
        Constructor<? extends AnnotationIntrospector> constructor = cls.getConstructor(TypeFactory.class);
        AnnotationIntrospector secondary = constructor.newInstance(TypeFactory.defaultInstance());

        // see http://wiki.fasterxml.com/JacksonJAXBAnnotations
        AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
        AnnotationIntrospectorPair pair = new AnnotationIntrospectorPair(primary, secondary);
        LOG.info("The pair of annotation introspectors ('" // NOSONAR
                + JacksonAnnotationIntrospector.class.getSimpleName() + " + "
                + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ") was successfully added.");
        return pair;

    } catch (ClassNotFoundException e) { // NOSONAR
        LOG.warn("The pair of annotation introspectors ('" + JacksonAnnotationIntrospector.class.getSimpleName()
                + " " + "+ " + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ") wasn't added: "
                + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + " was not found.");
    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        LOG.warn("The pair of annotation introspectors ('" + JacksonAnnotationIntrospector.class.getSimpleName()
                + " " + "+ " + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ") wasn't added.", e);
    }
    return null;

}

From source file:de.tbuchloh.kiskis.gui.AbstractAccountDetailView.java

public static AbstractAccountDetailView create(final ModelNode node) {
    final Class<?> c = NODE_MAP.get(node.getClass());
    try {/*from ww w. j a  v a  2s.  co  m*/
        final Constructor<?> builder = c.getConstructor(new Class[] { ModelNode.class });
        return (AbstractAccountDetailView) builder.newInstance(new Object[] { node });
    } catch (final Exception e) {
        throw new Error("should never happen!", e); //$NON-NLS-1$
    }
}

From source file:ezbake.thrift.ThriftUtils.java

@SuppressWarnings("unchecked")
public static <Y extends TServiceClient> Y getClient(Class<Y> clazz, HostAndPort hostAndPort, String securityId,
        Properties properties, TTransportFactory transportFactory)
        throws NoSuchMethodException, TException, Exception {
    final Constructor<?> constructor = clazz.getConstructor(TProtocol.class);
    final Object ds = constructor
            .newInstance(getProtocol(hostAndPort, securityId, properties, transportFactory));
    return (Y) ds;
}

From source file:at.jku.rdfstats.hist.builder.HistogramBuilderFactory.java

private static HistogramBuilder<?> newInstance(Class<? extends HistogramBuilder<?>> clazz, String typeUri,
        int preferredSize, RDFStatsConfiguration conf) throws HistogramBuilderException {
    if (typeUri == null)
        throw new HistogramBuilderException("Type URI cannot be null.");

    try {//ww  w.j  ava2 s .c o m
        Constructor<? extends HistogramBuilder<?>> constr = clazz
                .getConstructor(new Class[] { RDFStatsConfiguration.class, String.class, int.class });
        HistogramBuilder<?> builder = constr.newInstance(new Object[] { conf, typeUri, preferredSize });
        return builder;
    } catch (NoSuchMethodException e) {
        throw new HistogramBuilderException("Couldn't create histogram builder (datatype URI: " + typeUri
                + "), implementation '" + clazz.getCanonicalName() + "' is invalid.", e);
    } catch (Exception e) {
        throw new HistogramBuilderException("Couldn't create histogram builder (datatype URI: " + typeUri
                + "), please call HistogramBuilderFactory.supports() first to check if a type is supported.",
                e);
    }
}

From source file:io.cloudslang.lang.entities.bindings.values.PyObjectValueProxyFactory.java

@SuppressWarnings("unchecked")
private static Object getParamDefaultValue(PyObject pyObject, Class<?> parameterType) throws Exception {
    if (parameterType.equals(PyType.class)) {
        return pyObject.getType();
    } else if (parameterType.isPrimitive()) {
        return ClassUtils.primitiveToWrapper(parameterType).getConstructor(String.class).newInstance("0");
    } else if (Number.class.isAssignableFrom(parameterType) || String.class.isAssignableFrom(parameterType)) {
        return parameterType.getConstructor(String.class).newInstance("0");
    } else {/*from  ww  w  . ja  v  a  2  s  .c  o  m*/
        return null;
    }
}