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 int getStatusHeight(Context context) {

    int statusHeight = -1;
    try {// w w  w .j  a  v  a  2 s  .com
        Class clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
        statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusHeight;
}

From source file:com.liferay.tool.datamanipulator.util.GetterUtil.java

public static Object getNewInstance(String className)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {

    Class<?> clazz = getClass(className);

    return clazz.newInstance();
}

From source file:Main.java

public static Object xmlToBean(Node beanNode) throws ClassNotFoundException, IllegalAccessException,
        InstantiationException, NoSuchMethodException, InvocationTargetException {
    String className = beanNode.getNodeName();
    System.out.println(className);
    Class clazz = Class.forName(className);
    Object bean = clazz.newInstance();
    NodeList fieldNodeList = beanNode.getChildNodes();
    for (int i = 0; i < fieldNodeList.getLength(); i++) {
        Node fieldNode = fieldNodeList.item(i);
        if (fieldNode.getNodeType() == Node.ELEMENT_NODE) {
            String fieldName = fieldNode.getNodeName();
            if (!fieldName.contains(".")) {
                String getName = analyzeMethodName(fieldName, "get");
                String setName = analyzeMethodName(fieldName, "set");
                System.out.println(setName);
                clazz.getMethod(setName, clazz.getMethod(getName).getReturnType()).invoke(bean,
                        fieldNode.getTextContent());
            }/*from  w  w w.j a  v a  2 s . c om*/
        }
    }
    System.out.println(bean);
    return bean;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static int getStatusBarHeigh(Context context) {
    int heigh = 0;
    try {// ww  w  . j av a2s.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());
        heigh = context.getResources().getDimensionPixelSize(x);

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

    return heigh;
}

From source file:Main.java

public static Object loadClass(Context context, String path) {
    try {/*w  ww  . j a va2 s.  co m*/
        String dexPath = context.getApplicationInfo().sourceDir;
        PathClassLoader pathClassLoader = new PathClassLoader(dexPath, context.getClassLoader());
        Class<?> c = Class.forName(path, true, pathClassLoader);
        Object ret = c.newInstance();
        return ret;
    } catch (InstantiationException ex1) {
        ex1.printStackTrace();
    } catch (IllegalAccessException ex2) {
        ex2.printStackTrace();
    } catch (ClassNotFoundException ex3) {
        ex3.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static int getStatusBarHeight(Activity activity) {
    try {/* w w  w  .ja v  a2  s . co  m*/
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        Field field = clazz.getField("status_bar_height");
        int dpHeight = Integer.parseInt(field.get(object).toString());
        return activity.getResources().getDimensionPixelSize(dpHeight);
    } catch (Exception e1) {
        e1.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    if (sStatusBarHeight == 0) {
        try {/* w w  w. ja  v a2 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);
            sStatusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sStatusBarHeight;
}

From source file:Main.java

public static int getStatusHeight(Activity activity) {
    int statusBarHeight = 0;
    try {/*from  w w  w .j a  v  a2 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 = activity.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
        e.printStackTrace();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        statusBarHeight = frame.top;
    }
    return statusBarHeight;
}

From source file:Main.java

public static int getStatusHeight(Context context) {

    int statusHeight = -1;
    try {//from  ww  w .  j av  a 2s  . c om
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
        statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusHeight;
}

From source file:Main.java

private static <C extends Collection<T>, T> C toCollection(Class<C> cls, Iterable<T> iterable) {
    try {// ww w . ja  va 2 s. com
        C result = cls.newInstance();

        for (T item : iterable) {
            result.add(item);
        }

        return result;
    } catch (InstantiationException ex) {
        return null;
    } catch (IllegalAccessException ex) {
        return null;
    }
}