This discussion is archived
1 Reply Latest reply: Dec 16, 2010 3:35 AM by 823509 RSS

Introspector does not correctly find property with interface hierarchy type

823509 Newbie
Currently Being Moderated
Hi,

I read the javabean specs but I found nowhere this behavior. Is it a bug ?

testPropertyType fails because expects Data class
testPropertyReadable succeed because Data getMyData method exists
testPropertyWritable fails because no setMyData(Data) method does not exists

on javase 6

this may be related to PropertyDescriptor.getWriteMethod occasionally returns null
which IMHO is due to database access proxy class
package beanbug;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

import org.junit.Test;

public class DefaultBeanTest {
    
    @Test
    public void testPropertyType()
        throws Exception
    {
        final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
        for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
            if ("class".equals(property.getName())) {
                continue;
            }
            
            if ("myData".equals(property.getName())) {
                if (!property.getPropertyType().equals(ModifiableData.class)) {
                    throw new AssertionError("expects " + ModifiableData.class + " but was "
                            + property.getPropertyType());
                }
            }
        }
    }
    

    @Test
    public void testPropertyReadable()
        throws Exception
    {
        final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
        for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
            if ("class".equals(property.getName())) {
                continue;
            }
            
            if ("myData".equals(property.getName())) {
                if (property.getReadMethod() == null) {
                    throw new AssertionError("expects read method");
                }
            }
        }
    }
    

    @Test
    public void testPropertyWritable()
        throws Exception
    {
        final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
        for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
            if ("class".equals(property.getName())) {
                continue;
            }
            
            if ("myData".equals(property.getName())) {
                if (property.getWriteMethod() == null) {
                    throw new AssertionError("expects write method");
                }
            }
        }
    }
    

    static interface Data {
        
    }
    
    static interface ModifiableData
            extends Data {
    }
    
    static class DefaultData
            implements ModifiableData {
        
    }
    
    static interface Bean {
        
        Data getMyData();
        
    }
    
    static interface ModifiableBean
            extends Bean {
        
        ModifiableData getMyData();
        

        void setMyData(
                ModifiableData data);
    }
    
    static class DefaultBean
            implements ModifiableBean {
        
        @Override
        public ModifiableData getMyData()
        {
            return this.data;
        }
        

        @Override
        public void setMyData(
                final ModifiableData data)
        {
            this.data = data;
        }
        

        private ModifiableData data;
        
    }
    
}
Edited by: user13469619 on 13-Dec-2010 10:06

Edited by: sabre150 on 13-Dec-2010 11:23

Moderator action : corrected [ code] tags.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points