Java introspection – strange behavior

The following code is a small example that can easily reproduce the problem So I have a variable of type string and set a default value on it I have three methods:

Getter > setter > a convenient way to convert a string to a Boolean value

The internal callback does not return getmethod and setter as the getter of writemethod Instead, the istest () method is returned as readmethod The setter is empty

I learned from the document that if the type is a Boolean value, the "is" method takes precedence over get, but the type is string, so it is meaningless not even to find the "is XXX" method

public class Test {
    public class Arguments {
        private String test = Boolean.toString(true);

        public boolean istest() {
            return Boolean.parseBoolean(test);
        }

        public String gettest() {
            return test;
        }

        public void setTest(String test) {
            this.test = test;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IntrospectionException {
        BeanInfo info = Introspector.getBeanInfo(Arguments.class);
        System.out.println("Getter: " + info.getPropertyDescriptors()[1].getReadMethod());
        System.out.println("Setter: " + info.getPropertyDescriptors()[1].getWriteMethod());
        PropertyDescriptor descr = new PropertyDescriptor("test",Arguments.class);
        System.out.println("T");
    }

}

Does anyone have an opinion in this regard?

Additional information:

>The order will not change the result The istest () method is always regarded as readmethod > If I simply rename istest () to bstest (), it selects getters and setters as readmethod and writemethod So it is related to "is XXX"

Solution

The result you get is actually the expected result, according to JavaBeans specification

Reference to paragraph 8.3 1 simple properties:

Then, reference paragraph 8.3.1 for Boolean attributes 2:

From your example, the introspector will detect the istest and gettest methods Because istest takes precedence over gettest, it uses istest to determine the type of test attribute as Boolean However, the introspector expects the setter to have the signature void settest (Boolean test) and cannot find it, so the setter method is null

It should be noted that introspector does not read fields It uses the signature of getter / setter methods to determine which fields exist and their corresponding types The istest method signature specifies a Boolean property named test. Therefore, regardless of the actual test type, the introspector will consider that your class has a Boolean property test

In fact, for all introspectives, property testing may not even exist! You can convince yourself with the following code:

class Test {

    public class Arguments {
        public boolean istest() {
            return true;
        }
    }

    public static void main(String[] args) throws IntrospectionException {
        BeanInfo info = Introspector.getBeanInfo(Arguments.class);
        System.out.println("Getter: " + info.getPropertyDescriptors()[1].getReadMethod());
        System.out.println("Name of property: " + info.getPropertyDescriptors()[1].getName());
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>