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:de.dmxcontrol.device.Entity.java

public static void SendRequest(Class entity, String request) {
    try {//from   w  w w .j  a va  2s . co  m
        String type = ((Entity) entity.newInstance()).getNetworkID();
        if (request.equals(Request_All_GUIDs)) {
            type += Request_All_GUIDs;
            request = Request_All;
        }
        JSONObject o = new JSONObject();
        o.put("Type", type);
        o.put("Request", request);

        ServiceFrontend.get().sendMessage(o.toString().getBytes());
        type = null;
        o = null;
        request = null;
        if (type == null && o == null && request == null) {
            ;
        }
    } catch (Exception e) {
        Log.e("SendAllRequest: ", e.getMessage());
        DMXControlApplication.SaveLog();
    }
}

From source file:Main.java

public static <T> T loadBean(Node node, Class<T> beanClass)
        throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        DOMException, InvocationTargetException {
    T store = beanClass.newInstance();

    Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
        properties.put(property.getName(), property);
    }//from   ww w .  j av  a  2  s.c o  m

    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        PropertyDescriptor property = properties.get(attribute.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, attribute.getNodeValue());
        }
    }

    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        PropertyDescriptor property = properties.get(child.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, child.getTextContent());
        }
    }

    return store;
}

From source file:biomine.bmvis2.pipeline.GraphOperationSerializer.java

public static GraphOperation fromJSON(JSONObject o) throws GraphOperationSerializationException {

    try {/*w  w w.ja  v  a 2 s . co m*/
        String className = o.get("class").toString();

        Class cl = GraphOperationSerializer.class.getClassLoader().loadClass(className);

        GraphOperation ret = (GraphOperation) cl.newInstance();
        JSONObject retJSON = (JSONObject) o.get("object");
        ret.fromJSON(retJSON);
        return ret;
    } catch (Exception e) {
        throw new GraphOperationSerializationException(e);
    }
}

From source file:Main.java

public static Node applyXslToDocument2(Source xslt, Source doc, URIResolver resolver,
        Properties transformerProperties, HashMap<String, String> params, String transformerClassName)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException,
        NoSuchMethodException, TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = null;
    if (transformerClassName == null)
        transformerFactory = TransformerFactory.newInstance();
    else {/* w  w  w  . jav  a  2s  .  c  o  m*/
        Class transformerClass = Class.forName(transformerClassName);
        Constructor defaultConstructor = transformerClass.getConstructor(null);
        transformerFactory = (TransformerFactory) transformerClass.newInstance();
    }

    if (resolver != null)
        transformerFactory.setURIResolver(resolver);

    Transformer transformer = transformerFactory.newTransformer(xslt);
    if (transformerFactory != null)
        transformer.setOutputProperties(transformerProperties);

    if (params != null) {
        for (Map.Entry<String, String> cursor : params.entrySet()) {
            transformer.setParameter(cursor.getKey(), cursor.getValue());
        }
    }

    DOMResult result = new DOMResult();
    transformer.transform(doc, result);

    return (result.getNode());
}

From source file:mondrian.spi.DataServicesLocator.java

