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.google.code.jcaptcha4struts2.core.beans.JC4S2Config.java

/**
 * Returns the default {@link ImageCaptchaService} instance.
 * <p>//from  ww w  .j  a va 2 s. co  m
 * The default image captcha service is defined as a {@code PluginConstant}.
 * 
 * @return default {@link ImageCaptchaService} instance
 */
private static ImageCaptchaService getDefaultImageCaptchaService() {

    try {
        Class<?> clazz = Class.forName(DEFAULT_IMG_CAPTCHA_IMPL);
        return (ImageCaptchaService) clazz.newInstance();
    } catch (ClassNotFoundException e) {
        // JCaptcha Implementation Class Missing !
        String msg = "Unable to find class for  : " + DEFAULT_IMG_CAPTCHA_IMPL;
        LOG.fatal(msg, e);
        throw new IllegalStateException(msg);
    } catch (InstantiationException e) {
        // Instantiation Failure
        String msg = "Unable to instantiate class : " + DEFAULT_IMG_CAPTCHA_IMPL;
        LOG.fatal(msg, e);
        throw new IllegalStateException(msg);
    } catch (IllegalAccessException e) {
        // Reflective Access Failure
        String msg = "Unable to instantiate class : " + DEFAULT_IMG_CAPTCHA_IMPL;
        LOG.fatal(msg, e);
        throw new IllegalStateException(msg);
    } catch (Exception e) {
        // Unknown Failure
        String msg = "Unable to instantiate class : " + DEFAULT_IMG_CAPTCHA_IMPL;
        LOG.fatal(msg, e);
        throw new IllegalStateException(msg);
    }
}

From source file:net.geoprism.context.ServerInitializer.java

