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

/**
 * Returns status bar height;/*w w  w  .ja  va2 s. c o  m*/
 * 
 * @param context
 * @return
 */
public static int getStatusBarHeight(Context context) {
    int height = 0;
    try {
        Class<?> c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("status_bar_height");
        int temp = Integer.parseInt(field.get(obj).toString());
        height = context.getResources().getDimensionPixelSize(temp);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return height;
}

From source file:Main.java

public static Object newCollection(Class<?> claxx) throws IllegalAccessException, InstantiationException {
    return claxx.newInstance();
}

From source file:Main.java

@Nullable
@SuppressWarnings("unchecked")
public static <T> T newInstance(@NonNull String className) {
    try {//from www  . jav a 2 s  .co m
        Class<? extends T> cls = classForName(className, true);
        return cls != null ? cls.newInstance() : null;
    } catch (Exception e) {
        throw new RuntimeException(
                "An exception occurred while creating a new instance of " + className + ". " + e.getMessage());
    }
}

From source file:gov.nih.nci.ncicb.cadsr.common.util.ObjectFactory.java

public static Object createObect(String className) throws Exception {
    if (log.isDebugEnabled())
        log.debug("Instatiating Object = " + className);
    Class theClass = forName(new ObjectFactory().getClass(), className);
    return theClass.newInstance();

}

From source file:Main.java

/**
 * Copy fields from parent object to new child object.
 *
 * @param parent parent object//w  ww.  ja  v a  2 s  .  com
 * @param childClass child class
 * @param <T> child class
 * @return filled child object
 */
public static <T> T shallowCopy(Object parent, Class<T> childClass) {
    try {
        return shallowCopy(parent, childClass.newInstance());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

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

        NodeList nl = document.getElementsByTagName("StrategyClassName");
        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();

        System.out.println(cName);
        Class c = Class.forName("com.seven.strategy.sort." + cName);
        Object object = c.newInstance();
        return object;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object getBean() {
    try {//from w ww.  jav a2s  . c om
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document document;
        document = builder.parse(new File("config.xml"));

        NodeList nl = document.getElementsByTagName("TemplateClassName");

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

        System.out.println(cName);
        Class c = Class.forName("com.seven.templatemethod.one." + cName);
        Object object = c.newInstance();
        return object;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * //from ww w  .j a v  a  2  s.c om
 * @param node
 * @param classname
 * @return
 */
public static Object decode(Element node, String classname) {
    try {
        Class classObject = Class.forName(classname);
        Object object = classObject.newInstance();
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node child = attributes.item(i);
            String nodeName = child.getNodeName();
            Field field = classObject.getField(nodeName);
            field.setAccessible(true);
            field.set(object, child.getNodeValue());
        }
        return object;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (InstantiationException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    } catch (SecurityException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:de.micromata.genome.gwiki.utils.ScriptUtils.java

public static Object getScriptObject(String code) {
    GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
    try {//from  w  ww.  jav  a 2  s .  co  m
        Class<?> cls = loader.parseClass(code);
        Object obj = cls.newInstance();
        return obj;
    } catch (Throwable ex) {
        throw new RuntimeException("Cannot execute script Method: " + code + "; " + ex.getMessage(), ex);
    }
}

From source file:ca.travelagency.components.formheader.DaoEntityModel.java

@SuppressWarnings("unchecked")
private static <T> T createNewDaoEntity(Class<?> clazz) {
    try {//from   w  w w .j av a 2  s .  co  m
        return (T) clazz.newInstance();
    } catch (Exception e) {
        throw new RuntimeException("Can not create object of class: " + clazz.getName(), e);
    }
}