1 /*
2  * Copyright (c) 2004, 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 package nsk.jvmti.scenarios.allocation.AP03;
25 
26 import java.io.*;
27 import java.lang.reflect.*;
28 
29 import nsk.share.*;
30 import nsk.share.jvmti.*;
31 
32 public class ap03t001 extends DebugeeClass {
33     /* number of interations to provoke garbage collecting */
34     final static int GC_TRIES = 1;
35 
main(String[] argv)36     public static void main(String[] argv) {
37         argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
38 
39         // produce JCK-like exit status
40         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
41     }
42 
run(String argv[], PrintStream out)43     public static int run(String argv[], PrintStream out) {
44         return new ap03t001().runThis(argv, out);
45     }
46 
47     private static ap03t001 catcher = null;
48     private static Wicket wicket = new Wicket();
49 
setTag(long tag)50     private native void setTag(long tag);
51 
52     /* scaffold objects */
53     static ArgumentHandler argHandler = null;
54     static Log log = null;
55     long timeout = 0;
56     int status = Consts.TEST_PASSED;
57 
runThis(String argv[], PrintStream out)58     private int runThis(String argv[], PrintStream out) {
59         argHandler = new ArgumentHandler(argv);
60         log = new Log(out, argHandler);
61         timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds
62 
63         ap03t001 holder = new ap03t001();
64         log.display("Set tag for tested object");
65         holder.setTag(1L);
66         // make an object collectable
67         holder = null;
68 
69         // provoke GC and finalization
70         for (int i= 0; i < GC_TRIES; i++) {
71             System.gc();
72             Runtime.getRuntime().runFinalization();
73         }
74 
75         wicket.waitFor(timeout);
76         log.display("Sync point: GC and Finalization have been called");
77 
78         if (catcher == null) {
79             log.complain("Tested object has not been catched during finalization");
80         }
81 
82         status = checkStatus(status);
83         return status;
84     }
85 
finalize()86     protected void finalize() throws Throwable {
87         log.display("finalize() has been invoked");
88         // restore strong reference to finalized object
89         catcher = this;
90         wicket.unlock();
91     }
92 }
93