Example usage for java.beans PropertyDescriptor setWriteMethod

List of usage examples for java.beans PropertyDescriptor setWriteMethod

Introduction

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

Prototype

public synchronized void setWriteMethod(Method writeMethod) throws IntrospectionException 

Source Link

Document

Sets the method that should be used to write the property value.

Usage

From source file:org.psnively.scala.beans.ScalaBeanInfo.java

private static void addScalaSetter(Map<String, PropertyDescriptor> propertyDescriptors, Method writeMethod) {
    String propertyName = writeMethod.getName().substring(0,
            writeMethod.getName().length() - SCALA_SETTER_SUFFIX.length());

    PropertyDescriptor pd = propertyDescriptors.get(propertyName);
    if (pd != null && pd.getWriteMethod() == null) {
        try {//  www.j  ava 2s.c  om
            pd.setWriteMethod(writeMethod);
        } catch (IntrospectionException ex) {
            logger.debug("Could not add write method [" + writeMethod + "] for " + "property [" + propertyName
                    + "]: " + ex.getMessage());
        }
    } else if (pd == null) {
        try {
            pd = new PropertyDescriptor(propertyName, null, writeMethod);
            propertyDescriptors.put(propertyName, pd);
        } catch (IntrospectionException ex) {
            logger.debug("Could not create new PropertyDescriptor for " + "writeMethod [" + writeMethod
                    + "] property [" + propertyName + "]: " + ex.getMessage());
        }
    }
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

static PropertyDescriptor[] getInterfacePropertyDescriptors(Class<?> interfaceClass) {
    List<PropertyDescriptor> propDescriptors = new ArrayList<>();
    // Add prop descriptors for interface passed in
    propDescriptors.addAll(Arrays.asList(PropertyUtils.getPropertyDescriptors(interfaceClass)));

    // Look for interface inheritance. If super interfaces are found, recurse up the hierarchy tree and add prop
    // descriptors for each interface found.
    // PropertyUtils.getPropertyDescriptors() does not correctly walk the inheritance hierarchy for interfaces.
    Class<?>[] interfaces = interfaceClass.getInterfaces();
    if (interfaces != null) {
        for (Class<?> superInterfaceClass : interfaces) {
            List<PropertyDescriptor> superInterfacePropertyDescriptors = Arrays
                    .asList(getInterfacePropertyDescriptors(superInterfaceClass));
            /*/* w  w w . j  av a  2  s . co  m*/
             * #1814758
             * Check for existing descriptor with the same name to prevent 2 property descriptors with the same name being added
             * to the result list.  This caused issues when getter and setter of an attribute on different interfaces in
             * an inheritance hierarchy
             */
            for (PropertyDescriptor superPropDescriptor : superInterfacePropertyDescriptors) {
                PropertyDescriptor existingPropDescriptor = findPropDescriptorByName(propDescriptors,
                        superPropDescriptor.getName());
                if (existingPropDescriptor == null) {
                    propDescriptors.add(superPropDescriptor);
                } else {
                    try {
                        if (existingPropDescriptor.getReadMethod() == null) {
                            existingPropDescriptor.setReadMethod(superPropDescriptor.getReadMethod());
                        }
                        if (existingPropDescriptor.getWriteMethod() == null) {
                            existingPropDescriptor.setWriteMethod(superPropDescriptor.getWriteMethod());
                        }
                    } catch (IntrospectionException e) {
                        throw new MappingException(e);
                    }

                }
            }
        }
    }
    return propDescriptors.toArray(new PropertyDescriptor[propDescriptors.size()]);
}

From source file:org.archive.crawler.restlet.JobRelatedResource.java

protected void defaultUpdateDescriptor(PropertyDescriptor pd) {
    // make non-editable
    try {/*w  w  w .  j  a va2s. c  om*/
        pd.setWriteMethod(null);
    } catch (IntrospectionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (HIDDEN_PROPS.contains(pd.getName())) {
        pd.setHidden(true);
    }
}

From source file:net.yasion.common.core.bean.wrapper.ExtendedBeanInfo.java

private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
    int nParams = method.getParameterTypes().length;
    String propertyName = propertyNameFor(method);
    Class<?> propertyType = method.getParameterTypes()[nParams - 1];
    PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
    if (nParams == 1) {
        if (existingPd == null) {
            this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method));
        } else {/*from  www . j av a  2s .  c  o m*/
            existingPd.setWriteMethod(method);
        }
    } else if (nParams == 2) {
        if (existingPd == null) {
            this.propertyDescriptors
                    .add(new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method));
        } else if (existingPd instanceof IndexedPropertyDescriptor) {
            ((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method);
        } else {
            this.propertyDescriptors.remove(existingPd);
            this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(propertyName,
                    existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method));
        }
    } else {
        throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method);
    }
}

