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.jberet.support.io.MappingJsonFactoryObjectFactory.java

static void configureCustomSerializersAndDeserializers(final ObjectMapper objectMapper,
        final String customSerializers, final String customDeserializers, final String customDataTypeModules,
        final ClassLoader classLoader) throws Exception {
    if (customDeserializers != null || customSerializers != null) {
        final SimpleModule simpleModule = new SimpleModule("custom-serializer-deserializer-module");
        if (customSerializers != null) {
            final StringTokenizer st = new StringTokenizer(customSerializers, ", ");
            while (st.hasMoreTokens()) {
                final Class<?> aClass = classLoader.loadClass(st.nextToken());
                simpleModule.addSerializer(aClass, (JsonSerializer) aClass.newInstance());
            }//from   www  .  j a  va  2 s  .  c om
        }
        if (customDeserializers != null) {
            final StringTokenizer st = new StringTokenizer(customDeserializers, ", ");
            while (st.hasMoreTokens()) {
                final Class<?> aClass = classLoader.loadClass(st.nextToken());
                simpleModule.addDeserializer(aClass, (JsonDeserializer) aClass.newInstance());
            }
        }
        objectMapper.registerModule(simpleModule);
    }
    if (customDataTypeModules != null) {
        final StringTokenizer st = new StringTokenizer(customDataTypeModules, ", ");
        while (st.hasMoreTokens()) {
            final Class<?> aClass = classLoader.loadClass(st.nextToken());
            objectMapper.registerModule((Module) aClass.newInstance());
        }
    }
}

From source file:com.izforge.izpack.integration.UninstallHelper.java

/**
 * Uninstalls the application at the specified path, by running the GUI uninstaller.
 * <p/>// w w w  . j  a  v a  2s.  com
 * The uninstaller is launched in an isolated class loader as it locates resources using its class loader.
 * This also ensures it has all the classes it needs to run.
 *
 * @param uninstallJar the uninstall jar
 * @throws Exception for any error
 */
public static void guiUninstall(File uninstallJar) throws Exception {
    File copy = copy(uninstallJar);
    ClassLoader loader = getClassLoader(copy);

    @SuppressWarnings("unchecked")
    Class<GUIUninstallerContainer> containerClass = (Class<GUIUninstallerContainer>) loader
            .loadClass(GUIUninstallerContainer.class.getName());
    Object container = containerClass.newInstance();

    // now run the Destroyer. Can't do it via the UninstallerFrame as can't access the uninstall button
    runDestroyer(container, loader, copy);
}

From source file:com.revolsys.util.JavaBeanUtil.java

@SuppressWarnings("unchecked")
public static <V> V createInstance(final String className) {
    try {/*  www . j a  v  a 2s. com*/
        final Class<?> clazz = Class.forName(className);
        return (V) clazz.newInstance();
    } catch (final InstantiationException e) {
        return (V) Exceptions.throwCauseException(e);
    } catch (final Throwable e) {
        return (V) Exceptions.throwUncheckedException(e);
    }
}

From source file:com.momock.util.DataHelper.java

@SuppressWarnings("unchecked")
public static <T, I extends IDataList<?>> DataList<T> getBeanList(IDataList<I> nodes, Class<T> beanClass,
        String... props) {//from  w  w w .j  a  va  2  s .  c o  m
    DataList<T> dl = new DataList<T>();
    for (int i = 0; i < nodes.getItemCount(); i++) {
        try {
            T obj = beanClass.newInstance();
            if (obj instanceof IDataMutableMap) {
                IDataMutableMap<String, Object> target = (IDataMutableMap<String, Object>) obj;
                for (int j = 0; j < props.length; j++) {
                    target.setProperty(props[j], nodes.getItem(i).getItem(j));
                }
            } else {
                for (int j = 0; j < props.length; j++) {
                    BeanHelper.setProperty(obj, props[j], nodes.getItem(i).getItem(j));
                }
            }
            dl.addItem(obj);
        } catch (Exception e) {
            Logger.error(e);
        }
    }
    return dl;
}

