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     }
 36 
 37     public static value class CharPair implements Comparable<CharPair> {
 38         char c0, c1;
 39 
 40         public String toString() {
 41             return "(" + c0 + ", " + c1 + ")";
 42         }
 43 
 44         public int compareTo(CharPair o) {
 45             return (c0 - o.c0) - (c1 - o.c1);
 46         }
 47 
 48         public CharPair(char c0, char c1) {
 49             this.c0 = c0;
 50             this.c1 = c1;
 51         }
 52     }
 53 
 54     static ArchivedData archivedObjects;
 55     static boolean restored;
 56     static {
 57         if (archivedObjects == null) {
 58             restored = false;
 59             System.out.println("Not archived");
 60             archivedObjects = new ArchivedData();
 61 
 62             archivedObjects.intArray = new Integer[3];
 63             archivedObjects.intArray[0] = new Integer(0);
 64             archivedObjects.intArray[1] = new Integer(1);
 65             archivedObjects.intArray[2] = new Integer(2);
 66 
 67             archivedObjects.charPairArray = new CharPair[3];
 68             archivedObjects.charPairArray[0] = new CharPair('a', 'b');
 69             archivedObjects.charPairArray[1] = new CharPair('c', 'd');
 70             archivedObjects.charPairArray[2] = new CharPair('e', 'f');
 71         } else {
 72             restored = true;
 73             System.out.println("Initialized from CDS");
 74             System.out.println("intArray " + archivedObjects.intArray);
 75             System.out.println("charPairArray " + archivedObjects.charPairArray);
 76         }
 77 
 78         for (Integer i : archivedObjects.intArray) {
 79             System.out.println(i);
 80         }
 81 
 82         for (CharPair c : archivedObjects.charPairArray) {
 83             System.out.println(c);
 84         }
 85     }
 86 
 87     public static void main(String[] args) {
 88         if (!ValueClass.isFlatArray(archivedObjects.intArray)) {
 89             throw new RuntimeException("Integer array should be flat");
 90         }
 91 
 92         if (!ValueClass.isFlatArray(archivedObjects.intArray)) {
 93             throw new RuntimeException("CharPair array should be flat");
 94         }
 95 
 96         if (restored) {
 97             // Ensure archived arrays are restored properly
 98             Integer[] runtimeIntArray = new Integer[3];
 99             runtimeIntArray[0] = new Integer(0);
100             runtimeIntArray[1] = new Integer(1);
101             runtimeIntArray[2] = new Integer(2);
102 
103             CharPair[] runtimeCharPairArray = new CharPair[3];
104             runtimeCharPairArray[0] = new CharPair('a', 'b');
105             runtimeCharPairArray[1] = new CharPair('c', 'd');
106             runtimeCharPairArray[2] = new CharPair('e', 'f');
107 
108             if (Arrays.compare(archivedObjects.intArray, runtimeIntArray) != 0) {
109                 throw new RuntimeException("Integer array not restored correctly");
110             }
111 
112             if (Arrays.compare(archivedObjects.charPairArray, runtimeCharPairArray) != 0) {
113                 throw new RuntimeException("CharPair array not restored correctly");
114             }
115         }
116     }
117 }