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:Main.java

public static Object getObjectByClasspath(String classpath) {
    Object object = null;//from   w w  w.j  a  va 2s  .  c o  m
    try {
        Class clazz = Class.forName(classpath);
        object = clazz.newInstance();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return object;
}

From source file:Main.java

public static Object newInstance(Object model) {
    Object instance = null;// w  w w .  j  a v a2  s .c  o m
    try {
        Class<?> aClass = model.getClass();

        instance = aClass.newInstance();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return instance;
}

From source file:com.amar.web.demo.utils.ServletUtil.java

@SuppressWarnings("rawtypes")
public static <T> T request2Bean(HttpServletRequest request, Class<T> beanClass) {
    try {//from  w ww. ja v  a2s.  c  o m
        T bean = beanClass.newInstance();
        Map map = request.getParameterMap();
        BeanUtils.populate(bean, map);
        return bean;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static <T> Object newInstance(Class<T> clazz) throws InstantiationException, IllegalAccessException {

    return clazz.newInstance();
}

From source file:Main.java

public static <T> T getClassByName(String className, Class<T> classOfT) {
    try {//from  w  w w.  java  2s . c o  m
        Class<?> clazz = Class.forName(className);
        Object instance = clazz.newInstance();
        if (classOfT.isInstance(instance)) {
            return classOfT.cast(instance);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static <C extends Collection<T>, T> Collection<T> fromArray(T[] array, Class<C> clazz) {
    try {//from www.  j  ava  2 s.  com
        Collection<T> collection = clazz.newInstance();
        for (T o : array) {
            collection.add(o);
        }
        return collection;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Object getBean(String className) {
    try {/*from  w w w. j av  a 2 s  . com*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName(className);
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();

        Class c = Class.forName(chartType);
        return c.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object getBean(String filePath) {
    try {//from  w  w  w .j a v  a2s  .c  om
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File(filePath));

        NodeList nl = doc.getElementsByTagName("className");
        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();
        System.out.println(cName);
        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object getBean() {
    try {// ww w.j  av  a  2  s  .  c  om
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File("src/config.xml"));

        NodeList nl = doc.getElementsByTagName("className");
        Node node = nl.item(0).getFirstChild();
        String cName = node.getNodeValue();

        Class clazz = Class.forName(cName);
        Object obj = clazz.newInstance();
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static <InstanceType> InstanceType instantiate(Class<InstanceType> cls) throws IllegalArgumentException {
    try {//from ww  w .  j  a v  a2  s .  c  o m
        return cls.newInstance();
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}