Skip to content

Commit 878ceeb

Browse files
committed
8071693: Introspector ignores default interface methods
Backport-of: 1e4eafb4fe70832294a12938d93e7860073cf4cf
1 parent 2886a39 commit 878ceeb

File tree

2 files changed

+247
-4
lines changed

2 files changed

+247
-4
lines changed

src/java.desktop/share/classes/com/sun/beans/introspect/MethodInfo.java

+36-4
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,35 @@
2525

2626
package com.sun.beans.introspect;
2727

28+
import java.io.Closeable;
29+
import java.io.Externalizable;
30+
import java.io.Serializable;
2831
import java.lang.reflect.Method;
2932
import java.lang.reflect.Modifier;
3033
import java.lang.reflect.Type;
3134
import java.util.ArrayList;
35+
import java.util.Arrays;
3236
import java.util.Collections;
3337
import java.util.Comparator;
3438
import java.util.List;
39+
import java.util.Set;
3540

3641
import com.sun.beans.TypeResolver;
3742
import com.sun.beans.finder.MethodFinder;
3843

3944
final class MethodInfo {
45+
46+
// These are some common interfaces that we know a priori
47+
// will not contain any bean property getters or setters.
48+
static final Set<Class<?>> IGNORABLE_INTERFACES = Set.of(
49+
AutoCloseable.class,
50+
Cloneable.class,
51+
Closeable.class,
52+
Comparable.class,
53+
Externalizable.class,
54+
Serializable.class
55+
);
56+
4057
final Method method;
4158
final Class<?> type;
4259

@@ -66,6 +83,8 @@ static Class<?> resolve(Method method, Type type) {
6683
static List<Method> get(Class<?> type) {
6784
List<Method> list = null;
6885
if (type != null) {
86+
87+
// Add declared methods
6988
boolean inaccessible = !Modifier.isPublic(type.getModifiers());
7089
for (Method method : type.getMethods()) {
7190
if (method.getDeclaringClass().equals(type)) {
@@ -81,10 +100,19 @@ static List<Method> get(Class<?> type) {
81100
}
82101
}
83102
if (method != null) {
84-
if (list == null) {
85-
list = new ArrayList<>();
86-
}
87-
list.add(method);
103+
(list = createIfNeeded(list)).add(method);
104+
}
105+
}
106+
}
107+
108+
// Add default methods inherited from interfaces
109+
for (Class<?> iface : type.getInterfaces()) {
110+
if (IGNORABLE_INTERFACES.contains(iface)) {
111+
continue;
112+
}
113+
for (Method method : iface.getMethods()) {
114+
if (!Modifier.isAbstract(method.getModifiers())) {
115+
(list = createIfNeeded(list)).add(method);
88116
}
89117
}
90118
}
@@ -96,6 +124,10 @@ static List<Method> get(Class<?> type) {
96124
return Collections.emptyList();
97125
}
98126

127+
private static List<Method> createIfNeeded(List<Method> list) {
128+
return list != null ? list : new ArrayList<>();
129+
}
130+
99131
/**
100132
* A comparator that defines a total order so that methods have the same
101133
* name and identical signatures appear next to each others.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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

Comments
 (0)