[OFF-TOPIC] Generic Introspecting Problem

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[OFF-TOPIC] Generic Introspecting Problem

Fabio Cechinel Veronez
        Hello all,

        fist of all; I'm sorry about my poor English and really sorry about
this off-topic message but I don't know for whom ask this kind of
question (I would thank any help or even suggests of other list =)).

        Well, let me try to explain my problem: I'm having a problem when I
try to get, using 'java.beans.Introspector', the property type of a
class that implements a generic interface. My interface
'Identifiable<T>' defines a method called 'T getId()' that should be
implemented by the concret class that defines the id type. But when I
try to get the property type of the field 'id', in some cases I get
'java.lang.Object' and in other cases I get the right type depending
on the set of the getter, setter and static methods.

        I wrote the testing case below and i was expecting to see the output:

        "ThreePropertyAndWithoutValueOfWorks -> id:
ThreePropertyAndWithoutValueOfWorks$Id
        ThreePropertiesAndValueOfDontWork -> id: ThreePropertiesAndValueOfDontWork$Id
        WithValueOfButJustOnePropertyWorks -> id: WithValueOfButJustOnePropertyWorks$Id
        ThreePropertiesAndWithoutValueOfWorks -> id:
ThreePropertiesAndWithoutValueOfWorks$Id"

        but i received:

        "ThreePropertyAndWithoutValueOfWorks -> id:
ThreePropertyAndWithoutValueOfWorks$Id
        ThreePropertiesAndValueOfDontWork -> id: java.lang.Object
        WithValueOfButJustOnePropertyWorks -> id: WithValueOfButJustOnePropertyWorks$Id
        ThreePropertiesAndWithoutValueOfWorks -> id:
ThreePropertiesAndWithoutValueOfWorks$Id"

        Just 'ThreePropertiesAndValueOfDontWork' class does not return the
concret (not generic) type of 'id'. Note that the only difference
between the example classes are the getter/setter and the "valueOf
style" method.

        I don't know the reason of this strange behavior. Any one could help
me? It was expected?

        Ps.: My java -version:
        "java version "1.5.0_06"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
        Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)"

       
CODE:

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;


public class IntrospectorTest {
       
        public static void main(String[] args) throws IntrospectionException {
                printProperty(ThreePropertyAndWithoutValueOfWorks.class, "id");
                printProperty(ThreePropertiesAndValueOfDontWork.class, "id");
                printProperty(WithValueOfButJustOnePropertyWorks.class, "id");
                printProperty(ThreePropertiesAndWithoutValueOfWorks.class, "id");
               
        }
       
        public static void printProperty(Class target, String property)
throws IntrospectionException {
                PropertyDescriptor[] pds =
Introspector.getBeanInfo(target).getPropertyDescriptors();
                for (PropertyDescriptor descriptor : pds) {
                        if (descriptor.getName().equals(property))
                                System.out.println(target.getSimpleName() + " -> " +
descriptor.getName() + ": " + descriptor.getPropertyType().getName());
                }
        }

}

interface Identifiable<T> {
       
        public T getId();
}

class ThreePropertiesAndWithoutValueOfWorks implements
Identifiable<ThreePropertiesAndWithoutValueOfWorks.Id> {
       
        private Id id;
        private String name;
        private Object other;
       
       
        public Id getId() {
                return id;
        }
       
        public void setId(Id id) {
                this.id = id;
        }

        public Object getOther() {
                return other;
        }
       
        public void setOther(Object other) {
                this.other = other;
        }
       
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public static class Id {
        }
}

class ThreePropertiesAndValueOfDontWork implements
Identifiable<ThreePropertiesAndValueOfDontWork.Id> {
       
        private Id id;
        private String name;
        private Object other;
       
       
        public Id getId() {
                return id;
        }
       
        public void setId(Id id) {
                this.id = id;
        }

        public Object getOther() {
                return other;
        }
       
        public void setOther(Object other) {
                this.other = other;
        }
       
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
       
        public static ThreePropertyAndWithoutValueOfWorks valueOf(Integer value) {
                return new ThreePropertyAndWithoutValueOfWorks();
        }

        public static class Id {
        }
}

class ThreePropertyAndWithoutValueOfWorks implements
Identifiable<ThreePropertyAndWithoutValueOfWorks.Id> {
       
        private Id id;
        private String name;
        private Object other;
       
       
        public Id getId() {
                return id;
        }
       
        public void setId(Id id) {
                this.id = id;
        }

        public Object getOther() {
                return other;
        }
       
        public void setOther(Object other) {
                this.other = other;
        }
       
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
               
        public static class Id {
        }
}

class WithValueOfButJustOnePropertyWorks implements
Identifiable<WithValueOfButJustOnePropertyWorks.Id> {
       
        private Id id;
       
        public Id getId() {
                return id;
        }
       
        public void setId(Id id) {
                this.id = id;
        }
       
        public static ThreePropertyAndWithoutValueOfWorks valueOf(Integer value) {
                return new ThreePropertyAndWithoutValueOfWorks();
        }
               
        public static class Id {
        }
}



--
Fabio Cechinel Veronez
Bacharel em Ciências da Computação - UFSC
Sun Certified Programmer For The Java 2 Platform 1.4

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

IntrospectorTest.java (4K) Download Attachment