• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.61
today-context/src/main/java/infra/context/aot/AbstractAotProcessor.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.aot;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.io.IOException;
23
import java.nio.file.Path;
24

25
import infra.aot.generate.FileSystemGeneratedFiles;
26
import infra.aot.generate.GeneratedFiles.Kind;
27
import infra.aot.hint.RuntimeHints;
28
import infra.aot.nativex.FileNativeConfigurationWriter;
29
import infra.lang.Assert;
30
import infra.util.FileSystemUtils;
31

32
/**
33
 * Abstract base class for filesystem-based ahead-of-time (AOT) processing.
34
 *
35
 * <p>Concrete implementations should override {@link #doProcess()} that kicks
36
 * off the optimization of the target, usually an application.
37
 *
38
 * @param <T> the type of the processing result
39
 * @author Stephane Nicoll
40
 * @author Andy Wilkinson
41
 * @author Phillip Webb
42
 * @author Sam Brannen
43
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
44
 * @see FileSystemGeneratedFiles
45
 * @see FileNativeConfigurationWriter
46
 * @see ContextAotProcessor
47
 * @see infra.test.context.aot.TestAotProcessor
48
 * @since 4.0
49
 */
50
public abstract class AbstractAotProcessor<T> {
51

52
  /**
53
   * The name of a system property that is made available when the processor
54
   * runs.
55
   *
56
   * @see #doProcess()
57
   */
58
  public static final String AOT_PROCESSING = "infra.aot.processing";
59

60
  private final Settings settings;
61

62
  /**
63
   * Create a new processor instance with the supplied {@linkplain Settings settings}.
64
   *
65
   * @see Settings#builder()
66
   */
67
  protected AbstractAotProcessor(Settings settings) {
2✔
68
    this.settings = settings;
3✔
69
  }
1✔
70

71
  /**
72
   * Get the {@linkplain Settings settings} for this AOT processor.
73
   */
74
  protected Settings getSettings() {
75
    return this.settings;
3✔
76
  }
77

78
  /**
79
   * Run AOT processing.
80
   *
81
   * @return the result of the processing.
82
   */
83
  public final T process() {
84
    try {
85
      System.setProperty(AOT_PROCESSING, "true");
4✔
86
      return doProcess();
5✔
87
    }
88
    finally {
89
      System.clearProperty(AOT_PROCESSING);
3✔
90
    }
91
  }
92

93
  protected abstract T doProcess();
94

95
  /**
96
   * Delete the source, resource, and class output directories.
97
   */
98
  protected void deleteExistingOutput() {
99
    deleteExistingOutput(getSettings().getSourceOutput(),
13✔
100
            getSettings().getResourceOutput(), getSettings().getClassOutput());
9✔
101
  }
1✔
102

103
  private void deleteExistingOutput(Path... paths) {
104
    for (Path path : paths) {
16✔
105
      try {
106
        FileSystemUtils.deleteRecursively(path);
3✔
107
      }
108
      catch (IOException ex) {
×
109
        throw new IllegalStateException("Failed to delete existing output in '" + path + "'");
×
110
      }
1✔
111
    }
112
  }
1✔
113

114
  protected FileSystemGeneratedFiles createFileSystemGeneratedFiles() {
115
    return new FileSystemGeneratedFiles(this::getRoot);
6✔
116
  }
117

118
  private Path getRoot(Kind kind) {
119
    return switch (kind) {
6✔
120
      case CLASS -> getSettings().getClassOutput();
4✔
121
      case SOURCE -> getSettings().getSourceOutput();
4✔
122
      case RESOURCE -> getSettings().getResourceOutput();
3✔
123
    };
124
  }
125

126
  protected void writeHints(RuntimeHints hints) {
127
    FileNativeConfigurationWriter writer = new FileNativeConfigurationWriter(
3✔
128
            getSettings().getResourceOutput(), getSettings().getGroupId(), getSettings().getArtifactId());
10✔
129
    writer.write(hints);
3✔
130
  }
1✔
131

132
  /**
133
   * Common settings for AOT processors.
134
   */
135
  public static final class Settings {
136

137
    private final Path sourceOutput;
138

139
    private final Path resourceOutput;
140

141
    private final Path classOutput;
142

143
    @Nullable
144
    private final String groupId;
145

146
    @Nullable
147
    private final String artifactId;
148

149
    private Settings(Path sourceOutput, Path resourceOutput,
150
            Path classOutput, @Nullable String groupId, @Nullable String artifactId) {
2✔
151
      this.groupId = groupId;
3✔
152
      this.artifactId = artifactId;
3✔
153
      this.classOutput = classOutput;
3✔
154
      this.sourceOutput = sourceOutput;
3✔
155
      this.resourceOutput = resourceOutput;
3✔
156
    }
1✔
157

158
    /**
159
     * Create a new {@link Builder} for {@link Settings}.
160
     */
161
    public static Builder builder() {
162
      return new Builder();
4✔
163
    }
164

165
    /**
166
     * Get the output directory for generated sources.
167
     */
168
    public Path getSourceOutput() {
169
      return this.sourceOutput;
3✔
170
    }
171

172
    /**
173
     * Get the output directory for generated resources.
174
     */
175
    public Path getResourceOutput() {
176
      return this.resourceOutput;
3✔
177
    }
178

179
    /**
180
     * Get the output directory for generated classes.
181
     */
182
    public Path getClassOutput() {
183
      return this.classOutput;
3✔
184
    }
185

186
    /**
187
     * Get the group ID of the application.
188
     */
189
    @Nullable
190
    public String getGroupId() {
191
      return this.groupId;
3✔
192
    }
193

194
    /**
195
     * Get the artifact ID of the application.
196
     */
197
    @Nullable
198
    public String getArtifactId() {
199
      return this.artifactId;
3✔
200
    }
201

202
    /**
203
     * Fluent builder API for {@link Settings}.
204
     */
205
    public static final class Builder {
206

207
      @Nullable
208
      private Path sourceOutput;
209

210
      @Nullable
211
      private Path resourceOutput;
212

213
      @Nullable
214
      private Path classOutput;
215

216
      @Nullable
217
      private String groupId;
218

219
      @Nullable
220
      private String artifactId;
221

222
      private Builder() {
223
        // internal constructor
224
      }
225

226
      /**
227
       * Set the output directory for generated sources.
228
       *
229
       * @param sourceOutput the location of generated sources
230
       * @return this builder for method chaining
231
       */
232
      public Builder sourceOutput(Path sourceOutput) {
233
        this.sourceOutput = sourceOutput;
3✔
234
        return this;
2✔
235
      }
236

237
      /**
238
       * Set the output directory for generated resources.
239
       *
240
       * @param resourceOutput the location of generated resources
241
       * @return this builder for method chaining
242
       */
243
      public Builder resourceOutput(Path resourceOutput) {
244
        this.resourceOutput = resourceOutput;
3✔
245
        return this;
2✔
246
      }
247

248
      /**
249
       * Set the output directory for generated classes.
250
       *
251
       * @param classOutput the location of generated classes
252
       * @return this builder for method chaining
253
       */
254
      public Builder classOutput(Path classOutput) {
255
        this.classOutput = classOutput;
3✔
256
        return this;
2✔
257
      }
258

259
      /**
260
       * Set the group ID of the application.
261
       *
262
       * @param groupId the group ID of the application, used to locate
263
       * {@code native-image.properties}
264
       * @return this builder for method chaining
265
       */
266
      public Builder groupId(String groupId) {
267
        this.groupId = groupId;
3✔
268
        return this;
2✔
269
      }
270

271
      /**
272
       * Set the artifact ID of the application.
273
       *
274
       * @param artifactId the artifact ID of the application, used to locate
275
       * {@code native-image.properties}
276
       * @return this builder for method chaining
277
       */
278
      public Builder artifactId(String artifactId) {
279
        this.artifactId = artifactId;
3✔
280
        return this;
2✔
281
      }
282

283
      /**
284
       * Build the {@link Settings} configured in this {@code Builder}.
285
       */
286
      public Settings build() {
287
        Assert.notNull(this.sourceOutput, "'sourceOutput' is required");
4✔
288
        Assert.notNull(this.resourceOutput, "'resourceOutput' is required");
4✔
289
        Assert.notNull(this.classOutput, "'classOutput' is required");
4✔
290
        Assert.hasText(this.groupId, "'groupId' must not be null or empty");
4✔
291
        Assert.hasText(this.artifactId, "'artifactId' must not be null or empty");
4✔
292
        return new Settings(this.sourceOutput, this.resourceOutput, this.classOutput,
14✔
293
                this.groupId, this.artifactId);
294
      }
295

296
    }
297

298
  }
299

300
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc