Example usage for java.lang Class newInstance

List of usage examples for java.lang Class newInstance

Introduction

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

Prototype

@CallerSensitive
@Deprecated(since = "9")
public T newInstance() throws InstantiationException, IllegalAccessException 

Source Link

Document

Creates a new instance of the class represented by this Class object.

Usage

From source file:com.iflytek.spider.crawl.SignatureFactory.java

/** Return the default Signature implementation. */
public static Signature getSignature(Configuration conf) {
    String clazz = conf.get("db.signature.class", MD5Signature.class.getName());
    ObjectCache objectCache = ObjectCache.get(conf);
    Signature impl = (Signature) objectCache.getObject(clazz);
    if (impl == null) {
        try {/*w  w w. j  a  va2 s . com*/
            if (LOG.isInfoEnabled()) {
                LOG.info("Using Signature impl: " + clazz);
            }
            Class<?> implClass = Class.forName(clazz);
            impl = (Signature) implClass.newInstance();
            impl.setConf(conf);
            objectCache.setObject(clazz, impl);
        } catch (Exception e) {
            throw new RuntimeException("Couldn't create " + clazz, e);
        }
    }
    return impl;
}

From source file:Main.java

protected static boolean initProvider(String providerName, String className) {
    try {/*from  w  w w  . ja v  a  2  s  .com*/
        Provider provider = Security.getProvider(providerName);
        if (provider == null) {
            @SuppressWarnings("rawtypes")
            Class clazz = Class.forName(className);
            provider = (Provider) clazz.newInstance();
            Security.addProvider(provider);
        }
        return true;
    } catch (Throwable ignored) {
    }
    return false;
}

From source file:Main.java

/**
 * @throws NoSuchMechanismException/*  w  w w.  j a v a2  s . c om*/
 */
static XMLSignatureFactory getDOMInstance() {

    try {
        return XMLSignatureFactory.getInstance("DOM");
    } catch (NoSuchMechanismException nsme) {
        Provider provider;

        try {
            Class<?> clazz = Class.forName("org.jcp.xml.dsig.internal.dom.XMLDSigRI");

            provider = (Provider) clazz.newInstance();
        } catch (Exception e) {
            throw new NoSuchMechanismException(e);
        }

        return XMLSignatureFactory.getInstance("DOM", provider);
    }
}

From source file:ClassUtil.java

/**
 * Create new instance of specified class and type
 *
 * @param clazz of instance//w w  w  . j  av a2 s . co  m
 * @param <T> type of object
 * @return new Class instance
 */
public static <T> T getInstance(Class<T> clazz) {
    T t = null;
    try {
        t = clazz.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return t;
}

From source file:com.jquery.web.Request.java

public static <T> T map(Class<T> clazz) {
    try {/*from w  w w .j  av  a  2s .  co  m*/
        return map(clazz.newInstance());
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ms.commons.standalone.job.JobRunner.java

private static AbstractJob getJobInstanceByClassName(String fullClassName) {
    try {/*from w w  w  .ja v a2  s . c o  m*/
        Class<?> clazz = Class.forName(fullClassName);
        Object obj = clazz.newInstance();
        if (obj instanceof AbstractJob) {
            return (AbstractJob) obj;
        } else {
            String message = String.format("%s is not instanceof com.ms.commons.standalone.job.AbstractJob",
                    fullClassName);
            throw new RuntimeException(message);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("can not find class name for: " + fullClassName, e);
    } catch (InstantiationException e) {
        throw new RuntimeException("can not newInstance() class name for: " + fullClassName, e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("IllegalAccessException class name for: " + fullClassName, e);
    }
}

From source file:Main.java

/**
 * Returns an instance of the given class name, by calling the default
 * constructor.//  w ww  .jav  a  2s  .co  m
 *
 * @param className The class name to load and to instantiate.
 * @param returnNull If <code>true</code>, if the class is not found it
 * returns <code>true</code>, otherwise it throws a
 * <code>TilesException</code>.
 * @return The new instance of the class name.
 * @throws CannotInstantiateObjectException If something goes wrong during instantiation.
 * @since 2.0.7
 */
public static Object instantiate(String className, boolean returnNull) {
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    if (original == null) {
        Thread.currentThread().setContextClassLoader(Main.class.getClassLoader());
    }
    try {
        Class<?> namedClass = Class.forName(className);
        return namedClass.newInstance();
    } catch (ClassNotFoundException e) {
        if (returnNull) {
            return null;
        }
        throw new RuntimeException("Unable to resolve factory class: '" + className + "'", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Unable to access factory class: '" + className + "'", e);
    } catch (InstantiationException e) {
        throw new RuntimeException("Unable to instantiate factory class: '" + className
                + "'. Make sure that this class has a default constructor", e);
    } finally {
        // If the original context classloader of the current thread was
        // null, it must be reset.
        if (original == null) {
            Thread.currentThread().setContextClassLoader(null);
        }
    }
}

From source file:Main.java

public static <T> void fill(T[] list, int start, int end, Class<T> clazz) {
    for (int i = start; i <= end; i++) {
        try {//w  w w.j  a v  a 2s  .c om
            list[i] = clazz != null ? clazz.newInstance() : null;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    try {/*  w w  w  .ja  v a 2s  .  c  o m*/
        @SuppressWarnings("rawtypes")
        Class clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        Field field = clazz.getField("status_bar_height");

        int id = Integer.parseInt(field.get(object).toString());
        return context.getResources().getDimensionPixelSize(id);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:ee.ria.xroad.confproxy.commandline.ConfProxyUtilMain.java

/**
 * Creates an utility program instance of the provided class name.
 * @param className name of the utility program class
 * @return an instance of the requested utility program
 * @throws Exception if class could not be found or an instance could
 * not be created//from   w w w. j  ava2  s  .  c o  m
 */
@SuppressWarnings("unchecked")
static ConfProxyUtil createUtilInstance(final String className) throws Exception {
    Class<ConfProxyUtil> utilClass = (Class<ConfProxyUtil>) Class.forName(className);
    return utilClass.newInstance();
}