Example usage for java.beans PropertyDescriptor getReadMethod

List of usage examples for java.beans PropertyDescriptor getReadMethod

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getReadMethod.

Prototype

public synchronized Method getReadMethod() 

Source Link

Document

Gets the method that should be used to read the property value.

Usage

From source file:com.bradmcevoy.property.BeanPropertySource.java

@Override
public PropertyMetaData getPropertyMetaData(QName name, Resource r) {
    log.debug("getPropertyMetaData");
    BeanPropertyResource anno = getAnnotation(r);
    if (anno == null) {
        log.debug(" no annotation: ", r.getClass().getCanonicalName());
        return PropertyMetaData.UNKNOWN;
    }//  w  w w .  j a v a  2 s. c o  m
    if (!name.getNamespaceURI().equals(anno.value())) {
        log.debug("different namespace", anno.value(), name.getNamespaceURI());
        return PropertyMetaData.UNKNOWN;
    }

    PropertyDescriptor pd = getPropertyDescriptor(r, name.getLocalPart());
    if (pd == null || pd.getReadMethod() == null) {
        LogUtils.debug(log, "getPropertyMetaData: no read method:", name.getLocalPart(), r.getClass());
        return PropertyMetaData.UNKNOWN;
    } else {
        BeanPropertyAccess propAnno = pd.getReadMethod().getAnnotation(BeanPropertyAccess.class);
        if (propAnno != null) {
            if (!propAnno.value()) {
                log.trace(
                        "getPropertyMetaData: property is annotated and value is false, so do not allow access");
                return PropertyMetaData.UNKNOWN;
            } else {
                log.trace("getPropertyMetaData: property is annotated and value is true, so allow access");
            }
        } else {
            if (anno.enableByDefault()) {
                log.trace(
                        "getPropertyMetaData: no property annotation, property annotation is enable by default so allow access");
            } else {
                log.trace(
                        "getPropertyMetaData:no property annotation, class annotation says disable by default, decline access");
                return PropertyMetaData.UNKNOWN;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("writable: " + anno.writable() + " - " + (pd.getWriteMethod() != null));
        }
        boolean writable = anno.writable() && (pd.getWriteMethod() != null);
        if (writable) {
            return new PropertyMetaData(PropertyAccessibility.WRITABLE, pd.getPropertyType());
        } else {
            return new PropertyMetaData(PropertyAccessibility.READ_ONLY, pd.getPropertyType());
        }
    }
}

From source file:org.jdal.ui.bind.AbstractBinder.java

/**
 * @return Property for property binder//from ww w  . j  a v a 2 s  . co m
 */
protected Property getProperty() {
    PropertyDescriptor pd = getPropertyDescriptor();
    return new Property(getModel().getClass(), pd.getReadMethod(), pd.getWriteMethod());
}

From source file:org.echocat.redprecursor.annotations.utils.AccessAlsoProtectedMembersReflectivePropertyAccessor.java

@Override
protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
    Method result = null;//from w  w  w . j a  v  a2 s.co  m
    final PropertyDescriptor propertyDescriptor = findPropertyDescriptorFor(clazz, propertyName);
    if (propertyDescriptor != null) {
        result = propertyDescriptor.getReadMethod();
    }
    if (result == null) {
        Class<?> current = clazz;
        final String getterName = "get" + StringUtils.capitalize(propertyName);
        while (result == null && !Object.class.equals(current)) {
            try {
                final Method potentialMethod = current.getDeclaredMethod(getterName);
                if (!mustBeStatic || Modifier.isStatic(potentialMethod.getModifiers())) {
                    if (!potentialMethod.isAccessible()) {
                        potentialMethod.setAccessible(true);
                    }
                    result = potentialMethod;
                }
            } catch (NoSuchMethodException ignored) {
            }
            current = current.getSuperclass();
        }
    }
    return result;
}

From source file:io.milton.property.BeanPropertySource.java

