Path: blob/master/test/jdk/java/beans/Introspector/OverriddenGenericGetter.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 Introspector should work with overridden generic getter method32*/33public final class OverriddenGenericGetter {3435static class Parent<T> {36private T value;37public T getValue() {return value;}38public final void setValue(T value) {this.value = value;}39}4041static class ChildO extends Parent<Object> {42public ChildO() {}43@Override44public Object getValue() {return super.getValue();}45}4647static class ChildA extends Parent<ArithmeticException> {48public ChildA() {}49@Override50public ArithmeticException getValue() {return super.getValue();}51}5253static class ChildS extends Parent<String> {54public ChildS() {}55@Override56public String getValue() {return super.getValue();}57}5859public static void main(String[] args) throws Exception {60testBehaviour(ChildA.class);61testBehaviour(ChildO.class);62testBehaviour(ChildS.class);63test(ChildA.class, ArithmeticException.class);64test(ChildO.class, Object.class);65test(ChildS.class, String.class);66}6768private static void testBehaviour(Class<?> beanClass) throws Exception {69BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);70PropertyDescriptor pd = beanInfo.getPropertyDescriptors()[0];71Object bean = beanClass.getConstructor().newInstance();72Method readMethod = pd.getReadMethod();73Method writeMethod = pd.getWriteMethod();7475// check roundtrip for default null values76writeMethod.invoke(bean,readMethod.invoke(bean));77writeMethod.invoke(bean,readMethod.invoke(bean));7879// set property to non-default value80// check roundtrip for non-default values81Object param = pd.getPropertyType().getConstructor().newInstance();82writeMethod.invoke(bean, param);83writeMethod.invoke(bean, readMethod.invoke(bean));84writeMethod.invoke(bean, readMethod.invoke(bean));85}8687private static void test(Class<?> beanClass, Class<?> type) throws Exception {88BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);89PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();90if (pds.length != 1) {91throw new RuntimeException("Wrong number of properties");92}93PropertyDescriptor pd = pds[0];94String name = pd.getName();95if (!name.equals("value")) {96throw new RuntimeException("Wrong name: " + name);97}9899Class<?> propertyType = pd.getPropertyType();100if (propertyType != type) {101throw new RuntimeException("Wrong property type: " + propertyType);102}103Method readMethod = pd.getReadMethod();104if (readMethod == null) {105throw new RuntimeException("Read method is null");106}107Class<?> returnType = readMethod.getReturnType();108if (returnType != type) {109throw new RuntimeException("Wrong return type; " + returnType);110}111Method writeMethod = pd.getWriteMethod();112if (writeMethod == null) {113throw new RuntimeException("Write method is null");114}115Class<?>[] parameterTypes = writeMethod.getParameterTypes();116if (parameterTypes.length != 1) {117throw new RuntimeException("Wrong parameters " + parameterTypes);118}119if (parameterTypes[0] != Object.class) {120throw new RuntimeException("Wrong type: " + parameterTypes[0]);121}122}123}124125126