Skip to content

Commit 200695d

Browse files
authored
Application context improvements (#12259)
1 parent 4c87700 commit 200695d

File tree

16 files changed

+480
-124
lines changed

16 files changed

+480
-124
lines changed

context/src/main/java/io/micronaut/runtime/Micronaut.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import io.micronaut.context.banner.ResourceBanner;
2525
import io.micronaut.context.env.Environment;
2626
import io.micronaut.context.env.PropertySource;
27-
import org.jspecify.annotations.NonNull;
27+
import org.jspecify.annotations.NullMarked;
2828
import org.jspecify.annotations.Nullable;
2929
import io.micronaut.core.naming.Described;
3030
import io.micronaut.runtime.exceptions.ApplicationStartupException;
@@ -47,6 +47,7 @@
4747
* @author Graeme Rocher
4848
* @since 1.0
4949
*/
50+
@NullMarked
5051
public class Micronaut extends DefaultApplicationContextBuilder implements ApplicationContextBuilder {
5152
private static final String BANNER_NAME = "micronaut-banner.txt";
5253
private static final Logger LOG = LoggerFactory.getLogger(Micronaut.class);
@@ -65,7 +66,7 @@ protected Micronaut() {
6566
*/
6667
@Override
6768
@SuppressWarnings({"java:S1181", "java:S3776", "java:S1141"})
68-
public @NonNull ApplicationContext start() {
69+
public ApplicationContext start() {
6970
long start = System.nanoTime();
7071
printBanner();
7172
ApplicationContext applicationContext = super.build();
@@ -195,17 +196,17 @@ private static long elapsedMillis(long startNanos) {
195196
}
196197

197198
@Override
198-
public @NonNull Micronaut include(@Nullable String... configurations) {
199+
public Micronaut include(@Nullable String... configurations) {
199200
return (Micronaut) super.include(configurations);
200201
}
201202

202203
@Override
203-
public @NonNull Micronaut exclude(@Nullable String... configurations) {
204+
public Micronaut exclude(@Nullable String... configurations) {
204205
return (Micronaut) super.exclude(configurations);
205206
}
206207

207208
@Override
208-
public @NonNull Micronaut banner(boolean isEnabled) {
209+
public Micronaut banner(boolean isEnabled) {
209210
return (Micronaut) super.banner(isEnabled);
210211
}
211212

@@ -215,7 +216,7 @@ private static long elapsedMillis(long startNanos) {
215216
* @param classes The application
216217
* @return The classes
217218
*/
218-
public @NonNull Micronaut classes(@Nullable Class<?>... classes) {
219+
public Micronaut classes(@Nullable Class<?>... classes) {
219220
if (classes != null) {
220221
for (Class<?> aClass : classes) {
221222
packages(aClass.getPackage().getName());
@@ -225,67 +226,67 @@ private static long elapsedMillis(long startNanos) {
225226
}
226227

227228
@Override
228-
public @NonNull Micronaut properties(@Nullable Map<String, Object> properties) {
229+
public Micronaut properties(@Nullable Map<String, Object> properties) {
229230
return (Micronaut) super.properties(properties);
230231
}
231232

232233
@Override
233-
public @NonNull Micronaut singletons(Object... beans) {
234+
public Micronaut singletons(Object... beans) {
234235
return (Micronaut) super.singletons(beans);
235236
}
236237

237238
@Override
238-
public Micronaut beanDefinitions(@NonNull RuntimeBeanDefinition<?>... definitions) {
239+
public Micronaut beanDefinitions(RuntimeBeanDefinition<?>... definitions) {
239240
return (Micronaut) super.beanDefinitions(definitions);
240241
}
241242

242243
@Override
243-
public @NonNull Micronaut propertySources(@Nullable PropertySource... propertySources) {
244+
public Micronaut propertySources(@Nullable PropertySource... propertySources) {
244245
return (Micronaut) super.propertySources(propertySources);
245246
}
246247

247248
@Override
248-
public @NonNull Micronaut environmentPropertySource(boolean environmentPropertySource) {
249+
public Micronaut environmentPropertySource(boolean environmentPropertySource) {
249250
return (Micronaut) super.environmentPropertySource(environmentPropertySource);
250251
}
251252

252253
@Override
253-
public @NonNull Micronaut environmentVariableIncludes(@Nullable String... environmentVariables) {
254+
public Micronaut environmentVariableIncludes(@Nullable String... environmentVariables) {
254255
return (Micronaut) super.environmentVariableIncludes(environmentVariables);
255256
}
256257

257258
@Override
258-
public @NonNull Micronaut environmentVariableExcludes(@Nullable String... environmentVariables) {
259+
public Micronaut environmentVariableExcludes(@Nullable String... environmentVariables) {
259260
return (Micronaut) super.environmentVariableExcludes(environmentVariables);
260261
}
261262

262263
@Override
263-
public @NonNull Micronaut mainClass(Class<?> mainClass) {
264+
public Micronaut mainClass(@Nullable Class<?> mainClass) {
264265
return (Micronaut) super.mainClass(mainClass);
265266
}
266267

267268
@Override
268-
public @NonNull Micronaut classLoader(ClassLoader classLoader) {
269+
public Micronaut classLoader(@Nullable ClassLoader classLoader) {
269270
return (Micronaut) super.classLoader(classLoader);
270271
}
271272

272273
@Override
273-
public @NonNull Micronaut args(@Nullable String... args) {
274+
public Micronaut args(@Nullable String... args) {
274275
return (Micronaut) super.args(args);
275276
}
276277

277278
@Override
278-
public @NonNull Micronaut environments(@Nullable String... environments) {
279+
public Micronaut environments(@Nullable String... environments) {
279280
return (Micronaut) super.environments(environments);
280281
}
281282

282283
@Override
283-
public @NonNull Micronaut defaultEnvironments(@Nullable String... environments) {
284+
public Micronaut defaultEnvironments(@Nullable String... environments) {
284285
return (Micronaut) super.defaultEnvironments(environments);
285286
}
286287

287288
@Override
288-
public @NonNull Micronaut packages(@Nullable String... packages) {
289+
public Micronaut packages(@Nullable String... packages) {
289290
return (Micronaut) super.packages(packages);
290291
}
291292

@@ -356,7 +357,7 @@ public static ApplicationContext run(Class<?>[] classes, String... args) {
356357
*/
357358
protected void handleStartupException(Environment environment, Throwable exception) {
358359
Function<Throwable, Integer> exitCodeMapper = exitHandlers.computeIfAbsent(exception.getClass(), exceptionType -> (throwable -> 1));
359-
Integer code = exitCodeMapper.apply(exception);
360+
int code = exitCodeMapper.apply(exception);
360361
if (code > 0 && !environment.getActiveNames().contains(Environment.TEST)) {
361362
if (LOG.isErrorEnabled()) {
362363
LOG.error("Error starting Micronaut server: {}", exception.getMessage(), exception);
@@ -375,8 +376,7 @@ private void printBanner() {
375376
resolveBanner(out).print();
376377
}
377378

378-
@NonNull
379-
private Banner resolveBanner(@NonNull PrintStream out) {
379+
private Banner resolveBanner(PrintStream out) {
380380
return getResourceLoader().getResource(BANNER_NAME)
381381
.map(resource -> (Banner) new ResourceBanner(resource, out))
382382
.orElseGet(() -> new MicronautBanner(out));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2017-2025 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.core.io.scan;
17+
18+
import io.micronaut.core.annotation.Experimental;
19+
import io.micronaut.core.io.ResourceLoader;
20+
import org.jspecify.annotations.NullMarked;
21+
22+
import java.io.InputStream;
23+
import java.net.URL;
24+
import java.util.Optional;
25+
import java.util.stream.Stream;
26+
27+
/**
28+
* A simple {@link ClassPathResourceLoader} that uses a {@link Class}.
29+
*
30+
* @author Denis Stepanov
31+
* @since 5.0
32+
*/
33+
@Experimental
34+
@NullMarked
35+
public class ClassClassPathResourceLoader implements ClassPathResourceLoader {
36+
37+
private final Class<?> clazz;
38+
private final String basePath;
39+
40+
public ClassClassPathResourceLoader(Class<?> clazz) {
41+
this(clazz, "");
42+
}
43+
44+
public ClassClassPathResourceLoader(Class<?> clazz, String basePath) {
45+
this.clazz = clazz;
46+
this.basePath = basePath;
47+
}
48+
49+
@Override
50+
public ClassLoader getClassLoader() {
51+
return clazz.getClassLoader();
52+
}
53+
54+
@Override
55+
public Optional<InputStream> getResourceAsStream(String path) {
56+
return Optional.ofNullable(clazz.getResourceAsStream(basePath + path));
57+
}
58+
59+
@Override
60+
public Optional<URL> getResource(String path) {
61+
return Optional.ofNullable(clazz.getResource(path));
62+
}
63+
64+
@Override
65+
public Stream<URL> getResources(String name) {
66+
return clazz.getClassLoader().resources(name);
67+
}
68+
69+
@Override
70+
public ResourceLoader forBase(String basePath) {
71+
return new ClassClassPathResourceLoader(clazz, this.basePath + basePath);
72+
}
73+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2017-2025 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.core.io.scan;
17+
18+
import io.micronaut.core.annotation.Experimental;
19+
import io.micronaut.core.io.ResourceLoader;
20+
import org.jspecify.annotations.NullMarked;
21+
22+
import java.io.InputStream;
23+
import java.net.URL;
24+
import java.util.Optional;
25+
import java.util.stream.Stream;
26+
27+
/**
28+
* A simple {@link ClassPathResourceLoader} that uses a {@link ClassLoader}.
29+
*
30+
* @author Denis Stepanov
31+
* @since 5.0
32+
*/
33+
@Experimental
34+
@NullMarked
35+
public final class ClassLoaderClassPathResourceLoader implements ClassPathResourceLoader {
36+
37+
private final ClassLoader classLoader;
38+
private final String basePath;
39+
40+
public ClassLoaderClassPathResourceLoader() {
41+
this(ClassPathResourceLoader.defaultLoader(null).getClassLoader(), "");
42+
}
43+
44+
public ClassLoaderClassPathResourceLoader(ClassLoader classLoader) {
45+
this(classLoader, "");
46+
}
47+
48+
public ClassLoaderClassPathResourceLoader(ClassLoader classLoader, String basePath) {
49+
this.classLoader = classLoader;
50+
this.basePath = basePath;
51+
}
52+
53+
@Override
54+
public ClassLoader getClassLoader() {
55+
return classLoader;
56+
}
57+
58+
@Override
59+
public Optional<InputStream> getResourceAsStream(String path) {
60+
return Optional.ofNullable(classLoader.getResourceAsStream(basePath + path));
61+
}
62+
63+
@Override
64+
public Optional<URL> getResource(String path) {
65+
return Optional.ofNullable(classLoader.getResource(path));
66+
}
67+
68+
@Override
69+
public Stream<URL> getResources(String name) {
70+
return classLoader.resources(name);
71+
}
72+
73+
@Override
74+
public ResourceLoader forBase(String basePath) {
75+
return new ClassLoaderClassPathResourceLoader(classLoader, this.basePath + basePath);
76+
}
77+
}

core/src/main/java/io/micronaut/core/io/scan/ClassPathResourceLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.micronaut.core.io.ResourceLoader;
1919

20+
import org.jspecify.annotations.NullMarked;
2021
import org.jspecify.annotations.Nullable;
2122

2223
/**
@@ -26,6 +27,7 @@
2627
* @author Graeme Rocher
2728
* @since 1.0
2829
*/
30+
@NullMarked
2931
public interface ClassPathResourceLoader extends ResourceLoader {
3032

3133
/**

0 commit comments

Comments
 (0)