@Override
public PropertyMetaData getPropertyMetaData(QName name, Resource r) {
    log.debug("getPropertyMetaData");
    BeanPropertyResource anno = getAnnotation(r);
    if (anno == null) {
        log.debug(" no annotation: ", r.getClass().getCanonicalName());
        return PropertyMetaData.UNKNOWN;
    }//from w ww.  j  ava 2 s .  c o  m
    if (!name.getNamespaceURI().equals(anno.value())) {
        log.debug("different namespace", anno.value(), name.getNamespaceURI());
        return PropertyMetaData.UNKNOWN;
    }

    PropertyDescriptor pd = getPropertyDescriptor(r, name.getLocalPart());
    if (pd == null || pd.getReadMethod() == null) {
        LogUtils.debug(log, "getPropertyMetaData: no read method:", name.getLocalPart(), r.getClass());
        return PropertyMetaData.UNKNOWN;
    } else {
        BeanProperty propAnno = pd.getReadMethod().getAnnotation(BeanProperty.class);
        if (propAnno != null) {
            if (!propAnno.value()) {
                log.trace(
                        "getPropertyMetaData: property is annotated and value is false, so do not allow access");
                return PropertyMetaData.UNKNOWN;
            } else {
                log.trace("getPropertyMetaData: property is annotated and value is true, so allow access");
            }
        } else {
            if (anno.enableByDefault()) {
                log.trace(
                        "getPropertyMetaData: no property annotation, property annotation is enable by default so allow access");
            } else {
                log.trace(
                        "getPropertyMetaData:no property annotation, class annotation says disable by default, decline access");
                return PropertyMetaData.UNKNOWN;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("writable: " + anno.writable() + " - " + (pd.getWriteMethod() != null));
        }
        boolean writable = anno.writable() && (pd.getWriteMethod() != null);
        if (writable) {
            return new PropertyMetaData(PropertyAccessibility.WRITABLE, pd.getPropertyType());
        } else {
            return new PropertyMetaData(PropertyAccessibility.READ_ONLY, pd.getPropertyType());
        }
    }
}

From source file:org.openlmis.fulfillment.service.FulfillmentNotificationService.java

private String getContent(Object object, String messageKey, Map<String, String> messageParams) {
    String content = messageService.localize(new Message(messageKey)).getMessage();

    try {// w  w w  .  j  a  v a  2 s .c  om
        List<PropertyDescriptor> descriptors = Arrays.stream(getPropertyDescriptors(object.getClass()))
                .filter(d -> null != d.getReadMethod()).collect(Collectors.toList());

        for (PropertyDescriptor descriptor : descriptors) {
            String target = "{" + descriptor.getName() + "}";
            String replacement = String.valueOf(descriptor.getReadMethod().invoke(object));

            content = content.replace(target, replacement);
        }

        for (Map.Entry<String, String> entry : messageParams.entrySet()) {
            String target = "{" + entry.getKey() + "}";
            String replacement = entry.getValue();

            content = content.replace(target, replacement);
        }

    } catch (IllegalAccessException | InvocationTargetException exp) {
        throw new IllegalStateException("Can't get access to getter method", exp);
    }
    return content;
}

From source file:com.searchbox.core.search.AbstractSearchCondition.java

@Override
public boolean equals(Object other) {
    if (!this.getClass().equals(other.getClass())) {
        return false;
    } else {//w  ww.j a  v  a2s  .c  o  m
        try {
            for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(this.getClass())
                    .getPropertyDescriptors()) {

                Method method = propertyDescriptor.getReadMethod();
                Object res1 = method.invoke(this);
                Object res2 = method.invoke(other);

                if ((res1 == null && res2 != null) || (res1 != null && res2 == null)) {
                    return false;
                }
                if (res1 != null && res2 != null && !res1.equals(res2)) {
                    return false;
                }
            }
        } catch (Exception e) {
            LOGGER.error("Could not compare Objects", e);
            return false;
        }
    }
    return true;
}

From source file:com.ebay.jetstream.management.HtmlResourceFormatter.java

protected void formatProperty(Object bean, PropertyDescriptor pd) throws Exception {
    PrintWriter pw = getWriter();
    Method getter = pd.getReadMethod();
    Class<?> pclass = pd.getPropertyType();
    ManagedAttribute attr = getter.getAnnotation(ManagedAttribute.class);
    String text = attr != null ? attr.description() : null;
    if (CommonUtils.isEmptyTrimmed(text)) {
        text = pd.getDisplayName();/*from   w  w w . j a va  2  s  . c om*/
    } else {
        text = pd.getDisplayName() + " (" + text + ")";
    }
    pw.print(text + ": " + pclass.getName() + " = ");
    getter.setAccessible(true);
    Object value = getter.invoke(bean);
    Method setter = pd.getWriteMethod();
    attr = setter == null ? null : setter.getAnnotation(ManagedAttribute.class);
    boolean isComplex = !(String.class.isAssignableFrom(pclass) || ClassUtils.isPrimitiveOrWrapper(pclass));
    if (isComplex) {
        value = StringEscapeUtils.escapeXml(getSerializer().getXMLStringRepresentation(value));
    }
    if (attr == null) {
        if (isComplex) {
            pushElement("code", null);
        }
        pw.println(value);
        if (isComplex) {
            popElement();
        }
    } else {
        pw.println(attr.description());
        pushElement("form",
                "action=" + makePath(getPrefix(), getPath(), isComplex ? "?form" : "?" + pd.getName())
                        + " method=" + (isComplex ? "POST" : "GET"));
        if (isComplex) {
            pw.print("<TEXTAREA name=" + pd.getName() + " rows=4 cols=32>" + value + "</TEXTAREA>");
        } else {
            pw.print("<input type=text name=" + pd.getName() + " value=\"" + value + "\"/>");
        }
        pw.println("<input type=submit Value=\"Go\"/>");
        popElement();
    }
    pw.println("<P/>");
}

