|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8268748 |
| 27 | + * @summary Javac generates error opcodes when using nest pattern variables |
| 28 | + * @library /tools/lib |
| 29 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 30 | + * jdk.compiler/com.sun.tools.javac.main |
| 31 | + * jdk.jdeps/com.sun.tools.classfile |
| 32 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 33 | + * @run main NestedPatternVariablesBytecode |
| 34 | + */ |
| 35 | + |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.List; |
| 39 | +import java.util.stream.StreamSupport; |
| 40 | + |
| 41 | +import com.sun.tools.classfile.ClassFile; |
| 42 | +import com.sun.tools.classfile.ConstantPoolException; |
| 43 | +import com.sun.tools.classfile.Method; |
| 44 | +import com.sun.tools.classfile.Attribute; |
| 45 | +import com.sun.tools.classfile.Code_attribute; |
| 46 | +import com.sun.tools.classfile.Instruction; |
| 47 | + |
| 48 | +import toolbox.JavacTask; |
| 49 | +import toolbox.TestRunner; |
| 50 | +import toolbox.ToolBox; |
| 51 | + |
| 52 | +public class NestedPatternVariablesBytecode extends TestRunner { |
| 53 | + private static final String JAVA_VERSION = System.getProperty("java.specification.version"); |
| 54 | + private static final String TEST_METHOD = "test"; |
| 55 | + |
| 56 | + ToolBox tb; |
| 57 | + ClassFile cf; |
| 58 | + |
| 59 | + public NestedPatternVariablesBytecode() { |
| 60 | + super(System.err); |
| 61 | + tb = new ToolBox(); |
| 62 | + } |
| 63 | + |
| 64 | + public static void main(String[] args) throws Exception { |
| 65 | + NestedPatternVariablesBytecode t = new NestedPatternVariablesBytecode(); |
| 66 | + t.runTests(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testNestedPatternVariablesBytecode() throws Exception { |
| 71 | + String code = """ |
| 72 | + class NestedPatterVariablesTest { |
| 73 | + String test(Object o) { |
| 74 | + if (o instanceof (CharSequence cs && cs instanceof String s)) { |
| 75 | + return s; |
| 76 | + } |
| 77 | + return null; |
| 78 | + } |
| 79 | + }"""; |
| 80 | + Path curPath = Path.of("."); |
| 81 | + new JavacTask(tb) |
| 82 | + .options("--enable-preview", "-source", JAVA_VERSION) |
| 83 | + .sources(code) |
| 84 | + .outdir(curPath) |
| 85 | + .run(); |
| 86 | + |
| 87 | + cf = ClassFile.read(curPath.resolve("NestedPatterVariablesTest.class")); |
| 88 | + Method testMethod = Arrays.stream(cf.methods) |
| 89 | + .filter(m -> isTestMethod(m)) |
| 90 | + .findAny() |
| 91 | + .get(); |
| 92 | + Code_attribute code_attribute = (Code_attribute) testMethod.attributes.get(Attribute.Code); |
| 93 | + |
| 94 | + List<String> actualCode = getCodeInstructions(code_attribute); |
| 95 | + List<String> expectedCode = Arrays.asList( |
| 96 | + "aload_1", "instanceof", "ifeq", "aload_1", "checkcast", "astore_2", "aload_2", "instanceof", |
| 97 | + "ifeq", "aload_2", "checkcast", "astore_3", "aload_3", "areturn", "aconst_null", "areturn"); |
| 98 | + tb.checkEqual(expectedCode, actualCode); |
| 99 | + } |
| 100 | + |
| 101 | + boolean isTestMethod(Method m) { |
| 102 | + try { |
| 103 | + return TEST_METHOD.equals(m.getName(cf.constant_pool)); |
| 104 | + } catch (ConstantPoolException e) { |
| 105 | + throw new IllegalStateException(e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + List<String> getCodeInstructions(Code_attribute code) { |
| 110 | + return StreamSupport.stream(code.getInstructions().spliterator(), false) |
| 111 | + .map(Instruction::getMnemonic) |
| 112 | + .toList(); |
| 113 | + } |
| 114 | +} |
0 commit comments