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 8378740
 27  * @summary Verify warnings are properly suppressed for the combination of
 28  *          annotation processing and implicit compilation
 29  * @library /tools/lib
 30  * @modules
 31  *      jdk.compiler/com.sun.tools.javac.api
 32  *      jdk.compiler/com.sun.tools.javac.main
 33  * @build toolbox.ToolBox toolbox.JavacTask
 34  * @run junit APImplicitClassesWarnings
 35  */
 36 
 37 
 38 import java.nio.file.Files;
 39 import java.nio.file.Path;
 40 import java.nio.file.Paths;
 41 import java.util.List;
 42 import java.util.Set;
 43 import javax.annotation.processing.AbstractProcessor;
 44 import javax.annotation.processing.RoundEnvironment;
 45 import javax.annotation.processing.SupportedAnnotationTypes;
 46 import javax.lang.model.SourceVersion;
 47 import javax.lang.model.element.TypeElement;
 48 import org.junit.jupiter.api.BeforeEach;
 49 import org.junit.jupiter.api.Test;
 50 import org.junit.jupiter.api.TestInfo;
 51 import toolbox.JavacTask;
 52 import toolbox.Task;
 53 import toolbox.ToolBox;
 54 
 55 public class APImplicitClassesWarnings {
 56 
 57     final ToolBox tb = new ToolBox();
 58     Path base;
 59 
 60     @Test
 61     public void testCorrectSource() throws Exception {
 62         Path src = base.resolve("src");
 63         Path classes = base.resolve("classes");
 64         tb.writeJavaFiles(src,
 65                           """
 66                           package test;
 67 
 68                           @Deprecated(forRemoval=true)
 69                           public class Depr {
 70                           }
 71                           """,
 72                           """
 73                           package test;
 74                           public class Use {
 75                               Implicit implicit;
 76                               Depr depr;
 77                           }
 78                           """,
 79                           """
 80                           package test;
 81                           public interface Implicit {}
 82                           """);
 83         Files.createDirectories(classes);
 84 
 85         List<String> log = new JavacTask(tb)
 86             .options("-d", classes.toString(),
 87                      "-XDrawDiagnostics",
 88                      "-implicit:class",
 89                      "-sourcepath", src.toString())
 90             .files(src.resolve("test").resolve("Depr.java"),
 91                     src.resolve("test").resolve("Use.java"))
 92             .processors(new ProcessorImpl())
 93             .run()
 94             .writeAll()
 95             .getOutputLines(Task.OutputKind.DIRECT);
 96 
 97         List<String> expected = List.of(
 98             "Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test",
 99             "Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test",
100             "Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test",
101             "3 warnings"
102         );
103 
104         tb.checkEqual(expected, log);
105     }
106 
107     @Test
108     public void testCorrectSuppress() throws Exception {
109         Path src = base.resolve("src");
110         Path classes = base.resolve("classes");
111         tb.writeJavaFiles(src,
112                           //note the added @SuppressWarnings("removal"):
113                           """
114                           package test;
115 
116                           @Deprecated(forRemoval=true)
117                           public class Depr {
118                           }
119                           """,
120                           """
121                           package test;
122                           public class Use {
123                               Implicit implicit;
124                               @SuppressWarnings("removal")
125                               Depr depr;
126                           }
127                           """,
128                           """
129                           package test;
130                           public interface Implicit {}
131                           """);
132         Files.createDirectories(classes);
133 
134         new JavacTask(tb)
135                 .options("-d", classes.toString(),
136                          "-Werror",
137                          "-implicit:class",
138                          "-sourcepath", src.toString())
139                 .files(src.resolve("test").resolve("Depr.java"),
140                        src.resolve("test").resolve("Use.java"))
141                 .processors(new ProcessorImpl())
142                 .run()
143                 .writeAll();
144     }
145 
146     @SupportedAnnotationTypes("*")
147     private static class ProcessorImpl extends AbstractProcessor {
148         @Override
149         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
150             return false;
151         }
152         @Override
153         public SourceVersion getSupportedSourceVersion() {
154             return SourceVersion.latest();
155         }
156     }
157 
158     @BeforeEach
159     public void setUp(TestInfo info) {
160         base = Paths.get(".")
161                     .resolve(info.getTestMethod().orElseThrow().getName());
162     }
163 }