|
| 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 8264306 |
| 27 | + * @summary Verify patterns are desugaring in a reproducible manner |
| 28 | + * @library /tools/lib /tools/javac/lib |
| 29 | + * @modules |
| 30 | + * jdk.compiler/com.sun.tools.javac.api |
| 31 | + * jdk.compiler/com.sun.tools.javac.file |
| 32 | + * jdk.compiler/com.sun.tools.javac.main |
| 33 | + * jdk.compiler/com.sun.tools.javac.util |
| 34 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 35 | + * @compile StablePatternDesugaring.java |
| 36 | + * @run main StablePatternDesugaring |
| 37 | + */ |
| 38 | + |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.util.Arrays; |
| 43 | +import toolbox.ToolBox; |
| 44 | +import toolbox.JavacTask; |
| 45 | + |
| 46 | +public class StablePatternDesugaring { |
| 47 | + protected ToolBox tb; |
| 48 | + |
| 49 | + StablePatternDesugaring() { |
| 50 | + tb = new ToolBox(); |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String... args) throws Exception { |
| 54 | + new StablePatternDesugaring().run(); |
| 55 | + } |
| 56 | + |
| 57 | + void run() throws Exception { |
| 58 | + String code = """ |
| 59 | + public class T { |
| 60 | + private boolean t(Object o) { |
| 61 | + if (o instanceof String s) { |
| 62 | + return s.isEmpty(); |
| 63 | + } else if (o instanceof CharSequence cs) { |
| 64 | + return cs.isEmpty(); |
| 65 | + } else if (o instanceof Integer i) { |
| 66 | + return i != 0; |
| 67 | + } else if (o instanceof Float f) { |
| 68 | + return f != 0; |
| 69 | + } else { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + """; |
| 75 | + Path base = Paths.get("."); |
| 76 | + Path src = base.resolve("src"); |
| 77 | + |
| 78 | + tb.writeJavaFiles(src, code); |
| 79 | + |
| 80 | + Path classes1 = base.resolve("classes1"); |
| 81 | + |
| 82 | + if (Files.isDirectory(classes1)) { |
| 83 | + tb.cleanDirectory(classes1); |
| 84 | + } else { |
| 85 | + Files.createDirectories(classes1); |
| 86 | + } |
| 87 | + |
| 88 | + new JavacTask(tb) |
| 89 | + .files(tb.findJavaFiles(src)) |
| 90 | + .outdir(classes1) |
| 91 | + .run() |
| 92 | + .writeAll(); |
| 93 | + |
| 94 | + byte[] expected = Files.readAllBytes(classes1.resolve("T.class")); |
| 95 | + |
| 96 | + for (int i = 0; i < 10; i++) { |
| 97 | + Path classes2 = base.resolve("classes2"); |
| 98 | + |
| 99 | + if (Files.isDirectory(classes2)) { |
| 100 | + tb.cleanDirectory(classes2); |
| 101 | + } else { |
| 102 | + Files.createDirectories(classes2); |
| 103 | + } |
| 104 | + |
| 105 | + new JavacTask(tb) |
| 106 | + .files(tb.findJavaFiles(src)) |
| 107 | + .outdir(classes2) |
| 108 | + .run() |
| 109 | + .writeAll(); |
| 110 | + |
| 111 | + byte[] actual = Files.readAllBytes(classes2.resolve("T.class")); |
| 112 | + |
| 113 | + if (!Arrays.equals(expected, actual)) { |
| 114 | + throw new AssertionError("Classfiles differ!"); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments