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:org.arrow.util.DelegateUtil.java

/**
 * Creates a new {@link TransitionEvaluation} instance of the
 * class with the given name. Throws an IllegalArgumentException if the
 * class could not be instantiated./*from   w  w  w.j  a v a  2  s. co m*/
 *
 * @param className the transition evaluation class name
 * @return JavaDelegate
 */
public static TransitionEvaluation getTransitionEvaluation(String className) {

    Assert.notNull(className, "className must not be null");

    try {
        if (cache.containsKey(className)) {
            Object obj = cache.get(className);
            return (TransitionEvaluation) obj;
        }

        Class<?> clazz = Class.forName(className);
        TransitionEvaluation evaluation;
        evaluation = (TransitionEvaluation) clazz.newInstance();

        cache.put(className, evaluation);
        return evaluation;
    } catch (Exception e) {
        throw new IllegalArgumentException("transition evaluation delegation error", e);
    }
}

From source file:de.ncoder.studipsync.ui.StandardUIAdapter.java

public static UIAdapter loadUIAdapter(String classname) throws ParseException, ClassNotFoundException {
    try {//w  ww . j  a  va2  s .  c o m
        Class<?> clazz = Class.forName(classname);
        Object instance = clazz.newInstance();
        if (!(instance instanceof UIAdapter)) {
            throw new ParseException(instance + " is not an UI adapter");
        }
        return (UIAdapter) instance;
    } catch (InstantiationException | IllegalAccessException e) {
        ParseException pe = new ParseException(
                "Could not instantiate class " + classname + ". " + e.getMessage());
        pe.initCause(e);
        throw pe;
    }
}

From source file:de.micromata.genome.gwiki.utils.ClassUtils.java

public static <T> T createDefaultInstance(Class<? extends T> cls) {
    try {//from  ww  w .j av  a2s.  c  o m
        return cls.newInstance();
    } catch (Throwable ex) {
        throw new RuntimeException("Cannot Instantiate class instance: " + ex.toString(), ex);
    }
}

From source file:com.esofthead.mycollab.configuration.StorageManager.java

@SuppressWarnings("unchecked")
static void loadStorageConfig() {
    // Load storage configuration
    String storageSystem = ApplicationProperties.getString(ApplicationProperties.STORAGE_SYSTEM,
            StorageConfiguration.FILE_STORAGE_SYSTEM);
    instance.storageSystem = storageSystem;
    if (StorageConfiguration.FILE_STORAGE_SYSTEM.equals(storageSystem)) {
        LOG.debug("MyCollab uses file storage system");
        instance.storageConf = new FileStorageConfiguration();
    } else if (StorageConfiguration.S3_STORAGE_SYSTEM.equals(storageSystem)) {
        LOG.debug("MyCollab uses amazon s3 system");
        try {/*from w w  w.j  a v a2  s  . c o  m*/
            Class<StorageConfiguration> s3Conf = (Class<StorageConfiguration>) Class.forName(S3_CONF_CLS);
            StorageConfiguration newInstance = s3Conf.newInstance();
            instance.storageConf = newInstance;
        } catch (Exception e) {
            LOG.error("Can not load s3 file system with class " + S3_CONF_CLS, e);
            System.exit(-1);
        }
    } else {
        throw new MyCollabException("Can not load storage  " + storageSystem);
    }
}

From source file:com.google.gdt.eclipse.designer.smart.parser.ClassLoaderValidator.java

/**
 * Initialize SmartGWT environment.//ww  w .  j av a  2s.c om
 */
private static void initializeSmartGWT(final ClassLoader classLoader) throws Exception {
    // try process SmartGWT initialization routine
    try {
        // SmartGwtEntryPoint for version 2.3
        Class<?> smartGWTEntryPointClass = classLoader.loadClass("com.smartgwt.client.SmartGwtEntryPoint");
        Object smartGWTEntryPoint = smartGWTEntryPointClass.newInstance();
        ReflectionUtils.invokeMethod2(smartGWTEntryPoint, "onModuleLoad");
    } catch (ClassNotFoundException e) {
        // do nothing
    }
}

From source file:corner.util.BeanUtils.java

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

From source file:de.ncoder.studipsync.storage.StandardPathResolver.java

public static PathResolver loadPathResolver(String classname) throws ParseException, ClassNotFoundException {
    try {/*from  w  ww.  jav  a  2 s . com*/
        Class<?> clazz = Class.forName(classname);
        Object instance = clazz.newInstance();
        if (!(instance instanceof PathResolver)) {
            throw new ParseException(instance + " is not a PathResolver");
        }
        return (PathResolver) instance;
    } catch (InstantiationException | IllegalAccessException e) {
        ParseException pe = new ParseException(
                "Could not instantiate class " + classname + ". " + e.getMessage());
        pe.initCause(e);
        throw pe;
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;//from   ww w . j a 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:com.amazonaws.services.s3.internal.crypto.CryptoRuntime.java

public static void enableBouncyCastle() {
    try {//w ww . j  a  va2 s. c  o  m
        @SuppressWarnings("unchecked")
        Class<Provider> c = (Class<Provider>) Class.forName(BC_PROVIDER_FQCN);
        Provider provider = c.newInstance();
        Security.addProvider(provider);
    } catch (Exception e) {
        LogFactory.getLog(CryptoRuntime.class).debug("Bouncy Castle not available", e);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c;
    Object obj;/*from  w  w  w  . jav a 2s.  c o  m*/
    Field field;
    int x, 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;
    /**
     * Rect frame = new Rect();
            
     getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            
     int statusBarHeight = frame.top;
     */
}