Example usage for org.springframework.beans PropertyAccessor isReadableProperty

List of usage examples for org.springframework.beans PropertyAccessor isReadableProperty

Introduction

In this page you can find the example usage for org.springframework.beans PropertyAccessor isReadableProperty.

Prototype

boolean isReadableProperty(String propertyName);

Source Link

Document

Determine whether the specified property is readable.

Usage

From source file:net.solarnetwork.central.dras.domain.CapableObject.java

@Override
@SerializeIgnore// ww  w . j  a v  a2  s.com
public String getName() {
    if (object == null) {
        return null;
    }
    PropertyAccessor bean = PropertyAccessorFactory.forBeanPropertyAccess(object);
    if (bean.isReadableProperty("name")) {
        Object o = bean.getPropertyValue("name");
        if (o != null) {
            return o.toString();
        }
    }
    return null;
}

From source file:net.solarnetwork.util.DynamicServiceTracker.java

private boolean serviceMatchesFilters(Object service) {
    if (service == null) {
        return false;
    }// ww  w.java2s .co  m
    if (propertyFilters == null || propertyFilters.size() < 1) {
        log.debug("No property filter configured, {} service matches", serviceClassName);
        return true;
    }
    log.trace("Examining service {} for property match {}", service, propertyFilters);
    PropertyAccessor accessor = PropertyAccessorFactory.forBeanPropertyAccess(service);
    for (Map.Entry<String, Object> me : propertyFilters.entrySet()) {
        if (accessor.isReadableProperty(me.getKey())) {
            Object requiredValue = me.getValue();
            if (ignoreEmptyPropertyFilterValues && (requiredValue == null
                    || ((requiredValue instanceof String) && ((String) requiredValue).length() == 0))) {
                // ignore empty filter values, so this is a matching property... skip to the next filter
                continue;
            }
            Object serviceValue = accessor.getPropertyValue(me.getKey());
            if (requiredValue == null) {
                if (serviceValue == null) {
                    continue;
                }
                return false;
            }
            if (serviceValue instanceof Collection<?>) {
                // for collections, we test for containment
                Collection<?> collection = (Collection<?>) serviceValue;
                if (!collection.contains(requiredValue)) {
                    return false;
                }
            } else if (!requiredValue.equals(serviceValue)) {
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
}

From source file:net.solarnetwork.node.rfxcom.RFXCOMTransceiver.java

/**
 * Update the settings of this class./*  www  .  j ava2  s .  co m*/
 * 
 * <p>This method is designed to work with Spring's bean-managed OSGi Configuration
 * Admin service, rather than the container-managed approach of setting properties
 * directly. This is because many of the supported properties require communicating
 * with the RFXCOM device, but those can all be set via a single call. Thus the
 * supported properties of this method are those properties directly available on
 * this class itself, and those available on the {@link SetModeMessage} class.
 * 
 * @param properties the properties to change
 */
public void updateConfiguration(Map<String, ?> properties) {
    Map<String, Object> setModeProperties = new HashMap<String, Object>(properties);
    PropertyAccessor bean = PropertyAccessorFactory.forBeanPropertyAccess(this);

    // if this is NOT something that must be handled via a SetMode command, apply those directly...
    for (Map.Entry<String, ?> me : properties.entrySet()) {
        if (bean.isWritableProperty(me.getKey())) {
            bean.setPropertyValue(me.getKey(), me.getValue());
        } else {
            setModeProperties.put(me.getKey(), me.getValue());
        }
    }

    // and now apply remaining properties via single SetMode, so we only have to talk to
    // device one time

    if (this.status == null) {
        updateStatus();
    }
    if (this.status != null) {
        SetModeMessage msg = new SetModeMessage(mf.incrementAndGetSequenceNumber(), this.status);
        bean = PropertyAccessorFactory.forBeanPropertyAccess(msg);
        boolean changed = false;
        for (Map.Entry<String, Object> me : setModeProperties.entrySet()) {
            if (bean.isReadableProperty(me.getKey())) {
                Object currValue = bean.getPropertyValue(me.getKey());
                if (me.getValue() != null && me.getValue().equals(currValue)) {
                    continue;
                }
            }
            if (bean.isWritableProperty(me.getKey())) {
                bean.setPropertyValue(me.getKey(), me.getValue());
                changed = true;
            }
        }
        if (changed) {
            log.debug("Updating RFXCOM settings to {}", msg);
            setMode(msg);
        }
    }

}

From source file:org.jdal.ui.ViewSupport.java

/**
 * Bind controls following the same name convention or annotated with Property annotation.
 *//*from   w  ww .j a v  a  2 s  . c  o  m*/
public void autobind() {
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(getModel());
    PropertyAccessor viewPropertyAccessor = new DirectFieldAccessor(this);

    // Parse Property annotations
    List<AnnotatedElement> elements = AnnotatedElementAccessor.findAnnotatedElements(Property.class,
            getClass());

    for (AnnotatedElement ae : elements) {
        Property p = ae.getAnnotation(Property.class);
        InitializationConfig config = getInitializationConfig(ae.getAnnotation(Initializer.class));
        bindAndInitializeControl(p.value(), viewPropertyAccessor.getPropertyValue(((Field) ae).getName()),
                config);
        this.ignoredProperties.add(p.value());
    }

    // Iterate on model properties
    for (PropertyDescriptor pd : bw.getPropertyDescriptors()) {
        String propertyName = pd.getName();
        if (!ignoredProperties.contains(propertyName)
                && viewPropertyAccessor.isReadableProperty(propertyName)) {
            Object control = viewPropertyAccessor.getPropertyValue(propertyName);

            if (control != null) {
                if (log.isDebugEnabled())
                    log.debug("Found control: " + control.getClass().getSimpleName() + " for property: "
                            + propertyName);
                TypeDescriptor descriptor = viewPropertyAccessor.getPropertyTypeDescriptor(propertyName);
                InitializationConfig config = getInitializationConfig(
                        descriptor.getAnnotation(Initializer.class));
                // do bind
                bindAndInitializeControl(propertyName, control, config);
            }
        }
    }
}

From source file:com.nestedbird.modules.formparser.FormParse.java

/**
 * Can the field be written to and read from?
 * This is according to the springs accessor, if it has no setter or getter it still can somehow be read to...
 * apparently// w  w w  .  j  ava2 s.  c  o  m
 *
 * @param accessor  - The spring propertyAccessor          -
 * @param fieldName - The fieldName
 * @return boolean boolean
 */
private Boolean isFieldInteractable(final PropertyAccessor accessor, final String fieldName) {
    return accessor.isReadableProperty(fieldName) && accessor.isWritableProperty(fieldName);
}

From source file:org.springframework.flex.core.io.SpringPropertyProxy.java

/**
 * {@inheritDoc}//from   ww w . j av a 2s .c om
 */
@Override
public boolean isWriteOnly(Object instance, String propertyName) {
    PropertyAccessor accessor = PropertyProxyUtils.getPropertyAccessor(this.conversionService,
            this.useDirectFieldAccess, instance);
    return isReadIgnored(instance, propertyName)
            || (!accessor.isReadableProperty(propertyName) && accessor.isWritableProperty(propertyName));
}

From source file:org.springframework.flex.core.io.SpringPropertyProxy.java

private boolean isReadIgnored(Object instance, String propertyName) {
    PropertyAccessor accessor = PropertyProxyUtils.getPropertyAccessor(this.conversionService,
            this.useDirectFieldAccess, instance);
    if (!accessor.isReadableProperty(propertyName)) {
        return true;
    }/*w  ww  .  j  a v  a  2  s.com*/
    if (this.useDirectFieldAccess) {
        AmfIgnoreField ignoreField = accessor.getPropertyTypeDescriptor(propertyName)
                .getAnnotation(AmfIgnoreField.class);
        return ignoreField != null && ignoreField.onSerialization();
    } else {
        PropertyDescriptor pd = ((BeanWrapper) accessor).getPropertyDescriptor(propertyName);
        return pd.getReadMethod().getAnnotation(AmfIgnore.class) != null;
    }
}