Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/management/MBeanInfo/TooManyFooTest.java
41152 views
1
/*
2
* Copyright (c) 2006, 2015, 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 6398884
27
* @summary Test that a method inherited from two different interfaces
28
* appears only once in MBeanInfo.
29
* @author dfuchs
30
*
31
* @run clean TooManyFooTest
32
* @run build TooManyFooTest
33
* @run main TooManyFooTest
34
*/
35
36
import java.lang.management.ManagementFactory;
37
import java.lang.reflect.Method;
38
import java.util.Arrays;
39
import java.util.HashMap;
40
import java.util.HashSet;
41
import java.util.Map;
42
import java.util.Set;
43
import java.util.logging.Logger;
44
import javax.management.Descriptor;
45
import javax.management.MBeanInfo;
46
import javax.management.MBeanOperationInfo;
47
import javax.management.MBeanServer;
48
import javax.management.ObjectName;
49
import javax.management.StandardMBean;
50
import javax.management.openmbean.OpenMBeanOperationInfo;
51
52
/**
53
* Class TooManyFooTest
54
* @author Sun Microsystems, 2005 - All rights reserved.
55
*/
56
public class TooManyFooTest {
57
58
public static class NumberHolder {
59
public Integer getNumber() { return 0;}
60
public void setNumber(Integer n) {};
61
}
62
public static class MyNumberHolder extends NumberHolder {
63
64
}
65
public interface Parent1 {
66
public int foo(); // Both in Parent1 and Parent2
67
public Integer barfoo(); // Subtype in Parent1, Super type in Parent2
68
public Long foobar(); // Subtype in Parent1 & MBean, Super type in
69
// Parent2
70
public Number toofoo(); // Subtype in Parent1, Super type in Parent2
71
// Concrete type in MBean
72
public Object toofoofoo(); // Super type in Parent1, Subtype in Parent2,
73
public NumberHolder toobarbar(); // toofoofoo reversed
74
}
75
76
public interface Parent2 {
77
public int foo(); // Both in Parent1 and Parent2
78
public Number barfoo();
79
public Number foobar();
80
public Object toofoo();
81
public NumberHolder toofoofoo();
82
public Object toobarbar();
83
}
84
85
public interface ChildMBean extends Parent1, Parent2 {
86
public Long foobar();
87
public Long toofoo();
88
}
89
90
public interface ChildMXBean extends Parent1, Parent2 {
91
public Long foobar();
92
public Long toofoo();
93
}
94
95
public interface ChildMixMXBean extends ChildMBean, ChildMXBean {
96
}
97
98
public static class Child implements ChildMBean {
99
public int foo() {return 0;}
100
public Long foobar() {return 0L;}
101
public Long toofoo() {return 0L;}
102
public Integer barfoo() {return 0;}
103
public MyNumberHolder toofoofoo() { return null;}
104
public MyNumberHolder toobarbar() { return null;}
105
}
106
107
public static class ChildMix implements ChildMXBean {
108
public int foo() {return 0;}
109
public Long foobar() {return 0L;}
110
public Long toofoo() {return 0L;}
111
public Integer barfoo() {return 0;}
112
public MyNumberHolder toofoofoo() { return null;}
113
public MyNumberHolder toobarbar() { return null;}
114
}
115
116
public static class ChildMixMix extends Child implements ChildMixMXBean {
117
}
118
119
120
/** Creates a new instance of TooManyFooTest */
121
public TooManyFooTest() {
122
}
123
124
private static final int OPCOUNT;
125
private static final Map<String,String> EXPECTED_TYPES;
126
private static final String[][] type_array = {
127
{ "foo", int.class.getName() },
128
{ "foobar", Long.class.getName()},
129
{ "toofoo", Long.class.getName()},
130
{ "barfoo", Integer.class.getName()},
131
{ "toofoofoo", NumberHolder.class.getName()},
132
{ "toobarbar", NumberHolder.class.getName()},
133
};
134
static {
135
try {
136
final Set<String> declared = new HashSet<String>();
137
for (Method m:Child.class.getDeclaredMethods()) {
138
declared.add(m.getName()+Arrays.asList(m.getParameterTypes()));
139
}
140
final Set<String> exposed = new HashSet<String>();
141
for (Method m:ChildMBean.class.getMethods()) {
142
exposed.add(m.getName()+Arrays.asList(m.getParameterTypes()));
143
}
144
declared.retainAll(exposed);
145
OPCOUNT = declared.size();
146
EXPECTED_TYPES = new HashMap<String,String>();
147
for (String[] st:type_array) {
148
EXPECTED_TYPES.put(st[0],st[1]);
149
}
150
} catch (Exception x) {
151
throw new ExceptionInInitializerError(x);
152
}
153
}
154
155
private static void test(Object child, String name, boolean mxbean)
156
throws Exception {
157
final ObjectName childName =
158
new ObjectName("test:type=Child,name="+name);
159
final MBeanServer server =
160
ManagementFactory.getPlatformMBeanServer();
161
server.registerMBean(child,childName);
162
try {
163
final MBeanInfo info = server.getMBeanInfo(childName);
164
System.out.println(name+": " + info.getDescriptor());
165
final int len = info.getOperations().length;
166
if (len == OPCOUNT) {
167
System.out.println(name+": OK, only "+OPCOUNT+
168
" operations here...");
169
} else {
170
final String qual = (len>OPCOUNT)?"many":"few";
171
System.err.println(name+": Too "+qual+" foos! Found "+
172
len+", expected "+OPCOUNT);
173
for (MBeanOperationInfo op : info.getOperations()) {
174
System.err.println("public "+op.getReturnType()+" "+
175
op.getName()+"();");
176
}
177
throw new RuntimeException("Too " + qual +
178
" foos for "+name);
179
}
180
181
final Descriptor d = info.getDescriptor();
182
final String mxstr = String.valueOf(d.getFieldValue("mxbean"));
183
final boolean mxb =
184
(mxstr==null)?false:Boolean.valueOf(mxstr).booleanValue();
185
System.out.println(name+": mxbean="+mxb);
186
if (mxbean && !mxb)
187
throw new AssertionError("MXBean is not OpenMBean?");
188
189
for (MBeanOperationInfo mboi : info.getOperations()) {
190
191
// Sanity check
192
if (mxbean && !mboi.getName().equals("foo")) {
193
// The spec doesn't guarantee that the MBeanOperationInfo
194
// of an MXBean will be an OpenMBeanOperationInfo, and in
195
// some circumstances in our implementation it will not.
196
// However, in thsi tests, for all methods but foo(),
197
// it should.
198
//
199
if (!(mboi instanceof OpenMBeanOperationInfo))
200
throw new AssertionError("Operation "+mboi.getName()+
201
"() is not Open?");
202
}
203
204
final String exp = EXPECTED_TYPES.get(mboi.getName());
205
206
// For MXBeans, we need to compare 'exp' with the original
207
// type - because mboi.getReturnType() returns the OpenType
208
//
209
String type = (String)mboi.getDescriptor().
210
getFieldValue("originalType");
211
if (type == null) type = mboi.getReturnType();
212
if (type.equals(exp)) continue;
213
System.err.println("Bad return type for "+
214
mboi.getName()+"! Found "+type+
215
", expected "+exp);
216
throw new RuntimeException("Bad return type for "+
217
mboi.getName());
218
}
219
} finally {
220
server.unregisterMBean(childName);
221
}
222
}
223
224
public static void main(String[] args) throws Exception {
225
final Child child = new Child();
226
test(child,"Child[MBean]",false);
227
final ChildMix childx = new ChildMix();
228
test(childx,"ChildMix[MXBean]",true);
229
final ChildMixMix childmx = new ChildMixMix();
230
test(childmx,"ChildMixMix[MXBean]",false);
231
final StandardMBean schild = new StandardMBean(child,ChildMBean.class);
232
test(schild,"Child[StandarMBean(Child)]",false);
233
final StandardMBean schildx =
234
new StandardMBean(childx,ChildMXBean.class,true);
235
test(schildx,"ChildMix[StandarMXBean(ChildMix)]",true);
236
final StandardMBean schildmx =
237
new StandardMBean(childmx,ChildMixMXBean.class,true);
238
test(schildmx,"ChildMixMix[StandarMXBean(ChildMixMix)]",true);
239
}
240
241
}
242
243