private static void initialize(List<ServerContextListenerInfo> infos) {
    for (ServerContextListenerInfo info : infos) {
        try {//w ww.  ja va  2 s  . c o  m

            Class<?> clazz = LoaderDecorator.load(info.getClassName());
            Object newInstance = clazz.newInstance();

            ServerContextListener listener = (ServerContextListener) newInstance;
            listener.initialize();

            log.debug("Initialized: " + info.getClassName() + ".setup();");
        } catch (Exception e) {
            log.error(e);

            throw new ProgrammingErrorException(
                    "Unable to startup the server context listener [" + info.getClassName() + "]", e);
        }
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class c = null;
    Object bj = null;//from w w w .  j  av  a  2 s  .  c  o m
    Field field = null;
    int x = 0, statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        bj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(bj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return statusBarHeight;
}

From source file:com.teradata.tempto.internal.query.JdbcUtils.java

private static Driver getDatabaseDriver(JdbcConnectivityParamsState jdbcParamsState) {
    try {//from   w  w  w .  j a v  a 2 s .co m
        Class<?> driverClass = Class.forName(jdbcParamsState.driverClass, true,
                getDriverClassLoader(jdbcParamsState));
        return (Driver) driverClass.newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("could not create JDBC Driver for connection " + jdbcParamsState.getName(),
                e);
    }
}

From source file:org.openmrs.module.kenyaemr.calculation.CalculationManager.java

/**
 * Instantiates and configures a calculation
 * @param clazz the calculation class//w  w  w  .j  a  va  2  s .c om
 * @param configuration the configuration
 * @return the calculation instance
 */
public static BaseEmrCalculation instantiateCalculation(Class<? extends BaseEmrCalculation> clazz,
        String configuration) {
    try {
        BaseEmrCalculation calc = clazz.newInstance();

        if (configuration != null && clazz.isAssignableFrom(ConfigurableCalculation.class)) {
            ((ConfigurableCalculation) calc).setConfiguration(configuration);
        }

        return calc;
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.zhangyue.zeus.util.BeanUtils.java

/**
 * map??BeanBean??mapkey??mapkey?OMIT_REG?
 * //from   w w w  . j  a  v  a2s  . co m
 * @param <E>
 * @param cla
 * @param map
 * @return
 */
@SuppressWarnings({ "rawtypes" })
public static <E> E toBean(Class<E> cla, Map<String, Object> map) {

    // 
    E obj = null;
    try {
        obj = cla.newInstance();
        if (obj == null) {
            throw new Exception();
        }
    } catch (Exception e) {
        LOG.error(",:" + cla);
        return null;
    }

    // ?mapkey
    Map<String, Object> newmap = new HashMap<String, Object>();
    for (Map.Entry<String, Object> en : map.entrySet()) {
        newmap.put("set" + en.getKey().trim().replaceAll(OMIT_REG, "").toLowerCase(), en.getValue());
    }
    // 
    Method[] ms = cla.getMethods();
    for (Method method : ms) {
        String mname = method.getName().toLowerCase();
        if (mname.startsWith("set")) {

            Class[] clas = method.getParameterTypes();
            Object v = newmap.get(mname);

            if (v != null && clas.length == 1) {
                try {
                    method.invoke(obj, v);
                } catch (Exception e) {
                    LOG.error("," + cla + "." + method.getName()
                            + ".?" + clas[0] + ";:" + v.getClass());
                }
            }
        }
    }

    return obj;
}

From source file:com.zxy.commons.lang.utils.MapsUtils.java

/**
 * ?map{@link BeanUtils#populate(Object, Map)}?
 * /*w  w  w.  j a va  2 s  .  c om*/
 * @param map map?
 * @param classes 
 * @return ??
 * @throws IllegalAccessException IllegalAccessException
 * @throws InstantiationException InstantiationException
 * @throws InvocationTargetException InvocationTargetException
 * @see BeanUtils#populate(Object, Map)
 */
public static Object convertObject(Map<String, Object> map, Class<?> classes)
        throws InstantiationException, IllegalAccessException, InvocationTargetException {
    Object object = null;
    object = classes.newInstance();
    BeanUtils.populate(object, map);
    return object;
}

From source file:net.peakplatform.sonar.plugins.spring.rules.SpringRulesRepository.java

private static BeanCheck createCheck(final Class<BeanCheck> checkClass, final ActiveRule activeRule) {
    try {/*from w  w w.ja  v a2s .c o m*/
        BeanCheck check = checkClass.newInstance();
        check.setRule(activeRule.getRule());
        if (activeRule.getActiveRuleParams() != null) {
            for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
                if (!StringUtils.isEmpty(param.getValue())) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Rule param " + param.getKey() + " = " + param.getValue());
                    }

                    BeanUtils.setProperty(check, param.getRuleParam().getKey(), param.getValue());
                }
            }
        }

        return check;
    } catch (IllegalAccessException e) {
        throw new SonarException(e);
    } catch (InvocationTargetException e) {
        throw new SonarException(e);
    } catch (InstantiationException e) {
        throw new SonarException(e);
    }
}

From source file:blackboard.sonar.plugins.css.rules.CssRulesRepository.java

private static AbstractStyleCheck createCheck(Class<AbstractStyleCheck> checkClass, ActiveRule activeRule) {

    try {//w ww  . ja  v  a2s .  c o m
        AbstractStyleCheck check = checkClass.newInstance();
        check.setRule(activeRule.getRule());
        if (activeRule.getActiveRuleParams() != null) {
            for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
                if (!StringUtils.isEmpty(param.getValue())) {
                    LOG.debug("Rule param " + param.getKey() + " = " + param.getValue());
                    BeanUtils.setProperty(check, param.getRuleParam().getKey(), param.getValue());
                }
            }
        }

        return check;
    } catch (IllegalAccessException e) {
        throw new SonarException(e);
    } catch (InvocationTargetException e) {
        throw new SonarException(e);
    } catch (InstantiationException e) {
        throw new SonarException(e);
    }
}

From source file:io.openmessaging.rocketmq.utils.BeanUtils.java

public static <T> T populate(final KeyValue properties, final Class<T> clazz) {
    T obj = null;//  w  ww. j ava2s. co  m
    try {
        obj = clazz.newInstance();
        return populate(properties, obj);
    } catch (Throwable e) {
        log.warn("Error occurs !", e);
    }
    return obj;
}