Skip to content

Commit

Permalink
Add base infra for Web controllers hints
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll authored and sdeleuze committed Jun 10, 2022
1 parent 1e5f4f8 commit 5e56d9c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
@@ -0,0 +1,36 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.http;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.lang.Nullable;

/**
* {@link RuntimeHintsRegistrar} implementation that makes sure mime types
* are available in constrained environments.
*
* @author Stephane Nicoll
* @since 6.0
*/
class MediaTypeResourceHintsRegistrar implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
hints.resources().registerPattern("org/springframework/http/mime.types");
}
}
Expand Up @@ -22,6 +22,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.aot.hint.annotation.Reflective;
import org.springframework.core.annotation.AliasFor;

/**
Expand Down Expand Up @@ -72,6 +73,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
@Reflective(RequestMappingReflectiveProcessor.class)
public @interface RequestMapping {

/**
Expand Down
@@ -0,0 +1,41 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.bind.annotation;

import java.lang.reflect.Method;

import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
import org.springframework.aot.hint.annotation.SimpleReflectiveProcessor;

/**
* {@link ReflectiveProcessor} implementation for {@link RequestMapping}
* annotated types. On top of registering reflection hints for invoking
* the annotated method, this implementation handles return types that
* are serialized as well as TBD.
*
* @author Stephane Nicoll
* @since 6.0
*/
class RequestMappingReflectiveProcessor extends SimpleReflectiveProcessor {

@Override
protected void registerMethodHint(ReflectionHints hints, Method method) {
super.registerMethodHint(hints, method);
// TODO
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.bind.annotation;

import java.util.stream.Stream;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;

/**
* {@link RuntimeHintsRegistrar} implementation that make web binding
* annotations at runtime.
*
* @author Stephane Nicoll
* @since 6.0
*/
public final class WebAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
Stream.of(Controller.class, ControllerAdvice.class, CookieValue.class,
CrossOrigin.class, DeleteMapping.class, ExceptionHandler.class,
GetMapping.class, InitBinder.class, Mapping.class, MatrixVariable.class,
ModelAttribute.class, PatchMapping.class, PathVariable.class,
PostMapping.class, PutMapping.class, RequestAttribute.class,
RequestBody.class, RequestHeader.class, RequestMapping.class,
RequestParam.class, RequestPart.class, ResponseBody.class,
ResponseStatus.class, RestController.class, RestControllerAdvice.class,
SessionAttribute.class, SessionAttributes.class).forEach(
annotationType -> RuntimeHintsUtils.registerAnnotation(hints, annotationType));
}

}
@@ -0,0 +1 @@
Args = --initialize-at-build-time=org.springframework.http.HttpStatus
2 changes: 2 additions & 0 deletions spring-web/src/main/resources/META-INF/spring/aot.factories
@@ -0,0 +1,2 @@
org.springframework.aot.hint.RuntimeHintsRegistrar= \
org.springframework.http.MediaTypeResourceHintsRegistrar
Expand Up @@ -33,6 +33,7 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.SpringProperties;
import org.springframework.core.convert.converter.Converter;
Expand Down Expand Up @@ -70,6 +71,7 @@
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.WebAnnotationsRuntimeHintsRegistrar;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.cors.CorsConfiguration;
Expand Down Expand Up @@ -188,6 +190,7 @@
* @see EnableWebMvc
* @see WebMvcConfigurer
*/
@ImportRuntimeHints(WebAnnotationsRuntimeHintsRegistrar.class)
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {

/**
Expand Down

0 comments on commit 5e56d9c

Please sign in to comment.