> Test classes attached.
Well, now they are.
import java.beans.PropertyDescriptor;

import org.apache.commons.beanutils.*;


public class MyClass extends MySuperClass {
    //
    // bean property "id"
    //
    private String id = "initial id";
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    
    
    //
    // static stuff
    //
    
    public static void main(String[] args) throws Exception {
        Object bean = new MyClass();
        printAccessorMethodInfo(bean);
        
        setProperty(bean, "id", "some id"); // works like a charm
        getProperty(bean, "id"); // fails
    }
    
    
    static void
    setProperty(Object bean, String property, Object value)
    {
        try {
            
            PropertyUtils.setProperty(bean, property, value);
            //BeanUtils.setProperty(bean, property, value);
            
            System.out.println("setProperty succeeded.");
        } catch (Exception e) {
            printSetError(bean, property, value, e);
        }
    }
    
    static void
    getProperty(Object bean, String property)
    {
        try {
            
            PropertyUtils.getProperty(bean, property);
            //BeanUtils.getProperty(bean, property);
            
            System.out.println("getProperty succeeded.");
        } catch (Exception e) {
            printGetError(bean, property, e);
        }
    }
    
    
    static void printAccessorMethodInfo(Object bean) throws Exception {
        PropertyUtilsBean pub = new PropertyUtilsBean();
        PropertyDescriptor pDesc = pub.getPropertyDescriptor(bean, "id");
        System.out.println("read method = " + pDesc.getReadMethod());
        System.out.println("write method = " + pDesc.getWriteMethod());
    }
    
    //
    // error messages
    //
    
    private static void
    printSetError(Object bean, String property, Object value, Exception e)
    {
        System.err.println(
            "\nsetProperty('" + bean + "', \"" + property + "\""
            + ", '" + value + "') failed:");
        e.printStackTrace();
    }
    
    private static void
    printGetError(Object bean, String property, Exception e)
    {
        System.err.println(
            "\ngetProperty('" + bean + "', \"" + property + "\""
            + ") failed:");
        e.printStackTrace();
    }
}
/**
 * Depending on whether this class is package-private or public 
 * TestInheritedProperty fails or does not fail.
 */
// public
abstract class MySuperClass {
    abstract public String getId();
    abstract public void setId(String id);
    
    // What if the method's definitions are not abstract?
    // -> same error
    /*
    public String getId() {
        throw new RuntimeException("override me");
    }
    public void setId(String id) {
        throw new RuntimeException("override me");
    }
    */
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to