bug-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug classpath/22795] Static method mistaken for JavaBeans getter


From: gcc-bugzilla at gcc dot gnu dot org
Subject: [Bug classpath/22795] Static method mistaken for JavaBeans getter
Date: 16 Oct 2005 01:27:15 -0000

There appears to be a problem in gnu.java.beans.IntrospectionIncubator. In the
method addMethod(java.lang.reflect.Method), there is no check whether the
method is an instance or static method. This results in methods with the
signature:

public static <Type> getXXX();

being mistaken for JavaBeans property accessors. This looks and feels wrong,
and causes concrete problems for some applications that use JavaBeans.


------- Comment #1 from from-classpath at savannah dot gnu dot org  2004-11-04 
16:43 -------
I am on this.

Can you provide a simple test case where this bug shows up? (pseudo javacode is
ok)

Do you think its OK to just exclude static methods from being added in
IntrospectionIncubator? If so my fix would replace line:

if(Modifier.isPublic(method.getModifiers())) {
  // code that really adds the method
}

with

if(Modifier.isPublic(method.getModifiers()) &&
   !Modifier.isStatic(method.getModifiers())) {
  // code that really adds the method
}


------- Comment #2 from from-classpath at savannah dot gnu dot org  2004-11-05 
11:01 -------
Hello, and thanks for the prompt response.

Yes, I do think excluding static methods with beanish signatures is the right
thing to do; it is also more consistent with the Sun implementation of
java.beans.*.

The following code can reproduce the problem:

--8<--

import java.beans.*;
import java.util.*;
import java.io.Serializable;

class TestBeanA implements Serializable {
    TestBeanA() {}
    public void setObject(Object o) {}
    public Object getObject() { return null; }
}

class TestBeanB extends TestBeanA {
    TestBeanB() {}
    public static Object getSomeStaticValue() { return null; }
}

class NameComparator implements Comparator {
    public boolean equals(Object o) {
        return o!=null && NameComparator.class.equals(o.getClass());
    }
    public int compare(Object o1,Object o2) {
        if (o1 instanceof PropertyDescriptor && o2 instanceof
PropertyDescriptor) {
            String o1val = ((PropertyDescriptor)o1).getName();
            String o2val = ((PropertyDescriptor)o2).getName();
            return o1val.compareTo(o2val);
        } else throw new ClassCastException();
    }
    private NameComparator() {}
    static final NameComparator cmp = new NameComparator();
}

public class BeanTest {
    public static void main(String[] args) {
        try {
            PropertyDescriptor[] pda =
Introspector.getBeanInfo(TestBeanA.class).getPropertyDescriptors();
            PropertyDescriptor[] pdb =
Introspector.getBeanInfo(TestBeanB.class).getPropertyDescriptors();
            Collection a = new TreeSet(NameComparator.cmp),b = new
TreeSet(NameComparator.cmp);
            a.addAll(Arrays.asList(pda));
            b.addAll(Arrays.asList(pdb));
            System.out.println((a.containsAll(b) &&
b.containsAll(a))?"OK":"Error");
        } catch (IntrospectionException ie) {
            ie.printStackTrace(System.out);
        } catch (ClassCastException cce) {
            cce.printStackTrace(System.out);
        }
    }
}

---8<---

This will print out "OK" on Sun's JDK 1.4.2 SE, and "Error" on a JVM using the
classpath from CVS. 

I can be reached via email. My user alias is 'David.White', and the DNS MX
query string is 'chipkarte.at'. (verbose munged E-Mail address, please read
carefully)

thanks!


------- Comment #3 from from-classpath at savannah dot gnu dot org  2004-11-05 
12:39 -------
I fixed this in the way I proposed in the last post and it looks good (=
behaves as JDK now). See classpath-patches for the patch.

I let this bug report open until I have written some mauve tests for it (or is
anyone else interested in that :) ).


------- Comment #4 from from-classpath at savannah dot gnu dot org  2004-11-05 
13:02 -------
Hello, and thanks for the prompt response.

Yes, I do think excluding static methods with beanish signatures is the right
thing to do; it is also more consistent with the Sun implementation of
java.beans.*.

The following code can reproduce the problem:

--8<--

import java.beans.*;
import java.util.*;
import java.io.Serializable;

class TestBeanA implements Serializable {
    TestBeanA() {}
    public void setObject(Object o) {}
    public Object getObject() { return null; }
}

class TestBeanB extends TestBeanA {
    TestBeanB() {}
    public static Object getSomeStaticValue() { return null; }
}

class NameComparator implements Comparator {
    public boolean equals(Object o) {
        return o!=null && NameComparator.class.equals(o.getClass());
    }
    public int compare(Object o1,Object o2) {
        if (o1 instanceof PropertyDescriptor && o2 instanceof
PropertyDescriptor) {
            String o1val = ((PropertyDescriptor)o1).getName();
            String o2val = ((PropertyDescriptor)o2).getName();
            return o1val.compareTo(o2val);
        } else throw new ClassCastException();
    }
    private NameComparator() {}
    static final NameComparator cmp = new NameComparator();
}

public class BeanTest {
    public static void main(String[] args) {
        try {
            PropertyDescriptor[] pda =
Introspector.getBeanInfo(TestBeanA.class).getPropertyDescriptors();
            PropertyDescriptor[] pdb =
Introspector.getBeanInfo(TestBeanB.class).getPropertyDescriptors();
            Collection a = new TreeSet(NameComparator.cmp),b = new
TreeSet(NameComparator.cmp);
            a.addAll(Arrays.asList(pda));
            b.addAll(Arrays.asList(pdb));
            System.out.println((a.containsAll(b) &&
b.containsAll(a))?"OK":"Error");
        } catch (IntrospectionException ie) {
            ie.printStackTrace(System.out);
        } catch (ClassCastException cce) {
            cce.printStackTrace(System.out);
        }
    }
}

---8<---

This will print out "OK" on Sun's JDK 1.4.2 SE, and "Error" on a JVM using the
classpath from CVS. 

I can be reached via email. My user alias is 'David.White', and the DNS MX
query string is 'chipkarte.at'. (verbose munged E-Mail address, please read
carefully)

thanks!


------- Comment #5 from from-classpath at savannah dot gnu dot org  2004-11-07 
03:21 -------
provided mauve tests gnu/testlet/java/beans/Introspector/getBeanInfo
for this.


------- Comment #6 from from-classpath at savannah dot gnu dot org  2005-04-15 
12:50 -------
Forgot to mark as fixed.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22795





reply via email to

[Prev in Thread] Current Thread [Next in Thread]