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:Main.java

@SuppressWarnings("unchecked")
public static <T extends Collection<K>, K> T toCollection(Class<?> clazz, Enumeration<K> enumeration) {
    if (clazz == null) {
        throw new RuntimeException("Class cannot be null.");
    }/* ww  w .  j a v a 2 s .c o  m*/
    try {
        T collection = (T) clazz.newInstance();

        if (enumeration != null) {
            while (enumeration.hasMoreElements()) {
                collection.add(enumeration.nextElement());
            }
        }

        return collection;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;/*from  ww w  . j a  v  a  2 s.com*/
    Field field = null;
    int x = 0, sbar = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        sbar = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        Log.e("getStatusBarHight()", "get status bar height fail");
        e1.printStackTrace();
    }
    int statusBarHeight = sbar;
    Log.i("onPreDraw", "statusBarHeight: " + statusBarHeight);
    return statusBarHeight;
}

From source file:com.sun.socialsite.business.Factory.java

/**
 * Bootstrap the SocialSite business tier.
 *
 * Bootstrapping the application effectively instantiates all the necessary
 * pieces of the business tier and wires them together so that the app is
 * ready to run./*from  w  w w .j  a  v a 2s  .c  om*/
 *
 * @throws IllegalStateException If the app has not been properly prepared yet.
 * @throws BootstrapException If an error happens during the bootstrap process.
 */
public static final void bootstrap() throws BootstrapException {

    // if the app hasn't been properly started so far then bail
    if (!Startup.isPrepared()) {
        throw new IllegalStateException("Cannot bootstrap until application has been properly prepared");
    }

    String moduleClassname = Config.getProperty("guice.backend.module");
    if (moduleClassname == null) {
        throw new NullPointerException(
                "unable to lookup default guice module via property 'guice.backend.module'");
    }

    try {
        Class moduleClass = Class.forName(moduleClassname);
        Module module = (Module) moduleClass.newInstance();
        injector = Guice.createInjector(module);

    } catch (Throwable e) {
        // Fatal misconfiguration, cannot recover
        throw new RuntimeException("Error instantiating backend module " + moduleClassname, e);
    }

    socialSite = injector.getInstance(SocialSite.class);

    // make sure we are all set
    if (socialSite == null) {
        throw new BootstrapException("Bootstrapping failed, SocialSite instance is null");
    }

    log.info("SocialSite business tier successfully bootstrapped");
}

From source file:io.github.lxgaming.teleportbow.util.Toolbox.java

public static <T> Optional<T> newInstance(Class<? extends T> typeOfT) {
    try {//w  ww.  ja v  a  2 s .c  o m
        return Optional.of(typeOfT.newInstance());
    } catch (Exception ex) {
        return Optional.empty();
    }
}

From source file:cc.redpen.validator.ValidatorFactory.java

private static Validator createValidator(Class<? extends Validator> clazz) {
    try {//from  w w w.ja  va  2s.co  m
        return clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("Cannot create instance of " + clazz + " using default constructor");
    }
}

From source file:Main.java

/**
 * Used to optimize performance by avoiding expensive file access every time
 * a DTMManager is constructed as a result of constructing a Xalan xpath
 * context!//from w  w  w . j  a  v a  2s  . co m
 */
private static void speedUpDTMManager() throws Exception {
    // https://github.com/aws/aws-sdk-java/issues/238
    // http://stackoverflow.com/questions/6340802/java-xpath-apache-jaxp-implementation-performance
    if (System.getProperty(DTM_MANAGER_DEFAULT_PROP_NAME) == null) {
        Class<?> XPathContextClass = Class.forName(XPATH_CONTEXT_CLASS_NAME);
        Method getDTMManager = XPathContextClass.getMethod("getDTMManager");
        Object XPathContext = XPathContextClass.newInstance();
        Object dtmManager = getDTMManager.invoke(XPathContext);

        if (DTM_MANAGER_IMPL_CLASS_NAME.equals(dtmManager.getClass().getName())) {
            // This would avoid the file system to be accessed every time
            // the internal XPathContext is instantiated.
            System.setProperty(DTM_MANAGER_DEFAULT_PROP_NAME, DTM_MANAGER_IMPL_CLASS_NAME);
        }
    }
}

From source file:Main.java

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

From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java

/**
 * For each tag <chart> in configuration file, try to generate a chart
 * according to its parameters//  w  w w  .ja v  a  2  s .c om
 *
 * @param serieResults
 *            merged values
 * @param bstats
 *            full statistics (used for non-mergable values like commevents)
 * @param charts
 *            array from <chart> tags in configuration file
 */
public static void generatingCharts(Element eTimitResult, BenchmarkStatistics bstats, ConfigChart[] charts) {
    if (charts == null) {
        return;
    }
    TimIt.message(2, "Generating charts...");
    String className = null;

    for (ConfigChart cChart : charts) {
        TimIt.message(4, "Generating " + cChart.get("title") + " [" + cChart.get("subtitle") + "]" + "...");
        try {
            className = Utilities.class.getPackage().getName() + "." + cChart.get("type");
            Class<?> chartClass = Class.forName(className);
            Chart chart = (Chart) chartClass.newInstance();
            chart.generateChart(eTimitResult, bstats, cChart);
        } catch (ClassNotFoundException e) {
            System.err.println("  Fail: Unknown chart type: " + className);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

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

From source file:app.core.PersistenceTest.java

public static <T extends PersistentObject> T newInstance(Class<T> clazz) throws Exception {
    return buildRecursively(clazz.newInstance());
}