Skip to content

Commit 0992f85

Browse files
snicollsdeleuze
authored andcommitted
Add base infra for Web controllers hints
See spring-projectsgh-28518
1 parent 472af9c commit 0992f85

File tree

7 files changed

+135
-0
lines changed

7 files changed

+135
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2022 the original author or 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+
17+
package org.springframework.http;
18+
19+
import org.springframework.aot.hint.RuntimeHints;
20+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* {@link RuntimeHintsRegistrar} implementation that makes sure mime types
25+
* are available in constrained environments.
26+
*
27+
* @author Stephane Nicoll
28+
* @since 6.0
29+
*/
30+
class MediaTypeResourceHintsRegistrar implements RuntimeHintsRegistrar {
31+
32+
@Override
33+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
34+
hints.resources().registerPattern("org/springframework/http/mime.types");
35+
}
36+
}

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25+
import org.springframework.aot.hint.annotation.Reflective;
2526
import org.springframework.core.annotation.AliasFor;
2627

2728
/**
@@ -72,6 +73,7 @@
7273
@Retention(RetentionPolicy.RUNTIME)
7374
@Documented
7475
@Mapping
76+
@Reflective(RequestMappingReflectiveProcessor.class)
7577
public @interface RequestMapping {
7678

7779
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2022 the original author or 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+
17+
package org.springframework.web.bind.annotation;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.springframework.aot.hint.ReflectionHints;
22+
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
23+
import org.springframework.aot.hint.annotation.SimpleReflectiveProcessor;
24+
25+
/**
26+
* {@link ReflectiveProcessor} implementation for {@link RequestMapping}
27+
* annotated types. On top of registering reflection hints for invoking
28+
* the annotated method, this implementation handles return types that
29+
* are serialized as well as TBD.
30+
*
31+
* @author Stephane Nicoll
32+
* @since 6.0
33+
*/
34+
class RequestMappingReflectiveProcessor extends SimpleReflectiveProcessor {
35+
36+
@Override
37+
protected void registerMethodHint(ReflectionHints hints, Method method) {
38+
super.registerMethodHint(hints, method);
39+
// TODO
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2022 the original author or 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+
17+
package org.springframework.web.bind.annotation;
18+
19+
import java.util.stream.Stream;
20+
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.aot.hint.support.RuntimeHintsUtils;
24+
import org.springframework.lang.Nullable;
25+
import org.springframework.stereotype.Controller;
26+
27+
/**
28+
* {@link RuntimeHintsRegistrar} implementation that make web binding
29+
* annotations at runtime.
30+
*
31+
* @author Stephane Nicoll
32+
* @since 6.0
33+
*/
34+
public final class WebAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
35+
36+
@Override
37+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
38+
Stream.of(Controller.class, ControllerAdvice.class, CookieValue.class,
39+
CrossOrigin.class, DeleteMapping.class, ExceptionHandler.class,
40+
GetMapping.class, InitBinder.class, Mapping.class, MatrixVariable.class,
41+
ModelAttribute.class, PatchMapping.class, PathVariable.class,
42+
PostMapping.class, PutMapping.class, RequestAttribute.class,
43+
RequestBody.class, RequestHeader.class, RequestMapping.class,
44+
RequestParam.class, RequestPart.class, ResponseBody.class,
45+
ResponseStatus.class, RestController.class, RestControllerAdvice.class,
46+
SessionAttribute.class, SessionAttributes.class).forEach(
47+
annotationType -> RuntimeHintsUtils.registerAnnotation(hints, annotationType));
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Args = --initialize-at-build-time=org.springframework.http.HttpStatus
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.aot.hint.RuntimeHintsRegistrar= \
2+
org.springframework.http.MediaTypeResourceHintsRegistrar

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.context.ApplicationContextAware;
3434
import org.springframework.context.annotation.Bean;
3535
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.context.annotation.ImportRuntimeHints;
3637
import org.springframework.context.annotation.Lazy;
3738
import org.springframework.core.SpringProperties;
3839
import org.springframework.core.convert.converter.Converter;
@@ -70,6 +71,7 @@
7071
import org.springframework.web.HttpRequestHandler;
7172
import org.springframework.web.accept.ContentNegotiationManager;
7273
import org.springframework.web.bind.WebDataBinder;
74+
import org.springframework.web.bind.annotation.WebAnnotationsRuntimeHintsRegistrar;
7375
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
7476
import org.springframework.web.context.ServletContextAware;
7577
import org.springframework.web.cors.CorsConfiguration;
@@ -188,6 +190,7 @@
188190
* @see EnableWebMvc
189191
* @see WebMvcConfigurer
190192
*/
193+
@ImportRuntimeHints(WebAnnotationsRuntimeHintsRegistrar.class)
191194
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
192195

193196
/**

0 commit comments

Comments
 (0)