1 /*
  2  * Copyright (c) 2025, 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  * @summary Sanity test for ArrayReference (getValue/setValue) with flat arrays
 27  *
 28  * @modules java.base/jdk.internal.value
 29  * @library ..
 30  * @enablePreview
 31  * @run main/othervm ValueArrayReferenceTest
 32  *                   --add-modules java.base --add-exports java.base/jdk.internal.value=ALL-UNNAMED
 33  */
 34 import com.sun.jdi.ArrayReference;
 35 import com.sun.jdi.Field;
 36 import com.sun.jdi.ReferenceType;
 37 import com.sun.jdi.Value;
 38 import com.sun.jdi.event.BreakpointEvent;
 39 import java.util.ArrayList;
 40 import java.util.List;
 41 import java.util.stream.Collectors;
 42 import jdk.internal.value.ValueClass;
 43 
 44 class ValueArrayReferenceTarg {
 45     static value class Value {
 46         int v;
 47         Value() {
 48             this(0);
 49         }
 50         Value(int v) {
 51             this.v = v;
 52         }
 53         public int getValue() {
 54             return v;
 55         }
 56     }
 57 
 58     static int ARRAY_SIZE = 5;
 59     static void initArray(Value[] arr) {
 60         for (int i = 0; i < arr.length; i++) {
 61             arr[i] = new Value(i);
 62         }
 63     }
 64 
 65     static Value[] testRegularArray;
 66     static Value[] testNullableAtomicArray;
 67     static Value[] testNonNullNonAtomicArray;
 68     static Value[] testNonNullAtomicArray;
 69 
 70     static Value otherValue = new Value(25);
 71 
 72     static {
 73         try {
 74             testRegularArray = new Value[ARRAY_SIZE];
 75             initArray(testRegularArray);
 76 
 77             testNullableAtomicArray = (Value[])ValueClass.newNullableAtomicArray(Value.class, ARRAY_SIZE);
 78             initArray(testNullableAtomicArray);
 79 
 80             testNonNullNonAtomicArray = (Value[])ValueClass.newNullRestrictedNonAtomicArray(Value.class, ARRAY_SIZE, Value.class.newInstance());
 81             initArray(testNonNullNonAtomicArray);
 82 
 83             testNonNullAtomicArray = (Value[])ValueClass.newNullRestrictedAtomicArray(Value.class, ARRAY_SIZE, Value.class.newInstance());
 84             initArray(testNonNullAtomicArray);
 85         } catch (Exception ex) {
 86             throw new RuntimeException(ex);
 87         }
 88     }
 89 
 90     public static void main(String[] args) {
 91         System.out.println("Hello and goodbye from main");
 92     }
 93 }
 94 
 95 public class ValueArrayReferenceTest extends TestScaffold {
 96 
 97     ValueArrayReferenceTest (String args[]) {
 98         super(args);
 99     }
100 
101     public static void main(String[] args) throws Exception {
102         new ValueArrayReferenceTest(args).startTests();
103     }
104 
105     String arrayToString(ArrayReference array) {
106         List<Value> values = array.getValues();
107         // Mirror.toString reports object type and reference id,
108         // it should be enough to see if objects are different.
109         return values.stream()
110                 .map(String::valueOf)
111                 .collect(Collectors.joining(", ", "[", "]"));
112     }
113 
114     Value getFieldValue(ReferenceType cls, String fieldName) {
115         System.out.println("Getting value from " + fieldName);
116         Value value = cls.getValue(cls.fieldByName(fieldName));
117         System.out.println(" - " + value);
118         return value;
119     }
120 
121     ArrayReference getArrayFromField(ReferenceType cls, Field field) throws Exception {
122         System.out.println("Getting array from " + field.name());
123         ArrayReference array = (ArrayReference)cls.getValue(field);
124         System.out.println(" - " + array);
125         System.out.println("   " + arrayToString(array));
126         return array;
127     }
128 
129     boolean arraysEquals(ArrayReference arr1, ArrayReference arr2) throws Exception {
130         // Compare string representation of the array (contains object type and id for each element).
131         String s1 = arrayToString(arr1);
132         String s2 = arrayToString(arr2);
133         return s1.equals(s2);
134     }
135 
136     void fillArrayWithOtherValue(ArrayReference arr, Value value) throws Exception {
137         for (int i = 0; i < arr.length(); i++) {
138             arr.setValue(i, value);
139         }
140     }
141 
142     void verifyArraysEqual(List<ArrayReference> arrays) throws Exception {
143         // Compare 1st and 2nd, 2nd and 3rd, etc.
144         for (int i = 1; i < arrays.size(); i++) { // start from 1
145             ArrayReference arr1 = arrays.get(i - 1);
146             ArrayReference arr2 = arrays.get(i);
147             if (!arraysEquals(arr1, arr2)) {
148                 System.out.println("Arrays are different (" + (i - 1) + " and " + i + "):"
149                                  + "\n    - " + arrayToString(arr1)
150                                  + "\n    - " + arrayToString(arr2));
151                 throw new RuntimeException("Arrays are different");
152             }
153         }
154     }
155 
156     protected void runTests() throws Exception {
157         try {
158             BreakpointEvent bpe = startToMain("ValueArrayReferenceTarg");
159             ReferenceType cls = bpe.location().declaringType();
160 
161             // Get all arrays.
162             List<ArrayReference> arrays = new ArrayList<>();
163             List<Field> fields = cls.allFields();
164             for (Field field: fields) {
165                 if (field.name().startsWith("test")) {
166                     arrays.add(getArrayFromField(cls, field));
167                 }
168             }
169 
170             // Ensure elements in all arrays are equal.
171             verifyArraysEqual(arrays);
172 
173             // Update arrays.
174             Value otherValue = getFieldValue(cls, "otherValue");
175             for (ArrayReference arr: arrays) {
176                 fillArrayWithOtherValue(arr, otherValue);
177                 System.out.println("Array after update:");
178                 System.out.println("   " + arrayToString(arr));
179             }
180 
181             // Ensure elements in all arrays are equal.
182             verifyArraysEqual(arrays);
183         } finally {
184             // Resume the target until end.
185             listenUntilVMDisconnect();
186         }
187     }
188 }