From source file:org.apache.activemq.artemis.utils.uri.FluentPropertyBeanIntrospectorWithIgnores.java

@Override
public void introspect(IntrospectionContext icontext) throws IntrospectionException {
    for (Method m : icontext.getTargetClass().getMethods()) {
        if (m.getName().startsWith(getWriteMethodPrefix())) {
            String propertyName = propertyName(m);
            PropertyDescriptor pd = icontext.getPropertyDescriptor(propertyName);

            if (isIgnored(icontext.getTargetClass().getName(), m.getName())) {
                logger.trace(m.getName() + " Ignored for " + icontext.getTargetClass().getName());
                continue;
            }/*from   ww  w . j a v  a  2  s . c  o  m*/
            try {
                if (pd == null) {
                    icontext.addPropertyDescriptor(createFluentPropertyDescritor(m, propertyName));
                } else if (pd.getWriteMethod() == null) {
                    pd.setWriteMethod(m);
                }
            } catch (IntrospectionException e) {
                logger.debug(e.getMessage(), e);
            }
        }
    }
}

From source file:org.apache.openjpa.lib.conf.ConfigurationImpl.java

/**
 * Create a property descriptor for the given value.
 *///from  w  w w . j  a va  2 s.  co  m
private PropertyDescriptor getPropertyDescriptor(Value val) throws IntrospectionException {
    String prop = val.getProperty();
    prop = prop.substring(prop.lastIndexOf('.') + 1);

    // set up property descriptor
    PropertyDescriptor pd;
    try {
        pd = new PropertyDescriptor(Introspector.decapitalize(prop), getClass());
    } catch (IntrospectionException ie) {
        // if there aren't any methods for this value(i.e., if it's a
        // dynamically-added value), then an IntrospectionException will
        // be thrown. Try to create a PD with no read or write methods.
        pd = new PropertyDescriptor(Introspector.decapitalize(prop), (Method) null, (Method) null);
    }
    pd.setDisplayName(findLocalized(prop + "-name", true, val.getScope()));
    pd.setShortDescription(findLocalized(prop + "-desc", true, val.getScope()));
    pd.setExpert("true".equals(findLocalized(prop + "-expert", false, val.getScope())));

    try {
        pd.setReadMethod(getClass().getMethod("get" + StringUtils.capitalize(prop), (Class[]) null));
        pd.setWriteMethod(getClass().getMethod("set" + StringUtils.capitalize(prop),
                new Class[] { pd.getReadMethod().getReturnType() }));
    } catch (Throwable t) {
        // if an error occurs, it might be because the value is a
        // dynamic property.
    }

    String type = findLocalized(prop + "-type", true, val.getScope());
    if (type != null)
        pd.setValue(ATTRIBUTE_TYPE, type);

    String cat = findLocalized(prop + "-cat", false, val.getScope());
    if (cat != null)
        pd.setValue(ATTRIBUTE_CATEGORY, cat);

    pd.setValue(ATTRIBUTE_XML, toXMLName(prop));

    String order = findLocalized(prop + "-displayorder", false, val.getScope());
    if (order != null)
        pd.setValue(ATTRIBUTE_ORDER, order);

    // collect allowed values from alias keys, listed values, and
    // interface implementors
    Collection<String> allowed = new TreeSet<String>();
    List<String> aliases = Collections.emptyList();
    if (val.getAliases() != null) {
        aliases = Arrays.asList(val.getAliases());
        for (int i = 0; i < aliases.size(); i += 2)
            allowed.add(aliases.get(i));
    }
    String[] vals = Strings.split(findLocalized(prop + "-values", false, val.getScope()), ",", 0);
    for (int i = 0; i < vals.length; i++)
        if (!aliases.contains(vals[i]))
            allowed.add(vals[i]);
    try {
        Class<?> intf = Class.forName(findLocalized(prop + "-interface", true, val.getScope()), false,
                getClass().getClassLoader());
        pd.setValue(ATTRIBUTE_INTERFACE, intf.getName());
        String[] impls = Services.getImplementors(intf);
        for (int i = 0; i < impls.length; i++)
            if (!aliases.contains(impls[i]))
                allowed.add(impls[i]);
    } catch (Throwable t) {
    }
    if (!allowed.isEmpty())
        pd.setValue(ATTRIBUTE_ALLOWED_VALUES, (String[]) allowed.toArray(new String[allowed.size()]));

    return pd;
}

