Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/beans/Introspector/OverriddenGenericSetter.java
41149 views
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 setter method
33
*/
34
public final class OverriddenGenericSetter {
35
36
static class Parent<T> {
37
private T value;
38
public final T getValue() {return value;}
39
protected void setValue(T value) {this.value = value;}
40
}
41
42
static class ChildO extends Parent<Object> {
43
public ChildO() {}
44
@Override
45
public void setValue(Object value) {super.setValue(value);}
46
}
47
48
// For overridden setXXX javac will generate the "synthetic bridge" method
49
// setValue(Ljava/lang/Object). We will use two different types which may be
50
// placed before/after bridge method.
51
static class ChildA extends Parent<ArithmeticException> {
52
public ChildA() {}
53
@Override
54
public void setValue(ArithmeticException value) {super.setValue(value);}
55
}
56
57
static class ChildS extends Parent<String> {
58
public ChildS() {}
59
@Override
60
public void setValue(String value) {super.setValue(value);}
61
}
62
63
public static void main(String[] args) throws Exception {
64
testBehaviour(ChildA.class);
65
testBehaviour(ChildO.class);
66
testBehaviour(ChildS.class);
67
testProperty(ChildA.class, ArithmeticException.class);
68
testProperty(ChildO.class, Object.class);
69
testProperty(ChildS.class, String.class);
70
}
71
72
private static void testBehaviour(Class<?> beanClass) throws Exception {
73
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);
74
PropertyDescriptor pd = beanInfo.getPropertyDescriptors()[0];
75
Object bean = beanClass.getConstructor().newInstance();
76
Method readMethod = pd.getReadMethod();
77
Method writeMethod = pd.getWriteMethod();
78
79
// check roundtrip for default null values
80
writeMethod.invoke(bean,readMethod.invoke(bean));
81
writeMethod.invoke(bean,readMethod.invoke(bean));
82
83
// set property to non-default value
84
// check roundtrip for non-default values
85
Object param = pd.getPropertyType().getConstructor().newInstance();
86
writeMethod.invoke(bean, param);
87
writeMethod.invoke(bean, readMethod.invoke(bean));
88
writeMethod.invoke(bean, readMethod.invoke(bean));
89
}
90
91
private static void testProperty(Class<?> beanClass, Class<?> type) throws Exception {
92
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);
93
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
94
if (pds.length != 1) {
95
throw new RuntimeException("Wrong number of properties");
96
}
97
PropertyDescriptor pd = pds[0];
98
System.out.println("pd = " + pd);
99
String name = pd.getName();
100
if (!name.equals("value")) {
101
throw new RuntimeException("Wrong name: " + name);
102
}
103
Class<?> propertyType = pd.getPropertyType();
104
if (propertyType != type) {
105
throw new RuntimeException("Wrong property type: " + propertyType);
106
}
107
Method readMethod = pd.getReadMethod();
108
if (readMethod == null) {
109
throw new RuntimeException("Read method is null");
110
}
111
Class<?> returnType = readMethod.getReturnType();
112
if (returnType != Object.class) {
113
throw new RuntimeException("Wrong return type; " + returnType);
114
}
115
Method writeMethod = pd.getWriteMethod();
116
if (writeMethod == null) {
117
throw new RuntimeException("Write method is null");
118
}
119
Class<?>[] parameterTypes = writeMethod.getParameterTypes();
120
if (parameterTypes.length != 1) {
121
throw new RuntimeException("Wrong parameters " + parameterTypes);
122
}
123
if (parameterTypes[0] != type) {
124
throw new RuntimeException("Wrong type: " + parameterTypes[0]);
125
}
126
}
127
}
128
129