1 /*
  2  * Copyright (c) 2026, 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 import java.util.Arrays;
 26 import jdk.internal.value.ValueClass;
 27 
 28 public class ArchivedFlatArrayApp {
 29 
 30     // Check that arrays of both migrated value
 31     // classes and custom value classes are archived
 32     public static class ArchivedData {
 33         Integer[] intArray;
 34         CharPair[] charPairArray;
 35         Wrapper[] wrapperArray;
 36     }
 37 
 38     public static value class CharPair implements Comparable<CharPair> {
 39         char c0, c1;
 40 
 41         public String toString() {
 42             return "(" + c0 + ", " + c1 + ")";
 43         }
 44 
 45         public int compareTo(CharPair o) {
 46             return (c0 - o.c0) - (c1 - o.c1);
 47         }
 48 
 49         public CharPair(char c0, char c1) {
 50             this.c0 = c0;
 51             this.c1 = c1;
 52         }
 53     }
 54 
 55     public static value class Wrapper implements Comparable<Wrapper> {
 56         Integer i;
 57 
 58         public String toString() {
 59             return i.toString();
 60         }
 61 
 62         public int compareTo(Wrapper o) {
 63             return i - o.i;
 64         }
 65 
 66         Wrapper(int i) {
 67             this.i = new Integer(i);
 68         }
 69     }
 70 
 71     static ArchivedData archivedObjects;
 72     static boolean restored;
 73     static {
 74         if (archivedObjects == null) {
 75             restored = false;
 76             System.out.println("Not archived");
 77             archivedObjects = new ArchivedData();
 78 
 79             archivedObjects.intArray = new Integer[3];
 80             archivedObjects.intArray[0] = new Integer(0);
 81             archivedObjects.intArray[1] = new Integer(1);
 82             archivedObjects.intArray[2] = new Integer(2);
 83 
 84             archivedObjects.charPairArray = new CharPair[3];
 85             archivedObjects.charPairArray[0] = new CharPair('a', 'b');
 86             archivedObjects.charPairArray[1] = new CharPair('c', 'd');
 87             archivedObjects.charPairArray[2] = new CharPair('e', 'f');
 88 
 89             archivedObjects.wrapperArray = new Wrapper[3];
 90             archivedObjects.wrapperArray[0] = new Wrapper(0);
 91             archivedObjects.wrapperArray[1] = new Wrapper(1);
 92             archivedObjects.wrapperArray[2] = new Wrapper(2);
 93         } else {
 94             restored = true;
 95             System.out.println("Initialized from CDS");
 96             System.out.println("intArray " + archivedObjects.intArray);
 97             System.out.println("charPairArray " + archivedObjects.charPairArray);
 98             System.out.println("wrapperArray " + archivedObjects.wrapperArray);
 99         }
100 
101         for (Integer i : archivedObjects.intArray) {
102             System.out.println(i);
103         }
104 
105         for (CharPair c : archivedObjects.charPairArray) {
106             System.out.println(c);
107         }
108 
109         for (Wrapper w : archivedObjects.wrapperArray) {
110             System.out.println(w);
111         }
112     }
113 
114     public static void main(String[] args) {
115         if (!ValueClass.isFlatArray(archivedObjects.intArray)) {
116             throw new RuntimeException("Integer array should be flat");
117         }
118 
119         if (!ValueClass.isFlatArray(archivedObjects.charPairArray)) {
120             throw new RuntimeException("CharPair array should be flat");
121         }
122 
123         if (!ValueClass.isFlatArray(archivedObjects.wrapperArray)) {
124             throw new RuntimeException("Wrapper array should be flat");
125         }
126 
127         if (restored) {
128             // Ensure archived arrays are restored properly
129             Integer[] runtimeIntArray = new Integer[3];
130             runtimeIntArray[0] = new Integer(0);
131             runtimeIntArray[1] = new Integer(1);
132             runtimeIntArray[2] = new Integer(2);
133 
134             CharPair[] runtimeCharPairArray = new CharPair[3];
135             runtimeCharPairArray[0] = new CharPair('a', 'b');
136             runtimeCharPairArray[1] = new CharPair('c', 'd');
137             runtimeCharPairArray[2] = new CharPair('e', 'f');
138 
139             Wrapper[] runtimeWrapperArray = new Wrapper[3];
140             runtimeWrapperArray[0] = new Wrapper(0);
141             runtimeWrapperArray[1] = new Wrapper(1);
142             runtimeWrapperArray[2] = new Wrapper(2);
143 
144             if (Arrays.compare(archivedObjects.intArray, runtimeIntArray) != 0) {
145                 throw new RuntimeException("Integer array not restored correctly");
146             }
147 
148             if (Arrays.compare(archivedObjects.charPairArray, runtimeCharPairArray) != 0) {
149                 throw new RuntimeException("CharPair array not restored correctly");
150             }
151 
152             if (Arrays.compare(archivedObjects.wrapperArray, runtimeWrapperArray) != 0) {
153                 throw new RuntimeException("Wrapper array not restored correctly");
154             }
155         }
156     }
157 }