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.tinymediamanager.scraper.entities.MediaGenres.java

/**
 * get all available Languages. Here we use reflection to get rid of the dependency to the tmm core. If org.tinymediamanager.core.Utils is not in
 * our classpath, we only use en as available language
 * /*w ww  .j av  a2 s  .c o m*/
 * @return all available languages
 */
@SuppressWarnings("unchecked")
private static List<Locale> getLanguages() {
    try {
        Class<?> clazz = Class.forName("org.tinymediamanager.core.Utils");
        Object obj = clazz.newInstance();

        Method method = clazz.getDeclaredMethod("getLanguages");
        if (method.getReturnType() == List.class) {
            return (List<Locale>) method.invoke(obj);
        }
    } catch (Exception ignored) {
    }
    return Arrays.asList(new Locale("en", "US"));
}

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

/**
 * Uninstalls the application at the specified path, by running the {@link Destroyer} in the supplied uninstall
 * jar, using the console uninstaller container.
 * <p/>//from w  ww .j  a  va2s  . c  o  m
 * The Destroyer 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 consoleUninstall(File uninstallJar) throws Exception {
    File copy = copy(uninstallJar);
    ClassLoader loader = getClassLoader(copy);

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

    runDestroyer(container, loader, copy);
}

From source file:Main.java

public static Collection asTargetTypeCollection(Collection c, Class targetCollectionClass) {
    if (targetCollectionClass == null)
        throw new IllegalArgumentException("'targetCollectionClass' must be not null");
    if (c == null)
        return null;
    if (targetCollectionClass.isInstance(c))
        return c;

    Collection result = null;/*w  w  w . j  av  a  2 s .  c  o  m*/

    try {
        result = (Collection) targetCollectionClass.newInstance();
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "targetCollectionClass=" + targetCollectionClass.getName() + " is not correct!", e);
    }

    result.addAll(c);
    return result;
}

From source file:com.cloudera.sqoop.tool.SqoopTool.java

/**
 * @return the SqoopTool instance with the provided name, or null
 * if no such tool exists.//from   w  w w .ja v a 2  s  .  c  o  m
 */
public static final SqoopTool getTool(String toolName) {
    Class<? extends SqoopTool> cls = TOOLS.get(toolName);
    try {
        if (null != cls) {
            SqoopTool tool = cls.newInstance();
            tool.setToolName(toolName);
            return tool;
        }
    } catch (Exception e) {
        LOG.error(StringUtils.stringifyException(e));
        return null;
    }

    return null;
}

From source file:com.startechup.tools.ModelParser.java

/**
 * Creates a new instance of the model class.
 *
 * @param classModel The class that will contain the parse json values and the one
 *                   that will direct the parsing.
 * @return Returns a new object instance of the model class, null if exceptions occur.
 *//*from  www.j ava2  s  .com*/
private static Object getNewInstance(Class classModel) {
    Object object = null;
    try {
        object = classModel.newInstance();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }

    return object;
}

From source file:main.RankerOCR.java

/**
 * Compare the documents and rank them.//from w ww .j  a v a  2s.  c o m
 * <p>
 * @param d1 Origin text
 * @param d2 Comparative text
 * @param ranker Class of the ranker to use
 * @return Return the percent of difference between documents
 */
private static double rankDocuments(String d1, String d2, Class ranker) {
    try {
        Ranker instance = (Ranker) ranker.newInstance();
        return instance.compare(d1, d2);
    } catch (InstantiationException | IllegalAccessException ex) {
        printFormated(ex.getLocalizedMessage());
        System.exit(-100);
        return -1;
    }
}

From source file:org.jberet.support.io.JsonItemWriter.java

protected static JsonGenerator configureJsonGenerator(final JsonFactory jsonFactory,
        final OutputStream outputStream, final Class<?> outputDecorator,
        final Map<String, String> jsonGeneratorFeatures) throws Exception {
    if (outputDecorator != null) {
        jsonFactory.setOutputDecorator((OutputDecorator) outputDecorator.newInstance());
    }/*from w w w  .j av a2 s. c  o  m*/
    final JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream);

    if (jsonGeneratorFeatures != null) {
        for (final Map.Entry<String, String> e : jsonGeneratorFeatures.entrySet()) {
            final String key = e.getKey();
            final String value = e.getValue();
            final JsonGenerator.Feature feature;
            try {
                feature = JsonGenerator.Feature.valueOf(key);
            } catch (final Exception e1) {
                throw SupportMessages.MESSAGES.unrecognizedReaderWriterProperty(key, value);
            }
            if ("true".equals(value)) {
                if (!feature.enabledByDefault()) {
                    jsonGenerator.configure(feature, true);
                }
            } else if ("false".equals(value)) {
                if (feature.enabledByDefault()) {
                    jsonGenerator.configure(feature, false);
                }
            } else {
                throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, value, key);
            }
        }
    }

    return jsonGenerator;
}

From source file:clearUp.Collections3.java

public static <T extends BaseEntity> List<T> maplist2EntityMapList(List<Map<String, Object>> mapList,
        Class<T> beanClass) {
    List<T> newList = new ArrayList<T>();
    for (Map<String, Object> map : mapList) {
        try {/* w w w  .  ja v  a 2s. c o  m*/
            T t = beanClass.newInstance();
            t.setAll(map);
            newList.add(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return newList;
}

From source file:com.ms.commons.lang.BeanUtils.java

public static <T extends Object> List<T> convert(Class<T> clazz, Collection<?> raw,
        ValueEditable... specialConverts) {
    if (Argument.isEmpty(raw)) {
        return Collections.emptyList();
    }/*  w  ww  . jav  a2s. co m*/
    List<T> data = new ArrayList<T>(raw.size());
    for (Object obj : raw) {
        T vo;
        try {
            vo = clazz.newInstance();
            copyProperties(vo, obj, specialConverts);
            data.add(vo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return data;
}

From source file:Main.java

static java.util.Collection createConcreteJavaCollection(Class clazz)
        throws InstantiationException, IllegalAccessException {
    int modifiers = clazz.getModifiers();
    java.util.Collection result = null;
    if ((modifiers & ACC_ABSTRACT) == 0 && (modifiers & ACC_INTERFACE) == 0) {
        // class is concrete, we can instantiate it directly
        result = (java.util.Collection) clazz.newInstance();
    } else {/*from   w  ww .j ava 2s. co m*/
        // class is either abstract or an interface, we have to somehow choose a concrete collection :-(
        if (java.util.List.class.isAssignableFrom(clazz)) {
            result = new java.util.ArrayList();
        } else if (java.util.Set.class.isAssignableFrom(clazz)) {
            result = new java.util.HashSet();
        }
    }
    return result;
}