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:com.bodybuilding.turbine.web.AppInitializer.java

public static ClusterMonitorFactory createClusterMonitorFactory() {
    ClusterMonitorFactory instance = null;
    String className = DynamicPropertyFactory.getInstance().getStringProperty("ClusterMonitorFactory.impl",
            "com.netflix.turbine.plugins.DefaultAggregatorFactory").get();
    try {//from www  . java 2s .  c  om
        Class clazz = Class.forName(className);
        instance = (ClusterMonitorFactory) clazz.newInstance();
    } catch (Exception e) {
        log.error("Could not load ClusterMonitorFactory impl class {} falling back to DefaultAggregatorFactory",
                className, e);
        instance = new DefaultAggregatorFactory();
    }

    return instance;
}

From source file:de.tudarmstadt.ukp.dkpro.tc.ml.modelpersist.ModelPersistUtil.java

public static TCMachineLearningAdapter initMachineLearningAdapter(File tcModelLocation) throws Exception {
    File modelMeta = new File(tcModelLocation, MODEL_META);
    String fileContent = FileUtils.readFileToString(modelMeta);
    Class<?> classObj = Class.forName(fileContent);
    return (TCMachineLearningAdapter) classObj.newInstance();
}

From source file:Main.java

public static XMLReader createXmlReader() throws SAXException {
    try {//  w ww .  ja  v a2 s  .c  o m
        // use Xerces to ensure XML 1.1 is handled correctly
        Class<?> clazz = Class.forName("org.apache.xerces.parsers.SAXParser"); //$NON-NLS-1$
        return (XMLReader) clazz.newInstance();
    } catch (Throwable e) {
        SAXParser saxParser;
        try {
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            saxParserFactory.setNamespaceAware(true);
            saxParser = saxParserFactory.newSAXParser();
        } catch (ParserConfigurationException e2) {
            throw new SAXException(e2);
        }
        return saxParser.getXMLReader();
    }
}

From source file:Main.java

public static <T> void fill(List<T> list, int start, int end, Class<T> clazz) {
    for (int i = start; i <= end; i++) {
        try {/*from  ww w  .j  a  v  a 2s . c  o  m*/

            list.set(i, clazz != null ? clazz.newInstance() : null);

        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.makariev.dynamodb.config.base.BaseRepository.java

private static <T> T newInstance(Class<T> currentClass) {
    try {//from w  w w  . j ava  2 s  .  c om
        final T newEntity = currentClass.newInstance();
        return newEntity;
    } catch (InstantiationException ex) {
        throw new RuntimeException("could not create " + currentClass.getName(), ex);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException("could not create " + currentClass.getName(), ex);
    }
}

From source file:hudson.plugins.clearcase.ucm.model.UcmSelector.java

public static <T extends UcmSelector> T parse(String selector, Class<T> clazz) {
    try {//  www  . j ava  2  s  .com
        T instance = clazz.newInstance();
        instance.init(selector);
        return instance;
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.clicktravel.cheddar.event.AbstractEvent.java

public static <T extends Event> T newEvent(final Class<T> eventClass, final String serializedEvent) {
    try {//ww w. jav a 2s. c  o m
        final T event = eventClass.newInstance();
        event.deserializeAndApply(serializedEvent);
        return event;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException("Could not instantiate event " + eventClass.getName());
    }
}

From source file:Main.java

public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception {
    Field[] fields = cls.getDeclaredFields();
    Object obj = cls.newInstance();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        Method methodSet = cls.getDeclaredMethod(strSet, clsType);
        if (map.containsKey(name)) {
            Object objValue = typeConversion(clsType, map.get(name));
            methodSet.invoke(obj, objValue);
        }//  w w w .j a  v a  2 s  . c  o m
    }
    return obj;
}

From source file:de.tudarmstadt.ukp.dkpro.tc.core.ExperimentStarter.java

/**
 * Method which executes Groovy script provided in the pathToScript.
 * //from w w w .j a va 2  s. c o  m
 * @param pathToScript
 *            path to Groovy script.
 */
public static void start(String pathToScript)
        throws InstantiationException, IllegalAccessException, IOException {
    ClassLoader parent = ExperimentStarter.class.getClassLoader();
    GroovyClassLoader loader = new GroovyClassLoader(parent);

    StringWriter writer = new StringWriter();
    IOUtils.copy(parent.getResourceAsStream(pathToScript), writer, "UTF-8");
    Class<?> groovyClass = loader.parseClass(writer.toString());
    GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
    Object[] a = {};
    groovyObject.invokeMethod("run", a);
    loader.close();
}

From source file:Main.java

public static Object newInstanceQuitly(Class clazz) {
    if (clazz == null) {
        return null;
    }//from  w ww.  j  ava  2  s . c o  m

    try {
        Object instance = clazz.newInstance();
        return instance;
    } catch (IllegalAccessException exp) {
        //NLogger.debug( ClassUtils.class, exp, exp);
        return null;
    } catch (InstantiationException exp) {
        //NLogger.debug( ClassUtils.class, exp, exp);
        return null;
    }
}