Example usage for java.beans PropertyDescriptor setReadMethod

List of usage examples for java.beans PropertyDescriptor setReadMethod

Introduction

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

Prototype

public synchronized void setReadMethod(Method readMethod) throws IntrospectionException 

Source Link

Document

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

Usage

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

private static void addScalaGetter(Map<String, PropertyDescriptor> propertyDescriptors, Method readMethod) {
    String propertyName = readMethod.getName();

    PropertyDescriptor pd = propertyDescriptors.get(propertyName);
    if (pd != null && pd.getReadMethod() == null) {
        try {/*  ww  w  .ja  v a  2  s . c  om*/
            pd.setReadMethod(readMethod);
        } catch (IntrospectionException ex) {
            logger.debug("Could not add read method [" + readMethod + "] for " + "property [" + propertyName
                    + "]: " + ex.getMessage());
        }
    } else if (pd == null) {
        try {
            pd = new PropertyDescriptor(propertyName, readMethod, null);
            propertyDescriptors.put(propertyName, pd);
        } catch (IntrospectionException ex) {
            logger.debug("Could not create new PropertyDescriptor for " + "readMethod [" + readMethod
                    + "] 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));
            /*//from w  w w  . j av a2s  .  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.apache.openjpa.lib.conf.ConfigurationImpl.java

/**
 * Create a property descriptor for the given value.
 */// www  .ja v a2  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//w w  w. j ava 2  s  .com
 * @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));
            /*//  w  w w .jav  a  2 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.wings.STransferHandler.java

private static PropertyDescriptor getPropertyDescriptor(String propertyName, Class<?> componentClass) {
    PropertyDescriptor descriptor = new PropertyDescriptor();

    Method[] methods = componentClass.getMethods();
    for (Method method : methods) {
        if (method.getName().equalsIgnoreCase("get" + propertyName)) {
            descriptor.setReadMethod(method);
            continue;
        }/* w w w  .  ja v a2  s. c  o  m*/

        if (method.getName().equalsIgnoreCase("set" + propertyName)) {
            descriptor.setWriteMethod(method);
            continue;
        }
    }

    return descriptor;
}