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