Example usage for org.apache.commons.beanutils BeanMap getWriteMethod

List of usage examples for org.apache.commons.beanutils BeanMap getWriteMethod

Introduction

In this page you can find the example usage for org.apache.commons.beanutils BeanMap getWriteMethod.

Prototype

protected Method getWriteMethod(Object name) 

Source Link

Document

Returns the mutator for the property with the given name.

Usage

From source file:org.sindice.rdfcommons.beanmapper.BeanDeserializer.java

@SuppressWarnings("unchecked")
public <T> T deserialize(DeserializationContext context, Class<T> clazz, Annotation[] annotations,
        Identifier identifier, QueryEndpoint endPoint) throws DeserializationException {

    final T bean;
    try {/*from   w  ww .j a va  2  s .com*/
        bean = clazz.newInstance();
    } catch (Exception e) {
        throw new DeserializationException("Error while instantiating object.", e);
    }
    context.registerInstance(identifier, bean);

    final String classUrl = getClassURL(bean.getClass());
    final BeanMap beanMap = new BeanMap(bean);
    String propertyURL;
    for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) {

        // Skipping self description.
        if ("class".equals(entry.getKey())) {
            continue;
        }

        final String propertyName = entry.getKey();
        final Method propertyWriteMethod = beanMap.getWriteMethod(propertyName);
        if (propertyWriteMethod == null) {
            throw new DeserializationException(
                    String.format("Cannot find setter for property '%s' in bean %s", propertyName, bean));
        }
        final Class propertyType = propertyWriteMethod.getParameterTypes()[0];

        // Skipping properties marked as ignored.
        if (isIgnored(propertyWriteMethod)) {
            continue;
        }

        final Object value = retrieveObject(classUrl, propertyName, propertyWriteMethod, identifier, endPoint,
                context);

        // Skipping missing values.
        if (value == null) {
            continue;
        }

        Object deserializedValue = context.deserialize(context, propertyType,
                propertyWriteMethod.getAnnotations(), new Identifier(value, Identifier.Type.resource),
                endPoint);

        try {
            propertyWriteMethod.invoke(bean, deserializedValue);
        } catch (Exception e) {
            throw new DeserializationException(
                    String.format("Error while setting value [%s] on bean [%s] using setter [%s].", value, bean,
                            propertyWriteMethod),
                    e);
        }
    }

    return bean;
}

From source file:org.sindice.rdfcommons.beanmapper.StaticBeanDeserializer.java

public <T> T deserialize(DeserializationContext context, Class<T> clazz, Annotation[] annotations,
        Identifier identifier, QueryEndpoint endPoint) throws DeserializationException {

    if (identifier != null) {
        throw new IllegalArgumentException("for static deserialization the identifier is expected to be null.");
    }/*from  ww w.jav a2s .co  m*/

    final Identifier staticIdentifier = getIdentifier(clazz, annotations);

    // Create the bean instance.
    final T instance;
    try {
        instance = clazz.newInstance();
    } catch (Exception e) {
        throw new DeserializationException(String
                .format("Error while creating instance of class %s: defualt constructor required.", clazz));
    }
    context.registerInstance(staticIdentifier, instance);

    // Define the bean map.
    final BeanMap beanMap = new BeanMap(instance);

    // Extracts the class property URLs.
    final Map<String, Method> propertyURLs = new HashMap<String, Method>();
    final String classURL = getClassURL(clazz);
    String propertyName;
    Method propertyWriteMethod;
    for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) {
        // Skipping self description.
        if ("class".equals(entry.getKey())) {
            continue;
        }
        propertyName = entry.getKey();
        propertyWriteMethod = beanMap.getWriteMethod(propertyName);
        propertyURLs.put(getPropertyURL(classURL, propertyName, propertyWriteMethod), propertyWriteMethod);
    }

    // Retrieve the class triples.
    endPoint.addQuery((String) staticIdentifier.getId(), "?prop", "?value");
    ResultSet rs = endPoint.execute();

    // Coupling object properties with actual ones.
    Object property;
    Object value;
    Class propertyType;
    while (rs.hasNext()) {
        property = rs.getVariableValue("?prop");
        value = rs.getVariableValue("?value");
        rs.next();

        propertyWriteMethod = propertyURLs.get(property);
        if (propertyWriteMethod == null) {
            context.reportIssue(String.format("Cannot find write method for property URL %s", property));
            continue;
        }
        propertyType = propertyWriteMethod.getParameterTypes()[0];

        // Deserializing recursively.
        Object deserialized = context.deserialize(context, propertyType, propertyType.getAnnotations(),
                new Identifier(value, Identifier.Type.resource), endPoint);

        try {
            propertyWriteMethod.invoke(instance, deserialized);
        } catch (Exception e) {
            throw new DeserializationException(
                    String.format("Error while invoking write method %s of instance %s on value %s[%s]",
                            propertyWriteMethod, instance, deserialized, deserialized.getClass()));
        }
    }
    return instance;
}