1024 1025 new TreePathScanner<Void, Void>() { 1026 @Override public Void visitVariable(VariableTree vt, Void p) { 1027 assertNotNull(vt.getModifiers()); 1028 assertNotNull(vt.getType()); 1029 assertNotNull(vt.getName()); 1030 assertEquals("name should be <error>", "<error>", vt.getName().toString()); 1031 foundVar[0] = true; 1032 return super.visitVariable(vt, p); 1033 } 1034 }.scan(cuts, null); 1035 1036 if (!foundVar[0]) { 1037 fail("haven't found a variable"); 1038 } 1039 1040 String actualErrors = normalize(out.toString()); 1041 assertEquals("the error message is not correct, actual: " + actualErrors, expectedErrors, actualErrors); 1042 } 1043 1044 @Test 1045 void testCaseBodyStatements() throws IOException { 1046 String code = "class C {" + 1047 " void t(int i) {" + 1048 " switch (i) {" + 1049 " case 0 -> i++;" + 1050 " case 1 -> { i++; }" + 1051 " case 2 -> throw new RuntimeException();" + 1052 " case 3 -> if (true) ;" + 1053 " default -> i++;" + 1054 " }" + 1055 " switch (i) {" + 1056 " case 0: i++; break;" + 1057 " case 1: { i++; break;}" + 1058 " case 2: throw new RuntimeException();" + 1059 " case 3: if (true) ; break;" + 1060 " default: i++; break;" + 1061 " }" + 1062 " int j = switch (i) {" + 1063 " case 0 -> i + 1;" + | 1024 1025 new TreePathScanner<Void, Void>() { 1026 @Override public Void visitVariable(VariableTree vt, Void p) { 1027 assertNotNull(vt.getModifiers()); 1028 assertNotNull(vt.getType()); 1029 assertNotNull(vt.getName()); 1030 assertEquals("name should be <error>", "<error>", vt.getName().toString()); 1031 foundVar[0] = true; 1032 return super.visitVariable(vt, p); 1033 } 1034 }.scan(cuts, null); 1035 1036 if (!foundVar[0]) { 1037 fail("haven't found a variable"); 1038 } 1039 1040 String actualErrors = normalize(out.toString()); 1041 assertEquals("the error message is not correct, actual: " + actualErrors, expectedErrors, actualErrors); 1042 } 1043 1044 @Test //JDK-821742 1045 void testCompDeclVarType() throws IOException { 1046 String code = "package test; public class Test {" 1047 + "private void test() {" 1048 + "var v1 = 10,v2 = 12;" 1049 + "} private Test() {}}"; 1050 1051 JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, 1052 null, null, Arrays.asList(new MyFileObject(code))); 1053 CompilationUnitTree cut = ct.parse().iterator().next(); 1054 ct.enter(); 1055 ct.analyze(); 1056 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1057 MethodTree method = (MethodTree) clazz.getMembers().get(0); 1058 VariableTree stmt1 = (VariableTree) method.getBody().getStatements().get(0); 1059 VariableTree stmt2 = (VariableTree) method.getBody().getStatements().get(1); 1060 Tree v1Type = stmt1.getType(); 1061 Tree v2Type = stmt2.getType(); 1062 assertEquals("Implicit type for v1 is not correct: ", Kind.PRIMITIVE_TYPE, v1Type.getKind()); 1063 assertEquals("Implicit type for v2 is not correct: ", Kind.PRIMITIVE_TYPE, v2Type.getKind()); 1064 } 1065 1066 @Test 1067 void testCaseBodyStatements() throws IOException { 1068 String code = "class C {" + 1069 " void t(int i) {" + 1070 " switch (i) {" + 1071 " case 0 -> i++;" + 1072 " case 1 -> { i++; }" + 1073 " case 2 -> throw new RuntimeException();" + 1074 " case 3 -> if (true) ;" + 1075 " default -> i++;" + 1076 " }" + 1077 " switch (i) {" + 1078 " case 0: i++; break;" + 1079 " case 1: { i++; break;}" + 1080 " case 2: throw new RuntimeException();" + 1081 " case 3: if (true) ; break;" + 1082 " default: i++; break;" + 1083 " }" + 1084 " int j = switch (i) {" + 1085 " case 0 -> i + 1;" + |