|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 8317636 |
| 27 | + * @summary The test verifies heap walking API (FollowReferences) reports |
| 28 | + * field indeces in correct order (as described by jvmtiHeapReferenceInfoField spec). |
| 29 | + * For simplification only primitive field callback is tested |
| 30 | + * and all fields in the test classes are 'int'. |
| 31 | + * Field IDs are not reported to the callback, so the test uses field values |
| 32 | + * to distinguish between fields, so all field values in the test classes should be unique. |
| 33 | + * @run main/othervm/native -agentlib:FieldIndicesTest FieldIndicesTest |
| 34 | + */ |
| 35 | + |
| 36 | +import java.lang.ref.Reference; |
| 37 | + |
| 38 | +// Test class hierarchy is based on the example described in the spec. |
| 39 | +// Extra fields added to improve coverage. |
| 40 | +interface I0 { |
| 41 | + int p = 10; |
| 42 | + // extra fields |
| 43 | + public int p5 = 11; |
| 44 | + int p6 = 12; |
| 45 | + public int p1 = 13; |
| 46 | +} |
| 47 | + |
| 48 | +interface I1 extends I0 { |
| 49 | + int x = 20; |
| 50 | + // extra fields |
| 51 | + int x1 = 21; |
| 52 | + public int x2 = 22; |
| 53 | +} |
| 54 | + |
| 55 | +interface I2 extends I0 { |
| 56 | + int y = 30; |
| 57 | + // extra fields |
| 58 | + int y9 = 31; |
| 59 | + public int y4 = 32; |
| 60 | + public int y3 = 33; |
| 61 | +} |
| 62 | + |
| 63 | +class C1 implements I1 { |
| 64 | + public static int a = 40; |
| 65 | + private int b = 41; |
| 66 | + // extra fields |
| 67 | + private int a1 = 42; |
| 68 | + protected static int b7 = 43; |
| 69 | + static int b2 = 44; |
| 70 | + final protected int a3 = 45; |
| 71 | + static int a2 = 46; |
| 72 | + public int b1 = 47; |
| 73 | +} |
| 74 | + |
| 75 | +class C2 extends C1 implements I2 { |
| 76 | + static int q = 60; |
| 77 | + final int r = 61; |
| 78 | + // extra fields |
| 79 | + private int q11 = 61; |
| 80 | + final static int q9 = 62; |
| 81 | + static int q2 = 63; |
| 82 | + final protected int r3 = 64; |
| 83 | + public int r7 = 65; |
| 84 | +} |
| 85 | + |
| 86 | +public class FieldIndicesTest { |
| 87 | + static { |
| 88 | + System.loadLibrary("FieldIndicesTest"); |
| 89 | + } |
| 90 | + |
| 91 | + private static native void prepare(Object testObject); |
| 92 | + |
| 93 | + private static native void test(Object rootObject); |
| 94 | + |
| 95 | + private static native boolean testFailed(); |
| 96 | + |
| 97 | + private static void prepare(String name, Object testObject) { |
| 98 | + System.out.println(">>prepare(" + name + ")"); |
| 99 | + prepare(testObject); |
| 100 | + System.out.println("<<prepare(" + name + ")"); |
| 101 | + System.out.println(); |
| 102 | + } |
| 103 | + |
| 104 | + private static void test(String name, Object rootObject) { |
| 105 | + System.out.println(">>test(" + name + ")"); |
| 106 | + test(rootObject); |
| 107 | + System.out.println("<<test(" + name + ")"); |
| 108 | + System.out.println(); |
| 109 | + } |
| 110 | + |
| 111 | + public static void main(String argv[]) { |
| 112 | + C1 obj1 = new C1(); |
| 113 | + C2 obj2 = new C2(); |
| 114 | + |
| 115 | + prepare("obj1", obj1); |
| 116 | + prepare("obj2", obj2); |
| 117 | + |
| 118 | + test("obj1", obj1); |
| 119 | + test("obj2", obj2); |
| 120 | + |
| 121 | + Reference.reachabilityFence(obj1); |
| 122 | + Reference.reachabilityFence(obj2); |
| 123 | + |
| 124 | + if (testFailed()) { |
| 125 | + throw new RuntimeException("Test failed. See log for details"); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments