|
1 | 1 | /* |
2 | | - * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * @test |
26 | | - * @bug 8133884 8162711 8133896 8172158 8172262 8173636 8175119 8189747 8236842 8254023 8263432 |
| 26 | + * @bug 8133884 8162711 8133896 8172158 8172262 8173636 8175119 8189747 8236842 8254023 8263432 8305225 |
27 | 27 | * @summary Verify that annotation processing works. |
28 | 28 | * @library /tools/lib |
29 | 29 | * @modules |
@@ -2133,6 +2133,94 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment |
2133 | 2133 |
|
2134 | 2134 | } |
2135 | 2135 |
|
| 2136 | + @Test |
| 2137 | + public void testCreateProvidesWithAP(Path base) throws Exception { |
| 2138 | + Path src = base.resolve("src"); |
| 2139 | + Path m1 = src.resolve("m"); |
| 2140 | + |
| 2141 | + tb.writeJavaFiles(m1, |
| 2142 | + """ |
| 2143 | + module m { |
| 2144 | + provides api1.Api with test.Test; |
| 2145 | + uses api1.Api; |
| 2146 | + uses api2.Api; |
| 2147 | + } |
| 2148 | + """); |
| 2149 | + |
| 2150 | + Path classes = base.resolve("classes"); |
| 2151 | + Files.createDirectories(classes); |
| 2152 | + |
| 2153 | + List<String> expectedErrors = List.of( |
| 2154 | + "module-info.java:2:18: compiler.err.doesnt.exist: api1", |
| 2155 | + "module-info.java:2:32: compiler.err.doesnt.exist: test", |
| 2156 | + "module-info.java:3:14: compiler.err.doesnt.exist: api1", |
| 2157 | + "module-info.java:4:14: compiler.err.doesnt.exist: api2", |
| 2158 | + "4 errors"); |
| 2159 | + List<String> actualErrors = new JavacTask(tb) |
| 2160 | + .options("-XDrawDiagnostics") |
| 2161 | + .outdir(classes) |
| 2162 | + .files(findJavaFiles(m1)) |
| 2163 | + .run(Task.Expect.FAIL) |
| 2164 | + .writeAll() |
| 2165 | + .getOutputLines(OutputKind.DIRECT); |
| 2166 | + |
| 2167 | + tb.checkEqual(expectedErrors, actualErrors); |
| 2168 | + |
| 2169 | + new JavacTask(tb) |
| 2170 | + .options("-processor", CreateProvidesWithAP.class.getName()) |
| 2171 | + .outdir(classes) |
| 2172 | + .files(findJavaFiles(m1)) |
| 2173 | + .run() |
| 2174 | + .writeAll(); |
| 2175 | + } |
| 2176 | + |
| 2177 | + @SupportedAnnotationTypes("*") |
| 2178 | + public static final class CreateProvidesWithAP extends AbstractProcessor { |
| 2179 | + |
| 2180 | + int round; |
| 2181 | + |
| 2182 | + @Override |
| 2183 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 2184 | + processingEnv.getElementUtils().getModuleElement("m").getDirectives(); |
| 2185 | + if (round++ == 0) { |
| 2186 | + try (Writer w = processingEnv.getFiler().createSourceFile("test.Test").openWriter()) { |
| 2187 | + w.append(""" |
| 2188 | + package test; |
| 2189 | + public class Test implements api1.Api { |
| 2190 | + } |
| 2191 | + """); |
| 2192 | + } catch (IOException ex) { |
| 2193 | + throw new IllegalStateException(ex); |
| 2194 | + } |
| 2195 | + try (Writer w = processingEnv.getFiler().createSourceFile("api1.Api").openWriter()) { |
| 2196 | + w.append(""" |
| 2197 | + package api1; |
| 2198 | + public interface Api { |
| 2199 | + } |
| 2200 | + """); |
| 2201 | + } catch (IOException ex) { |
| 2202 | + throw new IllegalStateException(ex); |
| 2203 | + } |
| 2204 | + try (Writer w = processingEnv.getFiler().createSourceFile("api2.Api").openWriter()) { |
| 2205 | + w.append(""" |
| 2206 | + package api2; |
| 2207 | + public interface Api { |
| 2208 | + } |
| 2209 | + """); |
| 2210 | + } catch (IOException ex) { |
| 2211 | + throw new IllegalStateException(ex); |
| 2212 | + } |
| 2213 | + } |
| 2214 | + return false; |
| 2215 | + } |
| 2216 | + |
| 2217 | + @Override |
| 2218 | + public SourceVersion getSupportedSourceVersion() { |
| 2219 | + return SourceVersion.latest(); |
| 2220 | + } |
| 2221 | + |
| 2222 | + } |
| 2223 | + |
2136 | 2224 | private static void assertNonNull(String msg, Object val) { |
2137 | 2225 | if (val == null) { |
2138 | 2226 | throw new AssertionError(msg); |
|
0 commit comments