|
10 | 10 | import org.reactivestreams.Publisher;
|
11 | 11 | import reactor.core.publisher.Flux;
|
12 | 12 | import reactor.core.publisher.Mono;
|
13 |
| -import ru.tinkoff.kora.annotation.processor.common.TestUtils.ProcessorOptions; |
14 |
| -import ru.tinkoff.kora.annotation.processor.common.compile.ByteArrayJavaFileObject; |
15 |
| -import ru.tinkoff.kora.annotation.processor.common.compile.KoraCompileTestJavaFileManager; |
16 | 13 | import ru.tinkoff.kora.application.graph.*;
|
17 | 14 |
|
18 | 15 | import javax.annotation.processing.Processor;
|
19 |
| -import javax.tools.Diagnostic; |
20 |
| -import javax.tools.JavaCompiler; |
21 |
| -import javax.tools.JavaFileObject; |
22 |
| -import javax.tools.ToolProvider; |
23 | 16 | import java.io.IOException;
|
24 |
| -import java.io.StringWriter; |
25 | 17 | import java.lang.reflect.Constructor;
|
26 | 18 | import java.lang.reflect.InvocationTargetException;
|
27 | 19 | import java.lang.reflect.Method;
|
28 | 20 | import java.nio.charset.StandardCharsets;
|
29 | 21 | import java.nio.file.Files;
|
| 22 | +import java.nio.file.Path; |
30 | 23 | import java.nio.file.Paths;
|
31 |
| -import java.util.*; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Comparator; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
32 | 28 | import java.util.concurrent.CompletionStage;
|
33 | 29 | import java.util.concurrent.ExecutionException;
|
34 | 30 | import java.util.concurrent.Flow;
|
|
39 | 35 |
|
40 | 36 | @TestInstance(TestInstance.Lifecycle.PER_METHOD)
|
41 | 37 | public abstract class AbstractAnnotationProcessorTest {
|
42 |
| - |
43 |
| - private final JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
44 |
| - |
45 | 38 | protected TestInfo testInfo;
|
46 | 39 | protected CompileResult compileResult;
|
47 | 40 |
|
@@ -83,71 +76,58 @@ protected String commonImports() {
|
83 | 76 | }
|
84 | 77 |
|
85 | 78 | protected CompileResult compile(List<Processor> processors, @Language("java") String... sources) {
|
86 |
| - return compile(processors, Collections.emptyList(), sources); |
87 |
| - } |
88 |
| - |
89 |
| - protected CompileResult compile(List<Processor> processors, List<ProcessorOptions> processorOptions, @Language("java") String... sources) { |
90 |
| - var w = new StringWriter(); |
91 |
| - var diagnostic = new ArrayList<Diagnostic<? extends JavaFileObject>>(); |
92 | 79 | var testPackage = testPackage();
|
93 | 80 | var testClass = this.testInfo.getTestClass().get();
|
94 | 81 | var testMethod = this.testInfo.getTestMethod().get();
|
95 | 82 | var commonImports = this.commonImports();
|
96 |
| - var sourceList = Arrays.stream(sources).map(s -> "package %s;\n%s\n/**\n* @see %s#%s \n*/\n".formatted(testPackage, commonImports, testClass.getCanonicalName(), testMethod.getName()) + s) |
97 |
| - .map(s -> { |
98 |
| - var prefixes = List.of("class ", "interface ", "@interface ", "record ", "enum "); |
99 |
| - var firstClass = prefixes.stream() |
100 |
| - .map(p -> Map.entry(s.indexOf(p), p.length())) |
101 |
| - .filter(e -> e.getKey() >= 0) |
102 |
| - .map(e -> e.getKey() + e.getValue()) |
103 |
| - .min(Comparator.comparing(Function.identity())) |
104 |
| - .map(classStart -> { |
105 |
| - var firstSpace = s.indexOf(" ", classStart + 1); |
106 |
| - var firstBracket = s.indexOf("(", classStart + 1); |
107 |
| - var firstSquareBracket = s.indexOf("{", classStart + 1); |
108 |
| - var classEnd = IntStream.of(firstSpace, firstBracket, firstSquareBracket) |
109 |
| - .filter(i -> i >= 0) |
110 |
| - .min() |
111 |
| - .getAsInt(); |
112 |
| - var className = s.substring(classStart, classEnd).trim(); |
113 |
| - int generic = className.indexOf('<'); |
114 |
| - if (generic == -1) { |
115 |
| - return className; |
116 |
| - } else { |
117 |
| - return className.substring(0, generic); |
118 |
| - } |
119 |
| - }) |
120 |
| - .get(); |
121 |
| - |
122 |
| - return new ByteArrayJavaFileObject(JavaFileObject.Kind.SOURCE, testPackage + "." + firstClass, s.getBytes(StandardCharsets.UTF_8)); |
123 |
| - }) |
124 |
| - .toList(); |
125 |
| - |
126 |
| - try (var delegate = javaCompiler.getStandardFileManager(diagnostic::add, Locale.US, StandardCharsets.UTF_8); |
127 |
| - var manager = new KoraCompileTestJavaFileManager(this.testInfo, delegate, sourceList.toArray(ByteArrayJavaFileObject[]::new))) { |
128 |
| - |
129 |
| - var defaultOptions = new LinkedHashSet<>(List.of("--release", "17", "-XprintRounds")); |
130 |
| - defaultOptions.addAll(processorOptions.stream().map(o -> o.value).toList()); |
131 |
| - |
132 |
| - var task = javaCompiler.getTask( |
133 |
| - w, |
134 |
| - manager, |
135 |
| - diagnostic::add, |
136 |
| - defaultOptions, |
137 |
| - null, |
138 |
| - sourceList |
139 |
| - ); |
140 |
| - task.setProcessors(processors); |
141 |
| - task.setLocale(Locale.US); |
142 |
| - task.call(); |
143 |
| - w.close(); |
144 |
| - return this.compileResult = new CompileResult(testPackage, diagnostic, manager); |
145 |
| - } catch (RuntimeException e) { |
146 |
| - if (e.getCause() instanceof RuntimeException er) { |
147 |
| - throw er; |
| 83 | + var sourceList = new ArrayList<Path>(); |
| 84 | + for (var source : sources) { |
| 85 | + var string = "package %s;\n%s\n/**\n* @see %s#%s \n*/\n".formatted(testPackage, commonImports, testClass.getCanonicalName(), testMethod.getName()) + source; |
| 86 | + var prefixes = List.of("class ", "interface ", "@interface ", "record ", "enum "); |
| 87 | + var firstClass = prefixes.stream() |
| 88 | + .map(p -> Map.entry(string.indexOf(p), p.length())) |
| 89 | + .filter(e -> e.getKey() >= 0) |
| 90 | + .map(e -> e.getKey() + e.getValue()) |
| 91 | + .min(Comparator.comparing(Function.identity())) |
| 92 | + .map(classStart -> { |
| 93 | + var firstSpace = string.indexOf(" ", classStart + 1); |
| 94 | + var firstBracket = string.indexOf("(", classStart + 1); |
| 95 | + var firstSquareBracket = string.indexOf("{", classStart + 1); |
| 96 | + var classEnd = IntStream.of(firstSpace, firstBracket, firstSquareBracket) |
| 97 | + .filter(i -> i >= 0) |
| 98 | + .min() |
| 99 | + .getAsInt(); |
| 100 | + var className = string.substring(classStart, classEnd).trim(); |
| 101 | + int generic = className.indexOf('<'); |
| 102 | + if (generic == -1) { |
| 103 | + return className; |
| 104 | + } else { |
| 105 | + return className.substring(0, generic); |
| 106 | + } |
| 107 | + }) |
| 108 | + .get(); |
| 109 | + var className = testPackage + "." + firstClass; |
| 110 | + var path = Paths.get(".", "build", "in-test-generated", "sources").resolve(className.replace('.', '/') + ".java"); |
| 111 | + try { |
| 112 | + Files.createDirectories(path.getParent()); |
| 113 | + Files.write(path, string.getBytes(StandardCharsets.UTF_8)); |
| 114 | + } catch (IOException e) { |
| 115 | + throw new RuntimeException(e); |
148 | 116 | }
|
| 117 | + sourceList.add(path); |
| 118 | + } |
| 119 | + |
| 120 | + try { |
| 121 | + var jc = new JavaCompilation() |
| 122 | + .withSources(sourceList) |
| 123 | + .withProcessors(processors); |
| 124 | + var cl = jc.compile(); |
| 125 | + return this.compileResult = new CompileResult(testPackage, jc.diagnostics(), cl); |
| 126 | + } catch (TestUtils.CompilationErrorException e) { |
| 127 | + return this.compileResult = new CompileResult(testPackage, e.diagnostics, null); |
| 128 | + } catch (RuntimeException e) { |
149 | 129 | throw e;
|
150 |
| - } catch (IOException e) { |
| 130 | + } catch (Exception e) { |
151 | 131 | throw new RuntimeException(e);
|
152 | 132 | }
|
153 | 133 | }
|
|
0 commit comments