jdk/test/java/beans/Introspector/Test4619536.java
author lana
Tue, 23 May 2017 21:11:42 +0000
changeset 45187 331a6542b5b8
parent 5506 202f599c92aa
permissions -rw-r--r--
Added tag jdk-10+7 for changeset d554736d963e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4619536
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests resolving the ambiguities in the resolution of IndexedPropertyDescriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.IndexedPropertyDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.PropertyDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Test4619536 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        IndexedPropertyDescriptor ipd = BeanUtils.getIndexedPropertyDescriptor(A.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        if (!ipd.getIndexedPropertyType().equals(String.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            error(ipd, "A.foo should be String type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(B.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        if (pd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            error(pd, "B.foo should not be an indexed property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (!pd.getPropertyType().equals(Date.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            error(pd, "B.foo should be Date type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        pd = BeanUtils.findPropertyDescriptor(Child.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (pd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            error(pd, "Child.foo should not be an indexed property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        pd = BeanUtils.findPropertyDescriptor(Classic.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (pd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            error(pd, "Classic.foo should not be an indexed property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ipd = BeanUtils.getIndexedPropertyDescriptor(Index.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (!hasIPD(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            error(pd, "Index.foo should have ipd values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (hasPD(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            error(ipd, "Index.foo should not have pd values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        ipd = BeanUtils.getIndexedPropertyDescriptor(All.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!hasPD(ipd) || !hasIPD(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            error(ipd, "All.foo should have all pd/ipd values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (!isValidType(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            error(ipd, "All.foo pdType should equal ipdType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ipd = BeanUtils.getIndexedPropertyDescriptor(Getter.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (ipd.getReadMethod() == null || ipd.getWriteMethod() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            error(ipd, "Getter.foo classic methods incorrect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (!isValidType(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            error(ipd, "Getter.foo pdType should equal ipdType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ipd = BeanUtils.getIndexedPropertyDescriptor(BadGetter.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (hasPD(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            error(ipd, "BadGetter.foo should not have classic methods");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ipd = BeanUtils.getIndexedPropertyDescriptor(Setter.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (ipd.getReadMethod() != null || ipd.getWriteMethod() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            error(ipd, "Setter.foo classic methods incorrect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (!isValidType(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            error(ipd, "Setter.foo pdType should equal ipdType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ipd = BeanUtils.getIndexedPropertyDescriptor(BadSetter.class, "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (hasPD(ipd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            error(ipd, "BadSetter.foo should not have classic methods");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static boolean hasPD(PropertyDescriptor pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (null == pd.getPropertyType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return (null != pd.getReadMethod())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            || (null != pd.getWriteMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (null == ipd.getIndexedPropertyType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return (null != ipd.getIndexedReadMethod())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            || (null != ipd.getIndexedWriteMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static boolean isValidType(IndexedPropertyDescriptor ipd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Class type = ipd.getPropertyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return type.isArray() && type.getComponentType().equals(ipd.getIndexedPropertyType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static void error(PropertyDescriptor pd, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        BeanUtils.reportPropertyDescriptor(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        throw new Error(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // Test case from 4619536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static class A {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // prop foo on A should be indexed of type String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public String getFoo(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static class B extends A {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // prop foo on should be non-indexed of type Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        public Date getFoo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // Test case from 4812428 (this works in 1.5.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static class Parent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public void setFoo(String foo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public Child getFoo(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static class Child extends Parent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public Child getFoo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // This class has a complete set of pd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static class Classic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public String[] getFoo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        public void setFoo(String[] foo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // This class has a complete set of ipd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static class Index {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public String getFoo(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        public void setFoo(int i, String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // This class adds a proper getter and setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public static class All extends Index {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public String[] getFoo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public void setFoo(String[] foo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // This class adds a classic getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static class Getter extends Index {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        public String[] getFoo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // This class has an alternate getter and should be merged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static class BadGetter extends Index {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        public String getFoo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // This class adds a classic setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public static class Setter extends Index {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public void setFoo(String[] f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // This class has an alternate setter and should be merged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static class BadSetter extends Index {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        public void setFoo(String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}