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 <ValueType, BoundType> XmlAdapter<ValueType, BoundType> getXmlAdapter(
        Class<? extends XmlAdapter<ValueType, BoundType>> xmlAdapterClass) {
    try {/*from w  w  w . j a  va 2 s  .c  o m*/
        final XmlAdapter<ValueType, BoundType> xmlAdapter = xmlAdapterClass.newInstance();
        return xmlAdapter;
    } catch (IllegalAccessException iaex) {
        throw new RuntimeException(iaex);
    } catch (InstantiationException iex) {
        throw new RuntimeException(iex);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Activity context) {
    int statusHeight = 0;
    Rect frame = new Rect();
    context.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    statusHeight = frame.top;/*from  w w w .j ava2 s  .  com*/
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = context.getResources().getDimensionPixelSize(i5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:com.google.code.activetemplates.impl.handlers.BuiltinHandlerSPI.java

private static final void addElements(String pkgName) throws Exception {
    Set<Class<ElementHandler>> classes = getClasses(pkgName, ElementHandler.class);
    for (Class<ElementHandler> cl : classes) {
        ElementHandler eh = cl.newInstance();
        for (QName qn : eh.getElements()) {
            elements.put(qn, eh);/* ww  w . j a  v a2s  .  c  o  m*/
        }
    }
}

From source file:dk.statsbiblioteket.doms.domsutil.surveyable.SurveyableFactory.java

/**
 * Create a surveyable instance. This will create an instance of a
 * surveyable with the given classname, or throw an exception on any trouble
 * doing so.//  ww w. ja v a2s.  c o  m
 *
 * @param implementation The classname of the implementation to initialize
 * @return Surveyable instance.
 *
 * @throws SurveyableInstantiationException on trouble instantiating the
 * surveyable.
 */
public static Surveyable createSurveyable(String implementation) {
    log.trace("Enter createSurveyable('" + implementation + "')");
    try {
        Class surveyableClass = Class.forName(implementation);
        return (Surveyable) surveyableClass.newInstance();
    } catch (Exception e) {
        throw new SurveyableInstantiationException(
                "Cannot instantiate Surveyable class '" + implementation + "': " + e.getMessage(), e);
    }
}

From source file:Main.java

/**
 * Instantiates the elements of an array. This is useful because some Java
 * methods set arrays by reference and require the array's elements to be
 * instantiated.//ww w.ja v  a  2  s .co  m
 * 
 * @param elementClass
 *            The class of the array to be instantiated.
 * @param array
 *            The array to be instantiated.
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 */
public static <T> void instantiateArray(Class<T> elementClass, T[] array)
        throws InstantiationException, IllegalAccessException {
    for (int i = 0; i < array.length; i++) {
        array[i] = elementClass.newInstance();
    }
}

From source file:com.google.code.activetemplates.impl.handlers.BuiltinHandlerSPI.java

private static final void addAttributes(String pkgName) throws Exception {
    Set<Class<AttributeHandler>> classes = getClasses(pkgName, AttributeHandler.class);
    for (Class<AttributeHandler> cl : classes) {
        AttributeHandler eh = cl.newInstance();
        for (QName qn : eh.getAttributes()) {
            attributes.put(qn, eh);//from  w ww .  ja v  a2  s . c  o m
        }
    }
}

From source file:com.shelfmap.stepsfinder.CandidateStepsFactory.java

public static List<Object> createStepsInstances(Class<?> embedderClass) {
    List<Object> steps = new ArrayList<Object>();
    for (Class<?> clazz : findStepsClasses(embedderClass)) {
        try {//from w ww.j  a v a  2s .  c  o m
            steps.add(clazz.newInstance());
        } catch (InstantiationException ex) {
            LOGGER.error("Could not instanciate a class: " + clazz.getCanonicalName(), ex);
        } catch (IllegalAccessException ex) {
            LOGGER.error("Could not access ot the constructer of the class: " + clazz.getCanonicalName(), ex);
        }
    }
    return steps;
}

From source file:Main.java

/**
 * @param context//from   ww  w  . j a  v  a2  s .c  om
 * @return
 */
public static int getStatusHeight(Context context) {
    int statusHeight = 0;
    Rect localRect = new Rect();
    ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
    statusHeight = localRect.top;
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = context.getResources().getDimensionPixelSize(i5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:com.prey.ReflectionsUtils.java

public static Object JSONObjectToObject(JSONObject object) {
    Class clazz = ReflectionsUtils.getJSONObjectClass(object);
    try {/* w w w  .  j  a  va  2 s . c om*/
        return clazz.newInstance();
    } catch (IllegalAccessException e) {
        Log.e("Reflections Utils", "[Seguridad] Ocurrio un error al instanciar la clase " + clazz);
    } catch (InstantiationException e) {
        Log.e("Reflections Utils", "[Desconocido] Ocurrio un error al instanciar la clase " + clazz);
    }

    return null;
}

From source file:net.servicefixture.parser.TreeParserFactory.java

/**
 * Creates an <code>TreeParser</code> instance. Looks up system property
 * TreeParserFactory.TREE_PARSER_SYSPROP for the class name first. If not 
 * found, an <code>ExpressionTreeParser</code> instance will be created.
 *  /*from  w w  w . java2  s. c om*/
 */
public static TreeParser createTreeParser(String parserClazzName) {
    if (StringUtils.isEmpty(parserClazzName)) {
        parserClazzName = System.getProperty(TREE_PARSER_SYSPROP);
    }

    try {
        if (!StringUtils.isEmpty(parserClazzName)) {
            Class parserClazz = Thread.currentThread().getContextClassLoader().loadClass(parserClazzName);
            return (TreeParser) parserClazz.newInstance();
        }

        return new ExpressionTreeParser();
    } catch (Exception e) {
        throw new ServiceFixtureException("Unable to create TreeParser using class:" + parserClazzName + ".",
                e);
    }
}