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

public static int getResourceId(String packageName, String resFileName, String parameterName) {
    if ((packageName != null) && (resFileName != null) && (parameterName != null))
        try {//from  w  w w.ja  va  2 s .c o m
            Class localClass = Class.forName(packageName + "$" + resFileName);
            Field localField = localClass.getField(parameterName);
            Object localObject = localField.get(localClass.newInstance());
            return Integer.parseInt(localObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    return -1;
}

From source file:Main.java

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

From source file:fr.calamus.common.db.core.DbCentralFactory.java

public static <T extends DbCentralFactory> T init(Class<T> factoryClass) {
    T db = null;//from   w  w w.  ja  va  2s .  c o m
    try {
        db = (T) factoryClass.newInstance();
        instances.put(null, db);
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    }
    launchInstancesObserver();
    return db;
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;/*from   w  w w  . j  ava 2s.  c  o  m*/
    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) {
        e1.printStackTrace();
    }
    return sbar;
}

From source file:com.amazonaws.services.kinesis.clientlibrary.config.AWSCredentialsProviderPropertyValueDecoder.java

private static List<AWSCredentialsProvider> getValidCredentialsProviders(List<String> providerNames) {
    List<AWSCredentialsProvider> credentialsProviders = new ArrayList<AWSCredentialsProvider>();
    for (String providerName : providerNames) {
        try {/*from  w w w . ja  v a2  s.  co m*/
            Class<?> className = Class.forName(providerName);
            credentialsProviders.add((AWSCredentialsProvider) className.newInstance());
        } catch (Exception e) {
            LOG.debug("Can't find any credentials provider matching " + providerName + ".");
        }
    }
    return credentialsProviders;
}

From source file:Main.java

private static int getStatusBarHeight(Activity activity) {
    Class<?> c = null;
    Object obj = null;//  w  w w  .  java2 s .c o  m
    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 = activity.getResources().getDimensionPixelSize(x);
        return sbar;
    } catch (Exception e1) {
        //       loge("get status bar height fail");
        e1.printStackTrace();
        return 0;
    }
}

From source file:org.codehaus.mojo.hibernate3.processor.ProcessorUtil.java

public static List<GeneratedClassProcessor> buildProcessorsFromProperty(String delim, String processorsProperty)
        throws Throwable {

    List<Class<? extends GeneratedClassProcessor>> procs = parseProcesssorsFromProperty(delim,
            processorsProperty);/*  w ww. j  a v a 2  s.  c  om*/
    List<GeneratedClassProcessor> processors = new ArrayList<GeneratedClassProcessor>(procs.size());

    for (Class<? extends GeneratedClassProcessor> c : procs) {
        GeneratedClassProcessor processObj = c.newInstance();
        processors.add(processObj);
    }
    return processors;
}

From source file:com.opensymphony.xwork2.util.logging.LoggerFactory.java

protected static LoggerFactory getLoggerFactory() {
    lock.readLock().lock();/*ww w.j  ava 2s . c o  m*/
    try {
        if (factory != null) {
            return factory;
        }
    } finally {
        lock.readLock().unlock();
    }
    lock.writeLock().lock();
    try {
        if (factory == null) {
            String userLoggerFactory = System.getProperty(XWorkConstants.XWORK_LOGGER_FACTORY);
            if (userLoggerFactory != null) {
                try {
                    Class clazz = Class.forName(userLoggerFactory);
                    factory = (LoggerFactory) clazz.newInstance();
                } catch (Exception e) {
                    throw new XWorkException(
                            "System property [" + XWorkConstants.XWORK_LOGGER_FACTORY + "] was defined as ["
                                    + userLoggerFactory + "] but there is a problem to use that LoggerFactory!",
                            e);
                }
            } else {
                try {
                    Class.forName("org.apache.commons.logging.LogFactory");
                    factory = new com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory();
                } catch (ClassNotFoundException ex) {
                    //commons-logging not found try slf4j LogFactory
                    try {
                        Class.forName("org.slf4j.LoggerFactory");
                        factory = new Slf4jLoggerFactory();
                    } catch (ClassNotFoundException cnfex) {
                        // slf4j not found, falling back to jdk logging
                        factory = new JdkLoggerFactory();
                    }
                }
            }
        }
        return factory;
    } finally {
        lock.writeLock().unlock();
    }
}

From source file:com.linkedin.pinot.core.query.pruner.SegmentPrunerProvider.java

public static SegmentPruner getSegmentPruner(String prunerClassName, Configuration segmentPrunerConfig) {
    try {/*from ww  w .j  a v  a 2 s  .c  o  m*/
        Class<? extends SegmentPruner> cls = keyToFunction.get(prunerClassName.toLowerCase());
        if (cls != null) {
            SegmentPruner segmentPruner = (SegmentPruner) cls.newInstance();
            segmentPruner.init(segmentPrunerConfig);
            return segmentPruner;
        }
    } catch (Exception ex) {
        throw new RuntimeException("Not support SegmentPruner type with - " + prunerClassName, ex);
    }
    throw new UnsupportedOperationException("No SegmentPruner type with - " + prunerClassName);
}

From source file:es.amplia.research.maven.protodocbook.cmd.Factory.java

@SuppressWarnings("unchecked")
public static Cmd newCommand(String cmd)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {

    String packageName = Factory.class.getPackage().getName();

    String className = new StringBuilder().append(packageName).append(".").append(StringUtils.capitalize(cmd))
            .append("Cmd").toString();

    Class<Cmd> cmdClass = ((Class<Cmd>) Class.forName(className));

    return cmdClass.newInstance();
}