Path: blob/master/test/jdk/java/beans/Introspector/OverloadedSetter.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.beans.BeanInfo;24import java.beans.Introspector;25import java.beans.PropertyDescriptor;26import java.lang.reflect.Method;2728/**29* @test30* @bug 819637331* @summary behavior of this class is not specified, but it can be used to check32* our implementation for accidental changes33*/34public final class OverloadedSetter {3536class AAA {}37class CCC extends AAA {}38class BBB extends CCC {}39class DDD extends CCC {}4041class ZZZ {}4243// DDD will be selected because it is most specific type.44class ParentADC<T> {45public void setValue(AAA value) {}46public void setValue(DDD value) {}47public void setValue(CCC value) {}48}49// DDD will be selected because it is most specific type.50class ParentACD<T> {51public void setValue(AAA value) {}52public void setValue(CCC value) {}53public void setValue(DDD value) {}54}55// DDD will be selected because it is most specific type.56class ParentDAC<T> {57public void setValue(DDD value) {}58public void setValue(AAA value) {}59public void setValue(CCC value) {}60}61// DDD will be selected because it is most specific type.62class ParentDCA<T> {63public void setValue(DDD value) {}64public void setValue(CCC value) {}65public void setValue(AAA value) {}66}67// DDD will be selected because it is most specific type.68class ParentCAD<T> {69public void setValue(CCC value) {}70public void setValue(AAA value) {}71public void setValue(DDD value) {}72}73// DDD will be selected because it is most specific type.74class ParentCDA<T> {75public void setValue(CCC value) {}76public void setValue(DDD value) {}77public void setValue(AAA value) {}78}79// DDD will be selected because it is most specific type and ZZZ will be80// skipped because it will be placed at the end of the methods list.81class ParentCDAZ<T> {82public void setValue(CCC value) {}83public void setValue(DDD value) {}84public void setValue(AAA value) {}85public void setValue(ZZZ value) {}86}87// DDD will be selected because it is most specific type which related to88// the type of getValue(); BBB will be skipped because it is not a type or89// subclass of the type returned by getValue();90class ParentDACB<T> {91public DDD getValue(){return null;}92public void setValue(AAA value) {}93public void setValue(DDD value) {}94public void setValue(CCC value) {}95public void setValue(BBB value) {}96}9798public static void main(String[] args) throws Exception {99test(ParentADC.class);100test(ParentACD.class);101test(ParentDAC.class);102test(ParentDCA.class);103test(ParentCAD.class);104test(ParentCDA.class);105test(ParentCDAZ.class);106test(ParentDACB.class);107}108109private static void test(Class<?> beanClass) throws Exception {110BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);111PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();112if (pds.length != 1) {113throw new RuntimeException("Wrong number of properties");114}115PropertyDescriptor pd = pds[0];116String name = pd.getName();117if (!name.equals("value")) {118throw new RuntimeException("Wrong name: " + name);119}120121Class<?> propertyType = pd.getPropertyType();122if (propertyType != DDD.class) {123throw new RuntimeException("Wrong property type: " + propertyType);124}125Method writeMethod = pd.getWriteMethod();126if (writeMethod == null) {127throw new RuntimeException("Write method is null");128}129Class<?>[] parameterTypes = writeMethod.getParameterTypes();130if (parameterTypes.length != 1) {131throw new RuntimeException("Wrong parameters " + parameterTypes);132}133if (parameterTypes[0] != DDD.class) {134throw new RuntimeException("Wrong type: " + parameterTypes[0]);135}136}137}138139140