From source file:at.molindo.notify.model.BeanParams.java

@Override
public Iterator<ParamValue> iterator() {
    return new Iterator<ParamValue>() {

        private final Iterator<PropertyDescriptor> _iter = getDescriptorsIter();
        private ParamValue _next = findNext();

        private ParamValue findNext() {
            while (_iter.hasNext()) {
                PropertyDescriptor pd = _iter.next();
                if (pd.getWriteMethod() == null || pd.getReadMethod() == null) {
                    continue;
                }//from w  w  w .  j  a  v a2  s .c o m
                Object value = invoke(pd.getReadMethod());
                if (value == null) {
                    continue;
                }
                return Param.p(pd.getPropertyType(), pd.getName()).paramValue(value);
            }
            return null;
        }

        @Override
        public boolean hasNext() {
            return _next != null;
        }

        @Override
        public ParamValue next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            ParamValue next = _next;
            _next = findNext();
            return next;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:com.ebay.jetstream.management.HtmlResourceFormatter.java

@Override
protected final void formatBean(Object bean) throws Exception {
    Class<?> bclass = bean.getClass();
    ManagedResource mr = bclass.getAnnotation(ManagedResource.class);
    String help = mr == null ? null : mr.description();
    formatBean(false, bclass, help);/*w  ww  .  j  a va2 s . co  m*/
    boolean section = false;
    for (PropertyDescriptor pd : Introspector.getBeanInfo(bclass).getPropertyDescriptors()) {
        Method getter = pd.getReadMethod();
        if (!XMLSerializationManager.isHidden(getter)) {
            if (!section) {
                section = true;
                formatSection(false, "Properties");
            }
            formatProperty(bean, pd);
        }
    }
    if (section) {
        formatSection(true, "Properties");
    }
    section = false;
    for (Method method : bean.getClass().getMethods()) {
        ManagedOperation mo = method.getAnnotation(ManagedOperation.class);
        if (mo != null && method.getParameterTypes().length == 0) {
            help = mo.description();
            if (!section) {
                section = true;
                formatSection(false, "Operations");
            }
            formatOperation(method);
        }
        if (section) {
            formatSection(true, "Operations");
        }
    }
}

From source file:org.carewebframework.ui.xml.ZK2XML.java

/**
 * Adds the root component to the XML document at the current level along with all bean
 * properties that return String or primitive types. Then, recurses over all of the root
 * component's children./*from  w  w w.  j  a  v  a  2 s.co m*/
 * 
 * @param root The root component.
 * @param parent The parent XML node.
 */
private void toXML(Component root, Node parent) {
    TreeMap<String, String> properties = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    Class<?> clazz = root.getClass();
    ComponentDefinition def = root.getDefinition();
    String cmpname = def.getName();

    if (def.getImplementationClass() != clazz) {
        properties.put("use", clazz.getName());
    }

    if (def.getApply() != null) {
        properties.put("apply", def.getApply());
    }

    Node child = doc.createElement(cmpname);
    parent.appendChild(child);

    for (PropertyDescriptor propDx : PropertyUtils.getPropertyDescriptors(root)) {
        Method getter = propDx.getReadMethod();
        Method setter = propDx.getWriteMethod();
        String name = propDx.getName();

        if (getter != null && setter != null && !isExcluded(name, cmpname, null)
                && !setter.isAnnotationPresent(Deprecated.class)
                && (getter.getReturnType() == String.class || getter.getReturnType().isPrimitive())) {
            try {
                Object raw = getter.invoke(root);
                String value = raw == null ? null : raw.toString();

                if (StringUtils.isEmpty(value) || ("id".equals(name) && value.startsWith("z_"))
                        || isExcluded(name, cmpname, value)) {
                    continue;
                }

                properties.put(name, value.toString());
            } catch (Exception e) {
            }
        }
    }

    for (Entry<String, String> entry : properties.entrySet()) {
        Attr attr = doc.createAttribute(entry.getKey());
        child.getAttributes().setNamedItem(attr);
        attr.setValue(entry.getValue());
    }

    properties = null;

    for (Component cmp : root.getChildren()) {
        toXML(cmp, child);
    }
}