From source file:org.dozer.util.ReflectionUtils.java

/**
 * There are some nasty bugs for introspection with generics. This method addresses those nasty bugs and tries to find proper methods if available
 *  http://bugs.sun.com/view_bug.do?bug_id=6788525
 *  http://bugs.sun.com/view_bug.do?bug_id=6528714
 * @param descriptor/*from  w w  w .  jav a2 s .  co  m*/
 * @return
 */
private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyDescriptor descriptor) {
    Method readMethod = descriptor.getReadMethod();
    Method writeMethod = descriptor.getWriteMethod();

    if (readMethod != null && (readMethod.isBridge() || readMethod.isSynthetic())) {
        String propertyName = descriptor.getName();
        //capitalize the first letter of the string;
        String baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
        String setMethodName = "set" + baseName;
        String getMethodName = "get" + baseName;
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(getMethodName) && !method.isBridge() && !method.isSynthetic()) {
                try {
                    descriptor.setReadMethod(method);
                } catch (IntrospectionException e) {
                    //move on
                }
            }
            if (method.getName().equals(setMethodName) && !method.isBridge() && !method.isSynthetic()) {
                try {
                    descriptor.setWriteMethod(method);
                } catch (IntrospectionException e) {
                    //move on
                }
            }
        }
    }
    return descriptor;
}

From source file:org.dozer.util.ReflectionUtils.java

static PropertyDescriptor[] getInterfacePropertyDescriptors(Class<?> interfaceClass) {
    List<PropertyDescriptor> propDescriptors = new ArrayList<PropertyDescriptor>();
    // Add prop descriptors for interface passed in
    propDescriptors.addAll(Arrays.asList(PropertyUtils.getPropertyDescriptors(interfaceClass)));

    // Look for interface inheritance. If super interfaces are found, recurse up the hierarchy tree and add prop
    // descriptors for each interface found.
    // PropertyUtils.getPropertyDescriptors() does not correctly walk the inheritance hierarchy for interfaces.
    Class<?>[] interfaces = interfaceClass.getInterfaces();
    if (interfaces != null) {
        for (Class<?> superInterfaceClass : interfaces) {
            List<PropertyDescriptor> superInterfacePropertyDescriptors = Arrays
                    .asList(getInterfacePropertyDescriptors(superInterfaceClass));
            /*//from   w  ww.j  av a2 s  . c o  m
             * #1814758
             * Check for existing descriptor with the same name to prevent 2 property descriptors with the same name being added
             * to the result list.  This caused issues when getter and setter of an attribute on different interfaces in
             * an inheritance hierarchy
             */
            for (PropertyDescriptor superPropDescriptor : superInterfacePropertyDescriptors) {
                PropertyDescriptor existingPropDescriptor = findPropDescriptorByName(propDescriptors,
                        superPropDescriptor.getName());
                if (existingPropDescriptor == null) {
                    propDescriptors.add(superPropDescriptor);
                } else {
                    try {
                        if (existingPropDescriptor.getReadMethod() == null) {
                            existingPropDescriptor.setReadMethod(superPropDescriptor.getReadMethod());
                        }
                        if (existingPropDescriptor.getWriteMethod() == null) {
                            existingPropDescriptor.setWriteMethod(superPropDescriptor.getWriteMethod());
                        }
                    } catch (IntrospectionException e) {
                        throw new MappingException(e);
                    }

                }
            }
        }
    }
    return propDescriptors.toArray(new PropertyDescriptor[propDescriptors.size()]);
}

