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:eu.planets_project.pp.plato.services.action.PreservationActionRegistryFactory.java

/**
 * Returns an instance of a IPreservationActionRegistry, 
 * which is already connected to the remote endpoint.
 * /*  w  ww. j  a v a 2 s  . c  o m*/
 * @param registry defines type and loaction of the registry     
 * @return {@link IPreservationActionRegistry}
 */
static public IPreservationActionRegistry getInstance(PreservationActionRegistryDefinition registry)
        throws IllegalArgumentException {
    try {
        Class serviceLocatorClass = Class.forName(registry.getType());

        Object serviceLocator = serviceLocatorClass.newInstance();
        if (serviceLocator instanceof IPreservationActionRegistry) {
            IPreservationActionRegistry locator = null;
            locator = (IPreservationActionRegistry) serviceLocator;

            locator.connect(registry.getUrl());
            return locator;
        } else
            throw new IllegalArgumentException(
                    "schema  " + registry.getType() + " is not a IPreservationActionRegistry.");
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "schema '" + registry.getType() + "' is not a IPreservationActionRegistry", e);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c;
    Object obj;//  w  w  w .j ava  2 s  .co 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;
}

From source file:com.antsdb.saltedfish.sql.mysql.InstructionGenerator.java

@SuppressWarnings("unchecked")
static public Generator<ParseTree> getGenerator(ParseTree ctx) throws OrcaException {
    Class<?> klass = ctx.getClass();
    Generator<ParseTree> generator = _generatorByName.get(klass);
    if (generator == null) {
        String key = StringUtils.removeStart(klass.getSimpleName(), "MysqlParser$");
        key = StringUtils.removeEnd(key, "Context");
        key += "Generator";
        try {//  www .j a v  a2 s . c  o  m
            key = InstructionGenerator.class.getPackage().getName() + "." + key;
            Class<?> generatorClass = Class.forName(key);
            generator = (Generator<ParseTree>) generatorClass.newInstance();
            _generatorByName.put(klass, generator);
        } catch (Exception x) {
            throw new OrcaException("instruction geneartor is not found: " + key, x);
        }
    }
    return generator;
}

From source file:Main.java

/**
 * Sorts a collection using a comparator and returns it as a {@link List}
 *
 * @param c       Collection to be sorted
 * @param k       Comparator to sort by/*from  w  ww.  jav  a2  s.c  om*/
 * @param reverse Whether to reverse the sort order
 * @return a {@link List} of the sorted elements
 * @throws IllegalAccessException when unable to access the comparator class
 * @throws InstantiationException when unable to instantiate to comparator class
 */
public static <E> List<E> sortByCompare(Collection<E> c, Class<? extends Comparator<E>> k, boolean reverse)
        throws IllegalAccessException, InstantiationException {
    Comparator<E> comp = k.newInstance();
    int moves = 0;
    boolean firstRun = true;
    LinkedList<E> l = new LinkedList<>(c);

    while (moves > 0 || firstRun) {
        firstRun = false;
        moves = 0;

        for (int i = 1; i < l.size(); i++) {
            E a = l.get(i - 1);
            E b = l.get(i);
            if (reverse ? comp.compare(a, b) < 0 : comp.compare(a, b) > 0) {
                l.set(i - 1, b);
                l.set(i, a);
                moves++;
            }
        }
    }
    return l;
}

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;
    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());
        return context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
        return 75;
    }
}

From source file:org.jongo.model.IdSpecSet.java

public static <T> T newInstanceWithId(Class<T> spec, ObjectId id) {
    try {//from w ww.j a va  2  s.c o  m
        return id(spec.newInstance(), id);
    } catch (Exception e) {
        throw new RuntimeException("could not create spec instance", e);
    }
}

From source file:com.creditcloud.ump.model.ump.base.BaseResponse.java

public static <T extends BaseResponse> T fromHTML(String html, Class<T> theClass) {
    T response = null;/*w  w w  .j a v a 2 s . com*/
    try {
        response = theClass.newInstance();
        Map<String, String> fieldValueMaps = MessageUtils.getFieldValuesMap(html);
        BeanUtils.populate(response, fieldValueMaps);
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
        String errMsg = String.format("create response object for:%s from:%s error", theClass.getName(), html);
        throw new RuntimeException(errMsg, ex);
    }

    return response;
}

From source file:gridool.processors.job.GridJobProcessor.java

private static <A, R> GridJob<A, R> newJobInstance(Class<? extends GridJob<A, R>> jobClass)
        throws GridException {
    try {/*from www .  jav a2s . c o m*/
        return jobClass.newInstance();
    } catch (InstantiationException e) {
        String errmsg = "Failed to instantiate a job class: " + jobClass;
        LOG.error(errmsg);
        throw new GridException(errmsg, e);
    } catch (IllegalAccessException iae) {
        String errmsg = "Failed to instantiate a job class: " + jobClass;
        LOG.error(errmsg);
        throw new GridException(errmsg, iae);
    }
}

From source file:Main.java

/**
 * createXMLDoc(String) Given a name for the root element will create and
 * return a org.w3c.dom.Document with the root node specified. The Document
 * will be based on the Document implementation as specified in the system
 * properties file XML_DOCUMENT_CLASS/*from  www  .  j  a  va2s .c  o m*/
 * 
 * @param String
 *            rootName
 * @return Document
 * @author Peter Manta (after blatant copying of Jeff)
 */
public static org.w3c.dom.Document createXMLDoc(String rootName) {
    Class docClass = null;
    Document doc = null;
    Element root = null;
    try {
        docClass = Class.forName(XML_DOCUMENT_IMPL);
        doc = (Document) docClass.newInstance();
        root = doc.createElement(rootName);
        doc.appendChild(root);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }
    return doc;
}

From source file:com.mgmtp.jfunk.core.config.ModulesLoader.java

/**
 * Loads Guice modules whose class names are specified as properties. All properties starting
 * with "module." are considered to have a fully qualified class name representing a Guice
 * module. The modules are combined and override thespecified jFunkModule.
 * //from www. j a  va 2  s.c om
 * @param propertiesFile
 *            The properties file
 * @return the combined module
 */
public static Module loadModulesFromProperties(final Module jFunkModule, final String propertiesFile)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
    final List<Module> modules = Lists.newArrayList();

    LOG.debug("Using jfunk.props.file=" + propertiesFile);
    Properties props = loadProperties(propertiesFile);

    for (final Enumeration<?> en = props.propertyNames(); en.hasMoreElements();) {
        String name = (String) en.nextElement();
        if (name.startsWith("module.")) {
            String className = props.getProperty(name);
            LOG.info("Loading " + name + "=" + className);
            Class<? extends Module> moduleClass = Class.forName(className).asSubclass(Module.class);
            Module module = moduleClass.newInstance();
            modules.add(module);
        }
    }
    return Modules.override(jFunkModule).with(modules);
}