|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +import jdk.test.whitebox.WhiteBox; |
| 25 | + |
| 26 | +public class ResolvedReferencesWb { |
| 27 | + public static void main(String[] args) throws Exception { |
| 28 | + WhiteBox wb = WhiteBox.getWhiteBox(); |
| 29 | + |
| 30 | + if (args.length < 1) { |
| 31 | + throw new RuntimeException("Test requires arg: [true|false]"); |
| 32 | + } |
| 33 | + |
| 34 | + if (!args[0].equals("true") && !args[0].equals("false")) { |
| 35 | + throw new RuntimeException("Invalid argument: Test requires arg: [true|false]"); |
| 36 | + } |
| 37 | + |
| 38 | + ResolvedReferencesTestApp t = new ResolvedReferencesTestApp(); |
| 39 | + Object[] resolvedReferences = wb.getResolvedReferences(ResolvedReferencesTestApp.class); |
| 40 | + boolean isArchived = (args[0].equals("true")); |
| 41 | + |
| 42 | + if (resolvedReferences.length <= 0) { |
| 43 | + throw new RuntimeException("Resolved reference should not be null"); |
| 44 | + } |
| 45 | + |
| 46 | + boolean foundFoo = false; |
| 47 | + boolean foundBar = false; |
| 48 | + boolean foundQux = false; |
| 49 | + |
| 50 | + for (Object o : resolvedReferences) { |
| 51 | + if (o != null) { |
| 52 | + foundFoo |= (o.equals("fooString")); |
| 53 | + foundBar |= (o.equals("barString")); |
| 54 | + foundQux |= (o.equals("quxString")); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (isArchived) { |
| 59 | + // CDS eagerly resolves all the string literals in the ConstantPool. At this point, all |
| 60 | + // three strings should be in the resolvedReferences array. |
| 61 | + if (!foundFoo || !foundBar || !foundQux) { |
| 62 | + throwException(resolvedReferences, "Incorrect resolved references array, all strings should be present"); |
| 63 | + } |
| 64 | + } else { |
| 65 | + // If the class is not archived, the string literals in the ConstantPool are resolved |
| 66 | + // on-demand. At this point, ResolvedReferencesTestApp::<clinit> has been executed |
| 67 | + // so the two strings used there should be in the resolvedReferences array. |
| 68 | + // ResolvedReferencesTestApp::qux() is not executed so "quxString" |
| 69 | + // should not yet be resolved. |
| 70 | + if (!foundFoo || !foundBar || foundQux) { |
| 71 | + throwException(resolvedReferences, "Incorrect resolved references array, quxString should not be archived"); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + static void throwException(Object[] resolvedRefs, String errMsg) throws RuntimeException { |
| 77 | + System.out.printf("Resolved References Array Length: %d\n", resolvedRefs.length); |
| 78 | + for (Object o : resolvedRefs) { |
| 79 | + System.out.println(o); |
| 80 | + } |
| 81 | + throw new RuntimeException(errMsg); |
| 82 | + } |
| 83 | +} |
0 commit comments