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