|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8071693 |
| 27 | + * @summary Verify that the Introspector finds default methods inherited |
| 28 | + * from interfaces |
| 29 | + */ |
| 30 | + |
| 31 | +import java.beans.IntrospectionException; |
| 32 | +import java.beans.Introspector; |
| 33 | +import java.beans.PropertyDescriptor; |
| 34 | +import java.lang.reflect.Method; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.HashSet; |
| 37 | +import java.util.NavigableSet; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +public class DefaultMethodBeanPropertyTest { |
| 42 | + |
| 43 | +////////////////////////////////////// |
| 44 | +// // |
| 45 | +// SCENARIO 1 // |
| 46 | +// // |
| 47 | +////////////////////////////////////// |
| 48 | + |
| 49 | + public interface A1 { |
| 50 | + default int getValue() { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + default Object getObj() { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + public static int getStaticValue() { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public interface B1 extends A1 { |
| 63 | + } |
| 64 | + |
| 65 | + public interface C1 extends A1 { |
| 66 | + Number getFoo(); |
| 67 | + } |
| 68 | + |
| 69 | + public class D1 implements C1 { |
| 70 | + @Override |
| 71 | + public Integer getFoo() { |
| 72 | + return null; |
| 73 | + } |
| 74 | + @Override |
| 75 | + public Float getObj() { |
| 76 | + return null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static void testScenario1() { |
| 81 | + verifyProperties(D1.class, |
| 82 | + "getClass", // inherited method |
| 83 | + "getValue", // inherited default method |
| 84 | + "getFoo", // overridden interface method |
| 85 | + "getObj" // overridden default method |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | +////////////////////////////////////// |
| 90 | +// // |
| 91 | +// SCENARIO 2 // |
| 92 | +// // |
| 93 | +////////////////////////////////////// |
| 94 | + |
| 95 | + public interface A2 { |
| 96 | + default Object getFoo() { |
| 97 | + return null; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public interface B2 extends A2 { |
| 102 | + } |
| 103 | + |
| 104 | + public interface C2 extends A2 { |
| 105 | + } |
| 106 | + |
| 107 | + public class D2 implements B2, C2 { |
| 108 | + } |
| 109 | + |
| 110 | + public static void testScenario2() { |
| 111 | + verifyProperties(D2.class, |
| 112 | + "getClass", |
| 113 | + "getFoo" |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | +////////////////////////////////////// |
| 118 | +// // |
| 119 | +// SCENARIO 3 // |
| 120 | +// // |
| 121 | +////////////////////////////////////// |
| 122 | + |
| 123 | + public interface A3 { |
| 124 | + default Object getFoo() { |
| 125 | + return null; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public interface B3 extends A3 { |
| 130 | + @Override |
| 131 | + Set<?> getFoo(); |
| 132 | + } |
| 133 | + |
| 134 | + public interface C3 extends A3 { |
| 135 | + @Override |
| 136 | + Collection<?> getFoo(); |
| 137 | + } |
| 138 | + |
| 139 | + public class D3 implements B3, C3 { |
| 140 | + @Override |
| 141 | + public NavigableSet<?> getFoo() { |
| 142 | + return null; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public static void testScenario3() { |
| 147 | + verifyProperties(D3.class, |
| 148 | + "getClass", |
| 149 | + "getFoo" |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | +// Helper methods |
| 154 | + |
| 155 | + public static void verifyProperties(Class<?> type, String... getterNames) { |
| 156 | + |
| 157 | + // Gather expected properties |
| 158 | + final HashSet<PropertyDescriptor> expected = new HashSet<>(); |
| 159 | + for (String methodName : getterNames) { |
| 160 | + final String suffix = methodName.substring(3); |
| 161 | + final String propName = Introspector.decapitalize(suffix); |
| 162 | + final Method getter; |
| 163 | + try { |
| 164 | + getter = type.getMethod(methodName); |
| 165 | + } catch (NoSuchMethodException e) { |
| 166 | + throw new Error("unexpected error", e); |
| 167 | + } |
| 168 | + final PropertyDescriptor propDesc; |
| 169 | + try { |
| 170 | + propDesc = new PropertyDescriptor(propName, getter, null); |
| 171 | + } catch (IntrospectionException e) { |
| 172 | + throw new Error("unexpected error", e); |
| 173 | + } |
| 174 | + expected.add(propDesc); |
| 175 | + } |
| 176 | + |
| 177 | + // Verify properties can be found directly |
| 178 | + expected.stream() |
| 179 | + .map(PropertyDescriptor::getName) |
| 180 | + .filter(name -> BeanUtils.getPropertyDescriptor(type, name) == null) |
| 181 | + .findFirst() |
| 182 | + .ifPresent(name -> { |
| 183 | + throw new Error("property \"" + name + "\" not found in " + type); |
| 184 | + }); |
| 185 | + |
| 186 | + // Gather actual properties |
| 187 | + final Set<PropertyDescriptor> actual = |
| 188 | + Set.of(BeanUtils.getPropertyDescriptors(type)); |
| 189 | + |
| 190 | + // Verify the two sets are the same |
| 191 | + if (!actual.equals(expected)) { |
| 192 | + throw new Error("mismatch: " + type |
| 193 | + + "\nACTUAL:\n " |
| 194 | + + actual.stream() |
| 195 | + .map(Object::toString) |
| 196 | + .collect(Collectors.joining("\n ")) |
| 197 | + + "\nEXPECTED:\n " |
| 198 | + + expected.stream() |
| 199 | + .map(Object::toString) |
| 200 | + .collect(Collectors.joining("\n "))); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | +// Main method |
| 205 | + |
| 206 | + public static void main(String[] args) throws Exception { |
| 207 | + testScenario1(); |
| 208 | + testScenario2(); |
| 209 | + testScenario3(); |
| 210 | + } |
| 211 | +} |
0 commit comments