|
| 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 | +/** |
| 26 | + * @test |
| 27 | + * @bug 8344904 |
| 28 | + * @summary make sure all interned strings in old classes are archived. |
| 29 | + * @requires vm.cds.write.archived.java.heap |
| 30 | + * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds |
| 31 | + * @build OldClassWithStaticString |
| 32 | + * @build StaticStringInOldClass |
| 33 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller |
| 34 | + * -jar StaticStringInOldClass.jar StaticStringInOldClass StaticStringInOldClassApp OldClassWithStaticString |
| 35 | + * @run driver StaticStringInOldClass |
| 36 | + */ |
| 37 | + |
| 38 | +import java.lang.reflect.Field; |
| 39 | +import jdk.test.lib.process.OutputAnalyzer; |
| 40 | +import jdk.test.lib.helpers.ClassFileInstaller; |
| 41 | + |
| 42 | +public class StaticStringInOldClass { |
| 43 | + static final String appClass = StaticStringInOldClassApp.class.getName(); |
| 44 | + static String[] classes = { |
| 45 | + appClass, |
| 46 | + OldClassWithStaticString.class.getName(), |
| 47 | + }; |
| 48 | + |
| 49 | + public static void main(String[] args) throws Exception { |
| 50 | + String appJar = ClassFileInstaller.getJarPath("StaticStringInOldClass.jar"); |
| 51 | + OutputAnalyzer output; |
| 52 | + output = TestCommon.testDump(appJar, TestCommon.list(classes)); |
| 53 | + output = TestCommon.exec(appJar, appClass); |
| 54 | + TestCommon.checkExec(output, "Hello"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class StaticStringInOldClassApp { |
| 59 | + static String a = "xxxx123"; |
| 60 | + public static void main(String args[]) throws Exception { |
| 61 | + System.out.println("Hello"); |
| 62 | + String x = (a + "yyyy456").intern(); |
| 63 | + Class c = OldClassWithStaticString.class; |
| 64 | + Field f = c.getField("s"); |
| 65 | + String y = (String)(f.get(null)); |
| 66 | + if (x != y) { |
| 67 | + throw new RuntimeException("Interned strings not equal: " + |
| 68 | + "\"" + x + "\" @ " + System.identityHashCode(x) + " vs " + |
| 69 | + "\"" + y + "\" @ " + System.identityHashCode(y)); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments