Example usage for java.lang Class getDeclaredConstructor

List of usage examples for java.lang Class getDeclaredConstructor

Introduction

In this page you can find the example usage for java.lang Class getDeclaredConstructor.

Prototype

@CallerSensitive
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

Usage

From source file:com.glaf.core.util.ReflectUtils.java

@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> theClass, Configuration conf) {
    T result;/* w  ww . j a  va 2  s.c  om*/
    try {
        Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass);
        if (meth == null) {
            meth = theClass.getDeclaredConstructor(EMPTY_ARRAY);
            meth.setAccessible(true);
            CONSTRUCTOR_CACHE.put(theClass, meth);
        }
        result = meth.newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:org.intermine.bio.dataconversion.ChadoDBConverter.java

/**
 * Process the data from the Database and write to the ItemWriter.
 * {@inheritDoc}/*from   w  w  w . ja va2 s .c om*/
 */
@Override
public void process() throws Exception {

    if (StringUtils.isEmpty(processors)) {
        throw new IllegalArgumentException("processors not set in ChadoDBConverter");
    }

    Map<OrganismData, Integer> tempChadoOrgMap = getChadoOrganismIds(getConnection());

    for (OrganismData od : organismsToProcess) {
        Integer chadoId = tempChadoOrgMap.get(od);
        if (chadoId == null) {
            throw new RuntimeException("Organism " + od + " not found in the chado organism table");
        }
        chadoToOrgData.put(chadoId, od);
    }

    if (chadoToOrgData.size() == 0) {
        throw new RuntimeException("can't find any known organisms in the organism table");
    }

    String[] bits = processors.trim().split("[ \\t]+");
    for (int i = 0; i < bits.length; i++) {
        String className = bits[i];
        if (!StringUtils.isEmpty(className)) {
            Class<?> cls = Class.forName(className);
            Constructor<?> constructor = cls.getDeclaredConstructor(ChadoDBConverter.class);
            ChadoProcessor currentProcessor = (ChadoProcessor) constructor.newInstance(this);
            currentProcessor.process(getConnection());
            getCompletedProcessors().add(currentProcessor);
        }
    }
}

From source file:org.apache.pdfbox.pdmodel.encryption.SecurityHandlersManager.java

/**
 * Get the security handler for the protection policy.
 *
 * @param policy The policy to get the security handler for.
 *
 * @return The appropriate security handler.
 *
 * @throws BadSecurityHandlerException If it is unable to create a SecurityHandler.
 *///ww w . j a  v  a  2  s . co  m
public SecurityHandler getSecurityHandler(ProtectionPolicy policy) throws BadSecurityHandlerException {

    Object found = handlerPolicyClasses.get(policy.getClass());
    if (found == null) {
        throw new BadSecurityHandlerException(
                "Cannot find an appropriate security handler for " + policy.getClass().getName());
    }
    Class handlerclass = (Class) found;
    Class[] argsClasses = { policy.getClass() };
    Object[] args = { policy };
    try {
        Constructor c = handlerclass.getDeclaredConstructor(argsClasses);
        SecurityHandler handler = (SecurityHandler) c.newInstance(args);
        return handler;
    } catch (Exception e) {
        LOG.error(e, e);
        throw new BadSecurityHandlerException("problem while trying to instanciate the security handler "
                + handlerclass.getName() + ": " + e.getMessage());
    }
}

From source file:com.soomla.util.JSONFactory.java

public T create(JSONObject jsonObject, String packageName) {

    if (jsonObject == null) {
        // warn//  w ww  .j  a v  a2 s.  c  o m
        return null;
    }

    T t = null;
    try {
        //            SoomlaUtils.LogDebug(TAG, jsonObject.toString());
        String className = jsonObject.getString(com.soomla.data.JSONConsts.SOOM_CLASSNAME);
        Class<? extends T> clazz = (Class<? extends T>) Class.forName(packageName + "." + className);
        SoomlaUtils.LogDebug(TAG, "creating with: " + packageName + "." + className);
        if (clazz != null) {
            final Constructor<? extends T> jsonCtor = clazz.getDeclaredConstructor(JSONObject.class);
            t = jsonCtor.newInstance(jsonObject);
        } else {
            SoomlaUtils.LogError(TAG, "unknown class name:" + className);
        }

    } catch (JSONException e) {
        SoomlaUtils.LogError(TAG, "fromJSONObject JSONException:" + e.getMessage());
    } catch (InstantiationException e) {
        SoomlaUtils.LogError(TAG, "fromJSONObject InstantiationException:" + e.getMessage());
    } catch (IllegalAccessException e) {
        SoomlaUtils.LogError(TAG, "fromJSONObject IllegalAccessException:" + e.getMessage());
    } catch (NoSuchMethodException e) {
        SoomlaUtils.LogError(TAG, "fromJSONObject no JSONObject constructor found:" + e.getMessage());
    } catch (InvocationTargetException e) {
        SoomlaUtils.LogError(TAG, "fromJSONObject InvocationTargetException:" + e.getMessage());
        SoomlaUtils.LogError(TAG, "fromJSONObject InvocationTargetException[cause]:" + e.getCause());
    } catch (ClassNotFoundException e) {
        SoomlaUtils.LogError(TAG, "fromJSONObject ClassNotFoundException:" + e.getMessage());
    }

    return t;
}

From source file:eu.stratosphere.nephele.ipc.Server.java

public static <T> T newInstance(Class<T> theClass) {
    T result;// w w  w  .j av  a 2  s. co  m
    Constructor<T> meth = null;
    try {
        meth = theClass.getDeclaredConstructor(EMPTY_ARRAY);
        meth.setAccessible(true);
        result = meth.newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:com.koda.integ.hbase.test.BlockCacheSimpleTest.java

/**
 * Test create cache.//from   w  w  w  . j  ava2 s  .com
 */
public void testCreateCache() {
    LOG.info("Test create cache started");

    try {
        Class<?> cls = Class.forName("com.koda.integ.hbase.blockcache.OffHeapBlockCache");

        Constructor<?> ctr = cls.getDeclaredConstructor(Configuration.class);
        cache = (BlockCache) ctr.newInstance(conf);
        assertTrue(true);
        LOG.info("Test create cache finished.");
    } catch (Exception e) {
        LOG.error(
                "Could not instantiate 'com.koda.integ.hbase.blockcache.OffHeapBlockCache'+ class, will resort to standard cache impl.");
        assertTrue(false);
    }

}

From source file:ee.ria.xroad.common.conf.globalconf.GlobalConfProviderFactory.java

GlobalConfProviderFactory() throws Exception {
    try {//from  www.j  a v  a  2 s .c o m
        String providerClassName = System.getProperty(GLOBALCONF_PROVIDER_CLASS);
        if (!StringUtils.isEmpty(providerClassName)) {
            Class<?> providerClass = Class.forName(providerClassName);
            if (!GlobalConfProvider.class.isAssignableFrom(providerClass)) {
                throw new Exception(providerClass + " does not implement " + GlobalConfProvider.class);
            }

            instanceConstructor = providerClass.getDeclaredConstructor(boolean.class);
            log.info("Using {} as GlobalConfProvider", providerClass);
        }
    } catch (Exception e) {
        log.error("Could not create an instance constructor" + " for GlobalConfProvider", e);
    } finally {
        if (instanceConstructor == null) {
            instanceConstructor = GlobalConfImpl.class.getDeclaredConstructor(boolean.class);
        }
    }
}

From source file:org.wso2.msf4j.spring.MSF4JSpringApplication.java

private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
        ClassLoader classLoader, Object[] args, Set<String> names) {
    List<T> instances = new ArrayList<T>(names.size());
    for (String name : names) {
        try {/*  w  w  w  .  j  a v  a  2  s  .  co  m*/
            Class<?> instanceClass = ClassUtils.forName(name, classLoader);
            Assert.isAssignable(type, instanceClass);
            Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
            T instance = (T) BeanUtils.instantiateClass(constructor, args);
            instances.add(instance);
        } catch (Throwable ex) {
            throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
        }
    }
    return instances;
}

From source file:org.grouplens.grapht.util.ConstructorProxy.java

/**
 * Resolve this proxy into a {@link java.lang.reflect.Constructor} instance.
 * @return The {@link java.lang.reflect.Constructor} represented by this proxy.
 * @throws ClassNotFoundException If the proxy's declaring type cannot be resolved.
 * @throws NoSuchMethodException If the constructor does not exist on the declaring type.
 *///www. jav a  2  s  .  c  o  m
public Constructor resolve() throws ClassNotFoundException, NoSuchMethodException {
    Constructor ctor = constructor;
    if (ctor == null) {
        Class<?> cls = declaringClass.resolve();
        Class<?>[] ptypes = new Class<?>[parameterTypes.length];
        for (int i = ptypes.length - 1; i >= 0; i--) {
            ptypes[i] = parameterTypes[i].resolve();
        }
        constructor = ctor = cls.getDeclaredConstructor(ptypes);
    }
    return ctor;
}

From source file:org.alfresco.mobile.android.api.session.impl.CloudSessionImpl.java

/**
 * Create the Alfresco AuthenticationProvider. Used by the default
 * "CMIS enable" PassThruAuthenticationProvider.
 * //w  w w  .ja  v  a2  s  . c  om
 * @param className
 * @return
 */
private AuthenticationProvider createAuthenticationProvider(String className) {
    AuthenticationProvider s = null;
    try {
        Class<?> c = Class.forName(className);
        Constructor<?> t = c.getDeclaredConstructor(Map.class);
        s = (AuthenticationProvider) t.newInstance(userParameters);
    } catch (Exception e) {
        throw new AlfrescoSessionException(ErrorCodeRegistry.SESSION_AUTHENTICATOR, e);
    }
    return s;
}