From source file:com.impetus.ankush.common.utils.JsonMapperUtil.java

/**
 * Method to Convert the given json string into the specified class object.
 * /*from w ww  . j  a v a2s .c o  m*/
 * @param <S>
 *            the generic type
 * @param jsonString
 *            the json string
 * @param targetClass
 *            the target class
 * @return Object of the class formed using the JSON string.
 */
public static <S> S objectFromString(String jsonString, Class<S> targetClass) {
    S object = null;
    try {
        /* Creating the target class object */
        object = targetClass.newInstance();
    } catch (InstantiationException e) {
        LOG.error(e.getMessage(), e);
    } catch (IllegalAccessException e) {
        LOG.error(e.getMessage(), e);
    }
    try {
        /* Populating the object with json string values. */
        object = mapper.readValue(jsonString, targetClass);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    return object;
}

From source file:com.ksmpartners.ernie.util.TestUtil.java

/**
 * Verifies that an object created from the given Class can be serialized to/from a JSON and the resulting object
 * is equal to the original./*from   w  ww.ja v a 2  s .c  o m*/
 * @param clazz - the Class to be verified
 * @throws InstantiationException, IllegalAccessException - if the object can't be instantiated
 */
public static <T> void verifySerialization(Class<T> clazz)
        throws InstantiationException, IllegalAccessException {
    T t = clazz.newInstance();

    populateFields(clazz, t);

    String json = serialize(t);

    T newT = deserialize(json, clazz);

    String newJson = serialize(newT);

    // TODO: Verify that this is a good test.
    Assert.assertTrue(json.equals(newJson),
            "Pre and post serialization JSONs are not equal for class " + clazz.getCanonicalName());
}

From source file:com.all.shared.sync.SyncGenericConverter.java

private static <T> T createInstance(Class<T> clazz) {
    try {/*ww w  .j  av  a 2 s  .c  o  m*/
        return clazz.newInstance();
    } catch (InstantiationException e1) {
        LOGGER.error("Could not instantiate object of class " + clazz, e1);
    } catch (IllegalAccessException e1) {
        LOGGER.error("Could not instantiate object of class " + clazz + " due to access restrictions.", e1);
    } catch (Exception e) {
        LOGGER.error("Unexpected exception trying to create instance.", e);
    }
    return null;
}

From source file:com.asakusafw.testdriver.DirectIoUtil.java

private static <T> DataFormat<T> newDataFormat(Configuration configuration,
        Class<? extends DataFormat<?>> formatClass) {
    try {// ww w. j  a v a  2 s .c o  m
        @SuppressWarnings("unchecked")
        DataFormat<T> format = (DataFormat<T>) formatClass.newInstance();
        if (format instanceof Configurable) {
            ((Configurable) format).setConf(configuration);
        }
        return format;
    } catch (ReflectiveOperationException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Creates new instance from the given type.
 *
 * @param type class to reflectively instantiate
 * @param <T>  type of the class/*w  ww . j a v  a  2 s .  c  o  m*/
 * @return new instance
 */
public static <T> T newInstance(Class<T> type) {
    try {
        return type.newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

@SuppressWarnings("unchecked")
public static <T> T newInstanceOrReturnNull(String theClassName, Class<T> theType) {
    try {/*from w  w w.  ja v a2s.  c  o  m*/
        Class<?> clazz = Class.forName(theClassName);
        if (!theType.isAssignableFrom(clazz)) {
            throw new ConfigurationException(theClassName + " is not assignable to " + theType);
        }
        return (T) clazz.newInstance();
    } catch (ConfigurationException e) {
        throw e;
    } catch (Exception e) {
        ourLog.info("Failed to instantiate {}: {}", theClassName, e.toString());
        return null;
    }
}