Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/ClassDeallocGC/ClassDeallocGC.java
41155 views
1
/*
2
* Copyright (c) 2002, 2020, 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
*
27
* @summary converted from VM Testbase gc/gctests/ClassDeallocGC.
28
* VM Testbase keywords: [gc]
29
* VM Testbase readme:
30
* This test is a slightly modified version of a test taken from a research paper
31
* by CE McDowell of UCSC titled 'No so static "static fields"'.
32
* To quote from the paper :
33
* " In this paper we have identified a problem with the Java Language
34
* Specification with regard to class unloading and static fields. We have
35
* provided an example that demonstrates how the current implementation of
36
* class unloading can result in a static field being reinitialized resulting
37
* in a program that changes its behavior depending upon when garbage collection occurs."
38
* In this test, the creation of the object first assigned to
39
* class_one_object (actually an instance of ClassOne) also creates an instance
40
* of ClassTwo which contains a static field. Once the references to the ClassOne
41
* object and the Class object are set to null, a call to the garbage
42
* collector results in both ClassOne and ClassTwo being unloaded. When
43
* the final assignment to class_one_object occurs, creating a new instance of ClassOne
44
* and a new instance of ClassTwo, the program should print out the value of counter in
45
* ClassTwo as 2. This would mean that class was not unloaded and the static field
46
* was not reinitialized. If the test prints out 1, the test has failed as the static
47
* field was reinitilized.
48
*
49
* @library /vmTestbase
50
* /test/lib
51
* @run main/othervm gc.gctests.ClassDeallocGC.ClassDeallocGC
52
*/
53
54
package gc.gctests.ClassDeallocGC;
55
56
import nsk.share.TestFailure;
57
58
public class ClassDeallocGC
59
{
60
public static void main(String[] args)
61
throws java.io.IOException, ClassNotFoundException,
62
IllegalAccessException, InstantiationException
63
{
64
int count = 0, counter;
65
ClassOne class_one_object;
66
67
/* load the class ClassOne and instantiate an instance of it */
68
/* ClassOne uses ClassTwo which will thus get loaded also */
69
Class theClass = Class.forName("gc.gctests.ClassDeallocGC.ClassOne");
70
class_one_object = (ClassOne)theClass.newInstance();
71
72
/* remove all references to the class and the instance */
73
class_one_object = null;
74
theClass = null;
75
76
System.gc(); /* force garbage collection which also unloads classes */
77
78
/*loads and instantiates ClassOne again. ClassTwo also gets reloaded*/
79
class_one_object = (ClassOne) new ClassOne();
80
if ( (counter = class_one_object.getCounter()) == 2 ) {
81
System.out.println("Test Passed.");
82
} else {
83
throw new TestFailure("Test failed. counter = " + counter + ", should be 2.");
84
}
85
}
86
}
87
88
class ClassOne {
89
ClassTwo class_two;
90
public ClassOne(){
91
class_two = new ClassTwo();
92
}
93
public int getCounter() {
94
return class_two.getCounter();
95
}
96
}
97
98
99
class ClassTwo {
100
static int counter = 0;
101
public ClassTwo(){
102
counter++;
103
}
104
public int getCounter() {
105
return counter;
106
}
107
}
108
109