|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 8381654 |
| 27 | + * @summary AP interference with tokenizer warnings |
| 28 | + * @library /tools/lib |
| 29 | + * @modules |
| 30 | + * jdk.compiler/com.sun.tools.javac.api |
| 31 | + * jdk.compiler/com.sun.tools.javac.main |
| 32 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 33 | + * @run junit ${test.main.class} |
| 34 | + */ |
| 35 | + |
| 36 | + |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | +import javax.annotation.processing.AbstractProcessor; |
| 43 | +import javax.annotation.processing.RoundEnvironment; |
| 44 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 45 | +import javax.lang.model.SourceVersion; |
| 46 | +import javax.lang.model.element.TypeElement; |
| 47 | +import org.junit.jupiter.api.BeforeEach; |
| 48 | +import org.junit.jupiter.api.TestInfo; |
| 49 | +import org.junit.jupiter.params.ParameterizedTest; |
| 50 | +import org.junit.jupiter.params.provider.MethodSource; |
| 51 | +import toolbox.JavacTask; |
| 52 | +import toolbox.Task; |
| 53 | +import toolbox.ToolBox; |
| 54 | + |
| 55 | +public class TestParserWarnings { |
| 56 | + |
| 57 | + static boolean[] apOptions() { |
| 58 | + return new boolean[] {false, true}; |
| 59 | + } |
| 60 | + |
| 61 | + final ToolBox tb = new ToolBox(); |
| 62 | + Path base, src, classes; |
| 63 | + |
| 64 | + @ParameterizedTest @MethodSource("apOptions") |
| 65 | + public void testPreviewWarning(boolean useProcessor) throws Exception { |
| 66 | + tb.writeJavaFiles(src, """ |
| 67 | + public record MyRec() {} |
| 68 | + """); |
| 69 | + |
| 70 | + JavacTask task = new JavacTask(tb) |
| 71 | + .options("--enable-preview", |
| 72 | + "-source", Integer.toString(Runtime.version().feature()), |
| 73 | + "-XDforcePreview", |
| 74 | + "-XDrawDiagnostics") |
| 75 | + .files(tb.findJavaFiles(src)) |
| 76 | + .outdir(classes); |
| 77 | + if (useProcessor) { |
| 78 | + task.processors(new ProcessorImpl()); |
| 79 | + } |
| 80 | + List<String> log = task |
| 81 | + .run() |
| 82 | + .writeAll() |
| 83 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 84 | + |
| 85 | + List<String> expected = List.of( |
| 86 | + "- compiler.note.preview.filename: MyRec.java, DEFAULT", |
| 87 | + "- compiler.note.preview.recompile" |
| 88 | + ); |
| 89 | + |
| 90 | + tb.checkEqual(expected, log); |
| 91 | + } |
| 92 | + |
| 93 | + @ParameterizedTest @MethodSource("apOptions") |
| 94 | + public void testTextBlockWarning(boolean useProcessor) throws Exception { |
| 95 | + tb.writeJavaFiles(src, """ |
| 96 | + class TextBlockWhitespace { |
| 97 | + String m() { |
| 98 | + return ""\" |
| 99 | + \\u0009\\u0009\\u0009\\u0009tab indentation |
| 100 | + \\u0020\\u0020\\u0020\\u0020space indentation and trailing space\\u0020 |
| 101 | + \\u0020\\u0020\\u0020\\u0020""\"; |
| 102 | + } |
| 103 | + } |
| 104 | + """); |
| 105 | + |
| 106 | + JavacTask task = new JavacTask(tb) |
| 107 | + .options("-Xlint:text-blocks", |
| 108 | + "-XDrawDiagnostics") |
| 109 | + .files(tb.findJavaFiles(src)) |
| 110 | + .outdir(classes); |
| 111 | + if (useProcessor) { |
| 112 | + task.processors(new ProcessorImpl()); |
| 113 | + } |
| 114 | + List<String> log = task |
| 115 | + .run() |
| 116 | + .writeAll() |
| 117 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 118 | + |
| 119 | + List<String> expected = List.of( |
| 120 | + "TextBlockWhitespace.java:3:16: compiler.warn.inconsistent.white.space.indentation", |
| 121 | + "TextBlockWhitespace.java:3:16: compiler.warn.trailing.white.space.will.be.removed", |
| 122 | + "2 warnings" |
| 123 | + ); |
| 124 | + |
| 125 | + tb.checkEqual(expected, log); |
| 126 | + } |
| 127 | + |
| 128 | + @SupportedAnnotationTypes("*") |
| 129 | + private static class ProcessorImpl extends AbstractProcessor { |
| 130 | + @Override |
| 131 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public SourceVersion getSupportedSourceVersion() { |
| 137 | + return SourceVersion.latest(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @BeforeEach |
| 142 | + public void setUp(TestInfo info) throws Exception { |
| 143 | + base = Path.of(".").resolve(info.getTestMethod().get().getName()); |
| 144 | + if (Files.exists(base)) { |
| 145 | + tb.cleanDirectory(base); |
| 146 | + } |
| 147 | + src = base.resolve("src"); |
| 148 | + classes = base.resolve("classes"); |
| 149 | + Files.createDirectories(classes); |
| 150 | + } |
| 151 | +} |
0 commit comments