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 getClassFromXML() throws Exception {
    //create DOM object
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("property.xml"));

    //get the node name and get the class name
    NodeList nodeList = document.getElementsByTagName("className");
    Node node = nodeList.item(0).getFirstChild();
    String className = node.getNodeValue();

    //return an object through the class name
    Class tempClass = Class.forName(className);
    Object object = tempClass.newInstance();
    return object;
}

From source file:Main.java

public static Object getBean(String args) {
    try {// w  ww. j  av a 2  s.  c o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File("config.xml"));
        NodeList nl = null;
        Node classNode = null;
        String cName = null;
        nl = doc.getElementsByTagName(args);

        classNode = nl.item(0).getFirstChild();
        cName = classNode.getNodeValue();

        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.github.mushkevych.genfabeto.Ganfabeto.java

public static Object createObject(String className)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    Class classDefinition = Class.forName(className);
    return classDefinition.newInstance();
}

From source file:com.adavr.player.Launcher.java

public static Object loadClass(String className) {
    try {/*  ww  w  .  j  a va 2s  . c  o  m*/
        Class clazz = Class.forName(className);
        return clazz.newInstance();
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

public static Object getBean(String relativePath, String nodeName) {
    try {/*w w w. ja va 2 s  . c  o m*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File(path + relativePath));

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

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

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    int statusBarHeight = 0;
    try {/*from  w  w w .  j  a v a  2s  . co m*/
        Class<?> c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("status_bar_height");
        int x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
    }
    return statusBarHeight;
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    try {// ww  w.j a v a 2s  .c  o m
        Class<?> c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("status_bar_height");
        int x = Integer.parseInt(field.get(obj).toString());
        return context.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static int systemBarHeight(Context context) {
    int sbar = 0;
    try {/*from  w  w  w.j  av  a2s  . co  m*/
        Class<?> c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("status_bar_height");
        int x = Integer.parseInt(field.get(obj).toString());
        sbar = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sbar;
}

From source file:org.zkybase.cmdb.util.ReflectionUtils.java

public static <T> T newInstance(Class<T> clazz) {
    try {/*from   ww w  .j a  va 2 s .  c o  m*/
        return clazz.newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    int statusBarHeight = 0;
    if (statusBarHeight == 0) {
        try {//from ww w . j  a v a 2 s  . co  m
            Class c = Class.forName("com.android.internal.R$dimen");
            Object o = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = (Integer) field.get(o);
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return statusBarHeight;
}