public static DataServicesProvider getDataServicesProvider(final String className) {
    if (Util.isEmpty(className)) {
        return new DefaultDataServicesProvider();
    } else {//  w w  w . j av a2 s  .  c  o m
        ServiceDiscovery<DataServicesProvider> discovery = ServiceDiscovery
                .forClass(DataServicesProvider.class);
        List<Class<DataServicesProvider>> implementors = discovery.getImplementor();
        Predicate providerNamePredicate = new Predicate() {
            public boolean evaluate(Object o) {
                Class<DataServicesProvider> providerClass = (Class<DataServicesProvider>) o;
                return providerClass.getName().equals(className);
            }
        };
        Class<DataServicesProvider> provider = (Class<DataServicesProvider>) find(implementors,
                providerNamePredicate);
        if (provider != null) {
            try {
                return provider.newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        throw new RuntimeException("Unrecognized Service Provider: " + className);
    }
}

From source file:org.openmrs.calculation.CalculationUtil.java

/**
 * Utility method that constructs a Calculation instance from a provider Name, calculation name,
 * and configuration string/*w w w .  j  a v  a 2  s.  co m*/
 * 
 * @return the Calculation represented by the passed parameters
 * @throws InvalidCalculationException if there is no valid Calculation matching the parameters
 */
public static Calculation getCalculation(String providerClassName, String calculationName, String configuration)
        throws InvalidCalculationException {
    CalculationProvider calculationProvider = null;
    try {
        Class<?> providerClass = Context.loadClass(providerClassName);
        calculationProvider = (CalculationProvider) providerClass.newInstance();
    } catch (Exception e) {
        String msg = "Unable to instantiate CalculationProvider:" + providerClassName;
        throw new InvalidCalculationException(msg, e);
    }
    return calculationProvider.getCalculation(calculationName, configuration);
}

From source file:Main.java

public static String applyXslToDocument(Source xslt, Source doc, URIResolver resolver,
        Properties transformerProperties, HashMap<String, String> params, String transformerClassName)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException,
        NoSuchMethodException, TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = null;
    if (transformerClassName == null)
        transformerFactory = TransformerFactory.newInstance();
    else {// ww  w.j av a  2s.co m
        Class transformerClass = Class.forName(transformerClassName);
        Constructor defaultConstructor = transformerClass.getConstructor(null);
        transformerFactory = (TransformerFactory) transformerClass.newInstance();
    }

    if (resolver != null)
        transformerFactory.setURIResolver(resolver);

    Transformer transformer = transformerFactory.newTransformer(xslt);
    if (transformerFactory != null)
        transformer.setOutputProperties(transformerProperties);

    if (params != null) {
        for (Map.Entry<String, String> cursor : params.entrySet()) {
            transformer.setParameter(cursor.getKey(), cursor.getValue());
        }
    }

    StringWriter strWriter = new StringWriter();
    StreamResult result = new StreamResult(strWriter);
    transformer.transform(doc, result);

    return (strWriter.toString());
}

From source file:ch.cyclops.publish.ParseQueryResult.java

public static List map(List<Map> list, Class clazz) {
    List<Object> mapped = new ArrayList<>();

    // iterate and map those objects
    if (list != null) {
        for (Map map : list) {
            try {
                Object bean = clazz.newInstance();

                // map HashMap to POJO
                BeanUtils.populate(bean, map);

                // add it to list of mapped CDRs
                mapped.add(bean);/* w ww  . j  a va  2 s  .  com*/

            } catch (Exception ignored) {
                return null;
            }
        }
    }

    return mapped;
}

From source file:ch.icclab.cyclops.util.BeanList.java

public static List populate(List<Map> list, Class clazz) {
    List<Object> mapped = new ArrayList<>();

    // iterate and map those objects
    if (list != null) {
        for (Map map : list) {
            try {
                Object bean = clazz.newInstance();

                // map HashMap to POJO
                BeanUtils.populate(bean, map);

                // add it to list of mapped CDRs
                mapped.add(bean);//from www . ja v  a  2 s .com

            } catch (Exception ignored) {
                return null;
            }
        }
    }

    return mapped;
}

From source file:com.opengamma.util.rest.RestUtils.java

/**
 * Decodes an object from base-64 such as when passed in the URI.
 * <p>//from ww w .  j  a  v  a 2 s.co  m
 * The conversion uses Fudge, thus the object must be convertible to/from Fudge.
 * 
 * @param <T> the bean type
 * @param type  the bean type to build, not null
 * @param msgBase64  the base-64 Fudge message, not null
 * @return the bean, not null
 */
public static <T> T decodeBase64(final Class<T> type, final String msgBase64) {
    if (msgBase64 == null) {
        try {
            return type.newInstance();
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        }
    }
    byte[] msg = Base64.decodeBase64(msgBase64);
    FudgeContext context = OpenGammaFudgeContext.getInstance();
    FudgeMsg message = context.createMessageReader(new ByteArrayInputStream(msg)).nextMessage();
    if (message == null) {
        return null;
    }
    FudgeDeserializer deser = new FudgeDeserializer(context);
    return deser.fudgeMsgToObject(type, message);
}