119 @Override
120 public Void visitCase(CaseTree node, Void p) {
121 if (node.getStatements() != null) {
122 throw new AssertionError("Unexpected statements: " + node.getStatements());
123 }
124 if (node.getBody().getKind() != Tree.Kind.BLOCK) {
125 throw new AssertionError("Unexpected body: " + node.getBody());
126 }
127 return super.visitCase(node, p);
128 }
129 }.scan(parse(code), null);
130 }
131
132 private CompilationUnitTree parse(String code) throws IOException {
133 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
134 assert tool != null;
135 DiagnosticListener<JavaFileObject> noErrors = d -> {};
136
137 StringWriter out = new StringWriter();
138 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
139 List.of("-XDdev", "--enable-preview", "-source", "12"), null,
140 Arrays.asList(new MyFileObject(code)));
141 return ct.parse().iterator().next();
142 }
143
144 static class MyFileObject extends SimpleJavaFileObject {
145 private String text;
146
147 public MyFileObject(String text) {
148 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
149 this.text = text;
150 }
151
152 @Override
153 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
154 return text;
155 }
156 }
157 }
|
119 @Override
120 public Void visitCase(CaseTree node, Void p) {
121 if (node.getStatements() != null) {
122 throw new AssertionError("Unexpected statements: " + node.getStatements());
123 }
124 if (node.getBody().getKind() != Tree.Kind.BLOCK) {
125 throw new AssertionError("Unexpected body: " + node.getBody());
126 }
127 return super.visitCase(node, p);
128 }
129 }.scan(parse(code), null);
130 }
131
132 private CompilationUnitTree parse(String code) throws IOException {
133 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
134 assert tool != null;
135 DiagnosticListener<JavaFileObject> noErrors = d -> {};
136
137 StringWriter out = new StringWriter();
138 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
139 List.of("-XDdev", "--enable-preview", "-source", "13"), null,
140 Arrays.asList(new MyFileObject(code)));
141 return ct.parse().iterator().next();
142 }
143
144 static class MyFileObject extends SimpleJavaFileObject {
145 private String text;
146
147 public MyFileObject(String text) {
148 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
149 this.text = text;
150 }
151
152 @Override
153 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
154 return text;
155 }
156 }
157 }
|