1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.beans.BeanInfo; 25 import java.beans.Introspector; 26 import java.beans.PropertyDescriptor; 27 import java.lang.reflect.Method; 28 29 /** 30 * @test 31 * @bug 8196373 32 * @summary Introspector should work with overridden generic getter method 33 */ 34 public final class OverriddenGenericGetter { 35 36 static class Parent<T> { 37 private T value; getValue()38 public T getValue() {return value;} setValue(T value)39 public final void setValue(T value) {this.value = value;} 40 } 41 42 static class ChildO extends Parent<Object> { ChildO()43 public ChildO() {} 44 @Override getValue()45 public Object getValue() {return super.getValue();} 46 } 47 48 static class ChildA extends Parent<ArithmeticException> { ChildA()49 public ChildA() {} 50 @Override getValue()51 public ArithmeticException getValue() {return super.getValue();} 52 } 53 54 static class ChildS extends Parent<String> { ChildS()55 public ChildS() {} 56 @Override getValue()57 public String getValue() {return super.getValue();} 58 } 59 main(String[] args)60 public static void main(String[] args) throws Exception { 61 testBehaviour(ChildA.class); 62 testBehaviour(ChildO.class); 63 testBehaviour(ChildS.class); 64 test(ChildA.class, ArithmeticException.class); 65 test(ChildO.class, Object.class); 66 test(ChildS.class, String.class); 67 } 68 testBehaviour(Class<?> beanClass)69 private static void testBehaviour(Class<?> beanClass) throws Exception { 70 BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class); 71 PropertyDescriptor pd = beanInfo.getPropertyDescriptors()[0]; 72 Object bean = beanClass.getConstructor().newInstance(); 73 Method readMethod = pd.getReadMethod(); 74 Method writeMethod = pd.getWriteMethod(); 75 76 // check roundtrip for default null values 77 writeMethod.invoke(bean,readMethod.invoke(bean)); 78 writeMethod.invoke(bean,readMethod.invoke(bean)); 79 80 // set property to non-default value 81 // check roundtrip for non-default values 82 Object param = pd.getPropertyType().getConstructor().newInstance(); 83 writeMethod.invoke(bean, param); 84 writeMethod.invoke(bean, readMethod.invoke(bean)); 85 writeMethod.invoke(bean, readMethod.invoke(bean)); 86 } 87 test(Class<?> beanClass, Class<?> type)88 private static void test(Class<?> beanClass, Class<?> type) throws Exception { 89 BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class); 90 PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); 91 if (pds.length != 1) { 92 throw new RuntimeException("Wrong number of properties"); 93 } 94 PropertyDescriptor pd = pds[0]; 95 String name = pd.getName(); 96 if (!name.equals("value")) { 97 throw new RuntimeException("Wrong name: " + name); 98 } 99 100 Class<?> propertyType = pd.getPropertyType(); 101 if (propertyType != type) { 102 throw new RuntimeException("Wrong property type: " + propertyType); 103 } 104 Method readMethod = pd.getReadMethod(); 105 if (readMethod == null) { 106 throw new RuntimeException("Read method is null"); 107 } 108 Class<?> returnType = readMethod.getReturnType(); 109 if (returnType != type) { 110 throw new RuntimeException("Wrong return type; " + returnType); 111 } 112 Method writeMethod = pd.getWriteMethod(); 113 if (writeMethod == null) { 114 throw new RuntimeException("Write method is null"); 115 } 116 Class<?>[] parameterTypes = writeMethod.getParameterTypes(); 117 if (parameterTypes.length != 1) { 118 throw new RuntimeException("Wrong parameters " + parameterTypes); 119 } 120 if (parameterTypes[0] != Object.class) { 121 throw new RuntimeException("Wrong type: " + parameterTypes[0]); 122 } 123 } 124 } 125