|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | + * @bug 8282218 |
| 27 | + * |
| 28 | + * @run main/othervm -Xcomp -XX:TieredStopAtLevel=1 |
| 29 | + * -XX:CompileCommand=compileonly,compiler.c1.TestClassConstantPatching$Test::run |
| 30 | + * compiler.c1.TestClassConstantPatching |
| 31 | + */ |
| 32 | +package compiler.c1; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.lang.reflect.Method; |
| 38 | + |
| 39 | +public class TestClassConstantPatching { |
| 40 | + public static int COUNTER = 0; |
| 41 | + |
| 42 | + static class CustomLoader extends ClassLoader { |
| 43 | + @Override |
| 44 | + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
| 45 | + if (name.startsWith(Test.class.getName())) { |
| 46 | + Class<?> c = findLoadedClass(name); |
| 47 | + if (c != null) { |
| 48 | + return c; |
| 49 | + } |
| 50 | + try { |
| 51 | + COUNTER++; // update counter on loading |
| 52 | + |
| 53 | + InputStream in = getSystemResourceAsStream(name.replace('.', File.separatorChar) + ".class"); |
| 54 | + byte[] buf = in.readAllBytes(); |
| 55 | + return defineClass(name, buf, 0, buf.length); |
| 56 | + } catch (IOException e) { |
| 57 | + throw new ClassNotFoundException(name); |
| 58 | + } |
| 59 | + } |
| 60 | + return super.loadClass(name, resolve); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static class Test { |
| 65 | + static class T {} |
| 66 | + |
| 67 | + public static Class<?> run(boolean cond) { |
| 68 | + int before = COUNTER; |
| 69 | + Class<?> c = null; |
| 70 | + if (cond) { |
| 71 | + c = T.class; |
| 72 | + } |
| 73 | + int after = COUNTER; |
| 74 | + if (cond && before == after) { |
| 75 | + throw new AssertionError("missed update"); |
| 76 | + } |
| 77 | + return c; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) throws ReflectiveOperationException { |
| 82 | + ClassLoader cl = new CustomLoader(); |
| 83 | + |
| 84 | + Class.forName(TestClassConstantPatching.class.getName(), true, cl); // preload counter holder class |
| 85 | + |
| 86 | + Class<?> test = Class.forName(Test.class.getName(), true, cl); |
| 87 | + |
| 88 | + Method m = test.getDeclaredMethod("run", boolean.class); |
| 89 | + |
| 90 | + m.invoke(null, false); |
| 91 | + m.invoke(null, true); |
| 92 | + |
| 93 | + System.out.println("TEST PASSED"); |
| 94 | + } |
| 95 | +} |
| 96 | + |
0 commit comments