From source file:org.jspresso.framework.util.bean.PropertyHelper.java

private static PropertyDescriptor getPropertyDescriptorNoException(Class<?> beanClass, String property) {
    PropertyDescriptor descriptorToReturn = null;
    int nestedDotIndex = property.indexOf(IAccessor.NESTED_DELIM);
    if (nestedDotIndex > 0) {
        PropertyDescriptor rootDescriptor = getPropertyDescriptorNoException(beanClass,
                property.substring(0, nestedDotIndex));
        if (rootDescriptor != null) {
            descriptorToReturn = getPropertyDescriptorNoException(rootDescriptor.getPropertyType(),
                    property.substring(nestedDotIndex + 1));
        }/*from w  ww . j a  v a2s. c  om*/
    } else {
        PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
        for (PropertyDescriptor descriptor : descriptors) {
            if (property.substring(0, 1).equalsIgnoreCase(descriptor.getName().substring(0, 1))
                    && property.substring(1).equals(descriptor.getName().substring(1))) {
                // 1st letter might be uppercase in descriptor and lowercase in
                // property when property name is like 'tEst'.
                descriptorToReturn = descriptor;
            }
        }
    }
    if (descriptorToReturn == null || descriptorToReturn.getWriteMethod() == null) {
        // If we reach this point, no property with the given name has been found.
        // or the found descriptor is read-only.
        // If beanClass is indeed an interface, we must also deal with all its
        // super-interfaces.
        List<Class<?>> superTypes = new ArrayList<>();
        if (beanClass.getSuperclass() != null && beanClass.getSuperclass() != Object.class) {
            superTypes.add(beanClass.getSuperclass());
        }
        Collections.addAll(superTypes, beanClass.getInterfaces());
        for (Class<?> superType : superTypes) {
            PropertyDescriptor descriptor;
            descriptor = getPropertyDescriptorNoException(superType, property);
            if (descriptor != null) {
                if (descriptorToReturn != null) {
                    try {
                        descriptorToReturn.setWriteMethod(descriptor.getWriteMethod());
                    } catch (IntrospectionException ex) {
                        throw new NestedRuntimeException(ex);
                    }
                } else {
                    descriptorToReturn = descriptor;
                }
            }
        }
    }
    return descriptorToReturn;
}

From source file:org.springframework.beans.ExtendedBeanInfo.java

private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
    int nParams = method.getParameterCount();
    String propertyName = propertyNameFor(method);
    Class<?> propertyType = method.getParameterTypes()[nParams - 1];
    PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
    if (nParams == 1) {
        if (existingPd == null) {
            this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method));
        } else {//ww w  . j a  v  a 2 s .c om
            existingPd.setWriteMethod(method);
        }
    } else if (nParams == 2) {
        if (existingPd == null) {
            this.propertyDescriptors
                    .add(new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method));
        } else if (existingPd instanceof IndexedPropertyDescriptor) {
            ((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method);
        } else {
            this.propertyDescriptors.remove(existingPd);
            this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(propertyName,
                    existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method));
        }
    } else {
        throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method);
    }
}