1 /* 2 * Copyright (c) 2017, 2019, 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 8231827 27 * @summary Basic tests for bindings from instanceof 28 * @compile --enable-preview -source ${jdk.version} BindingsTest1.java 29 * @run main/othervm --enable-preview BindingsTest1 30 */ 31 32 public class BindingsTest1 { 33 public static boolean Ktrue() { return true; } 34 public static void main(String[] args) { 35 Object o1 = "hello"; 36 Integer i = 42; 37 Object o2 = i; 38 Object o3 = "there"; 39 40 41 // Test for (e matches P).T = { binding variables in P } 42 if (o1 instanceof String s) { 43 s.length(); 44 } 45 46 // Test for e1 && e2.T = union(e1.T, e2.T) 47 if (o1 instanceof String s && o2 instanceof Integer in) { 48 s.length(); 49 in.intValue(); 50 } 51 52 // test for e1&&e2 - include e1.T in e2 53 if (o1 instanceof String s && s.length()>0) { 54 System.out.print("done"); 55 } 56 57 // Test for (e1 || e2).F = union(e1.F, e2.F) 58 if (!(o1 instanceof String s) || !(o3 instanceof Integer in)){ 59 } else { 60 s.length(); 61 i.intValue(); 62 } 63 64 // Test for e1||e2 - include e1.F in e2 65 66 if (!(o1 instanceof String s) || s.length()>0) { 67 System.out.println("done"); 68 } 69 70 // Test for e1 ? e2: e3 - include e1.T in e2 71 if (o1 instanceof String s ? s.length()>0 : false) { 72 System.out.println("done"); 73 } 74 75 // Test for e1 ? e2 : e3 - include e1.F in e3 76 if (!(o1 instanceof String s) ? false : s.length()>0){ 77 System.out.println("done"); 78 } 79 80 // Test for (!e).T = e.F 81 82 if (!(!(o1 instanceof String s) || !(o3 instanceof Integer in))){ 83 s.length(); 84 i.intValue(); 85 } 86 87 // Test for (!e).F = e.T 88 if (!(o1 instanceof String s)) { 89 90 } else { 91 s.length(); 92 } 93 94 L1: { 95 if (o1 instanceof String s) { 96 s.length(); 97 } else { 98 break L1; 99 } 100 s.length(); 101 } 102 103 L2: { 104 if (!(o1 instanceof String s)) { 105 break L2; 106 } else { 107 s.length(); 108 } 109 s.length(); 110 } 111 112 L4: { 113 if (!(o1 instanceof String s)) { 114 break L4; 115 } 116 s.length(); 117 } 118 119 { 120 while (!(o1 instanceof String s)) { 121 } 122 123 s.length(); 124 } 125 126 L5: { 127 while (!(o1 instanceof String s)) { 128 } 129 130 s.length(); 131 } 132 133 { 134 L6: for ( ;!(o1 instanceof String s); ) { 135 136 } 137 138 s.length(); 139 } 140 141 { 142 L7: do { 143 144 } while (!(o1 instanceof String s)); 145 146 s.length(); 147 } 148 149 if (o1 instanceof String s) { 150 Runnable r1 = new Runnable() { 151 @Override 152 public void run() { 153 s.length(); 154 } 155 }; 156 r1.run(); 157 Runnable r2 = () -> { 158 s.length(); 159 }; 160 r2.run(); 161 String s2 = s; 162 } 163 164 System.out.println("BindingsTest1 complete"); 165 } 166 }