Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/reflect/ReflectionFactory/ReflectionFactoryTest.java
41149 views
1
/*
2
* Copyright (c) 2016, 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.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.Externalizable;
27
import java.io.IOException;
28
import java.io.ObjectInput;
29
import java.io.ObjectInputStream;
30
import java.io.ObjectOutput;
31
import java.io.ObjectOutputStream;
32
import java.io.OptionalDataException;
33
import java.io.Serializable;
34
import java.lang.invoke.MethodHandle;
35
import java.lang.reflect.Constructor;
36
import java.lang.reflect.InvocationTargetException;
37
38
import sun.reflect.ReflectionFactory;
39
40
import org.testng.Assert;
41
import org.testng.annotations.BeforeClass;
42
import org.testng.annotations.Test;
43
import org.testng.annotations.DataProvider;
44
import org.testng.TestNG;
45
46
/*
47
* @test
48
* @bug 8137058 8164908 8168980
49
* @run testng ReflectionFactoryTest
50
* @run testng/othervm/policy=security.policy ReflectionFactoryTest
51
* @summary Basic test for the unsupported ReflectionFactory
52
* @modules jdk.unsupported
53
*/
54
55
public class ReflectionFactoryTest {
56
57
// Initialized by init()
58
static ReflectionFactory factory;
59
60
@DataProvider(name = "ClassConstructors")
61
static Object[][] classConstructors() {
62
return new Object[][] {
63
{Object.class},
64
{Foo.class},
65
{Bar.class},
66
};
67
}
68
69
@BeforeClass
70
static void init() {
71
factory = ReflectionFactory.getReflectionFactory();
72
}
73
74
/**
75
* Test that the correct Constructor is selected and run.
76
* @param type type of object to create
77
* @throws NoSuchMethodException - error
78
* @throws InstantiationException - error
79
* @throws IllegalAccessException - error
80
* @throws InvocationTargetException - error
81
*/
82
@Test(dataProvider="ClassConstructors")
83
static void testConstructor(Class<?> type)
84
throws NoSuchMethodException, InstantiationException,
85
IllegalAccessException, InvocationTargetException
86
{
87
@SuppressWarnings("unchecked")
88
Constructor<?> c = factory.newConstructorForSerialization(type);
89
90
Object o = c.newInstance();
91
Assert.assertEquals(o.getClass(), type, "Instance is wrong type");
92
if (o instanceof Foo) {
93
Foo foo = (Foo)o;
94
foo.check();
95
}
96
}
97
98
@DataProvider(name = "NonSerialConstructors")
99
static Object[][] constructors() throws NoSuchMethodException {
100
return new Object[][] {
101
{Foo.class, Object.class.getDeclaredConstructor()},
102
{Foo.class, Foo.class.getDeclaredConstructor()},
103
{Baz.class, Object.class.getDeclaredConstructor()},
104
{Baz.class, Foo.class.getDeclaredConstructor()},
105
{Baz.class, Baz.class.getDeclaredConstructor()}
106
};
107
}
108
109
/**
110
* Tests that the given Constructor, in the hierarchy, is run.
111
*/
112
@Test(dataProvider="NonSerialConstructors")
113
static void testNonSerializableConstructor(Class<?> cl,
114
Constructor<?> constructorToCall)
115
throws ReflectiveOperationException
116
{
117
@SuppressWarnings("unchecked")
118
Constructor<?> c = factory.newConstructorForSerialization(cl,
119
constructorToCall);
120
121
Object o = c.newInstance();
122
Assert.assertEquals(o.getClass(), cl, "Instance is wrong type");
123
124
int expectedFoo = 0;
125
int expectedBaz = 0;
126
if (constructorToCall.getName().equals("ReflectionFactoryTest$Foo")) {
127
expectedFoo = 1;
128
} else if (constructorToCall.getName().equals("ReflectionFactoryTest$Baz")) {
129
expectedFoo = 1;
130
expectedBaz = 4;
131
}
132
133
Assert.assertEquals(((Foo)o).foo(), expectedFoo);
134
if (o instanceof Baz) {
135
Assert.assertEquals(((Baz)o).baz(), expectedBaz);
136
}
137
}
138
139
static class Foo {
140
private int foo;
141
public Foo() {
142
this.foo = 1;
143
}
144
145
public String toString() {
146
return "foo: " + foo;
147
}
148
149
public void check() {
150
int expectedFoo = 1;
151
Assert.assertEquals(foo, expectedFoo, "foo() constructor not run");
152
}
153
154
public int foo() { return foo; }
155
}
156
157
static class Bar extends Foo implements Serializable {
158
private int bar;
159
public Bar() {
160
this.bar = 1;
161
}
162
163
public String toString() {
164
return super.toString() + ", bar: " + bar;
165
}
166
167
public void check() {
168
super.check();
169
int expectedBar = 0;
170
Assert.assertEquals(bar, expectedBar, "bar() constructor not run");
171
}
172
}
173
174
static class Baz extends Foo {
175
private final int baz;
176
public Baz() { this.baz = 4; }
177
public int baz() { return baz; }
178
}
179
180
/**
181
* Test newConstructorForExternalization returns the constructor and it can be called.
182
* @throws NoSuchMethodException - error
183
* @throws InstantiationException - error
184
* @throws IllegalAccessException - error
185
* @throws InvocationTargetException - error
186
*/
187
@Test
188
static void newConstructorForExternalization()
189
throws NoSuchMethodException, InstantiationException,
190
IllegalAccessException, InvocationTargetException {
191
Constructor<?> cons = factory.newConstructorForExternalization(Ext.class);
192
Ext ext = (Ext)cons.newInstance();
193
Assert.assertEquals(ext.ext, 1, "Constructor not run");
194
}
195
196
static class Ext implements Externalizable {
197
private static final long serialVersionUID = 1L;
198
199
int ext;
200
201
public Ext() {
202
ext = 1;
203
}
204
205
@Override
206
public void writeExternal(ObjectOutput out) throws IOException {}
207
208
@Override
209
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {}
210
}
211
212
@Test
213
static void testReadWriteObjectForSerialization() throws Throwable {
214
MethodHandle readObjectMethod = factory.readObjectForSerialization(Ser.class);
215
Assert.assertNotNull(readObjectMethod, "readObjectMethod not found");
216
217
MethodHandle readObjectNoDataMethod = factory.readObjectNoDataForSerialization(Ser.class);
218
Assert.assertNotNull(readObjectNoDataMethod, "readObjectNoDataMethod not found");
219
220
MethodHandle writeObjectMethod = factory.writeObjectForSerialization(Ser.class);
221
Assert.assertNotNull(writeObjectMethod, "writeObjectMethod not found");
222
223
MethodHandle readResolveMethod = factory.readResolveForSerialization(Ser.class);
224
Assert.assertNotNull(readResolveMethod, "readResolveMethod not found");
225
226
MethodHandle writeReplaceMethod = factory.writeReplaceForSerialization(Ser.class);
227
Assert.assertNotNull(writeReplaceMethod, "writeReplaceMethod not found");
228
229
byte[] data = null;
230
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
231
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
232
Ser ser = new Ser();
233
234
writeReplaceMethod.invoke(ser);
235
Assert.assertTrue(ser.writeReplaceCalled, "writeReplace not called");
236
Assert.assertFalse(ser.writeObjectCalled, "writeObject should not have been called");
237
238
writeObjectMethod.invoke(ser, oos);
239
Assert.assertTrue(ser.writeReplaceCalled, "writeReplace should have been called");
240
Assert.assertTrue(ser.writeObjectCalled, "writeObject not called");
241
oos.flush();
242
data = baos.toByteArray();
243
}
244
245
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
246
ObjectInputStream ois = new ObjectInputStream(bais)) {
247
Ser ser2 = new Ser();
248
249
readObjectMethod.invoke(ser2, ois);
250
Assert.assertTrue(ser2.readObjectCalled, "readObject not called");
251
Assert.assertFalse(ser2.readObjectNoDataCalled, "readObjectNoData should not be called");
252
Assert.assertFalse(ser2.readResolveCalled, "readResolve should not be called");
253
254
readObjectNoDataMethod.invoke(ser2, ois);
255
Assert.assertTrue(ser2.readObjectCalled, "readObject should have been called");
256
Assert.assertTrue(ser2.readObjectNoDataCalled, "readObjectNoData not called");
257
Assert.assertFalse(ser2.readResolveCalled, "readResolve should not be called");
258
259
readResolveMethod.invoke(ser2);
260
Assert.assertTrue(ser2.readObjectCalled, "readObject should have been called");
261
Assert.assertTrue(ser2.readObjectNoDataCalled, "readObjectNoData not called");
262
Assert.assertTrue(ser2.readResolveCalled, "readResolve not called");
263
}
264
}
265
266
@Test
267
static void hasStaticInitializer() {
268
boolean actual = factory.hasStaticInitializerForSerialization(Ser.class);
269
Assert.assertTrue(actual, "hasStaticInitializerForSerialization is wrong");
270
}
271
272
static class Ser implements Serializable {
273
private static final long serialVersionUID = 2L;
274
static {
275
// Define a static class initialization method
276
}
277
278
boolean readObjectCalled = false;
279
boolean readObjectNoDataCalled = false;
280
boolean writeObjectCalled = false;
281
boolean readResolveCalled = false;
282
boolean writeReplaceCalled = false;
283
284
public Ser() {}
285
286
private void readObject(ObjectInputStream ois) throws IOException {
287
Assert.assertFalse(writeObjectCalled, "readObject called too many times");
288
readObjectCalled = ois.readBoolean();
289
}
290
291
private void readObjectNoData(ObjectInputStream ois) throws IOException {
292
Assert.assertFalse(readObjectNoDataCalled, "readObjectNoData called too many times");
293
readObjectNoDataCalled = true;
294
}
295
296
private void writeObject(ObjectOutputStream oos) throws IOException {
297
Assert.assertFalse(writeObjectCalled, "writeObject called too many times");
298
writeObjectCalled = true;
299
oos.writeBoolean(writeObjectCalled);
300
}
301
302
private Object writeReplace() {
303
Assert.assertFalse(writeReplaceCalled, "writeReplace called too many times");
304
writeReplaceCalled = true;
305
return this;
306
}
307
308
private Object readResolve() {
309
Assert.assertFalse(readResolveCalled, "readResolve called too many times");
310
readResolveCalled = true;
311
return this;
312
}
313
}
314
315
/**
316
* Test the constructor of OptionalDataExceptions.
317
*/
318
@Test
319
static void newOptionalDataException() {
320
OptionalDataException ode = factory.newOptionalDataExceptionForSerialization(true);
321
Assert.assertTrue(ode.eof, "eof wrong");
322
ode = factory.newOptionalDataExceptionForSerialization(false);
323
Assert.assertFalse(ode.eof, "eof wrong");
324
325
}
326
327
328
329
// Main can be used to run the tests from the command line with only testng.jar.
330
@SuppressWarnings("raw_types")
331
@Test(enabled = false)
332
public static void main(String[] args) {
333
Class<?>[] testclass = {ReflectionFactoryTest.class};
334
TestNG testng = new TestNG();
335
testng.setTestClasses(testclass);
336
testng.run();
337
}
338
}
339
340