1515 */
1616package org .springframework .hateoas .aot ;
1717
18- import java .io .IOException ;
1918import java .util .Arrays ;
20- import java .util .Comparator ;
2119import java .util .HashSet ;
2220import java .util .List ;
2321import java .util .Set ;
24- import java .util .function .Predicate ;
2522import java .util .stream .Stream ;
2623
27- import org .slf4j .Logger ;
28- import org .slf4j .LoggerFactory ;
2924import org .springframework .aot .generate .GenerationContext ;
30- import org .springframework .aot .hint .MemberCategory ;
31- import org .springframework .aot .hint .TypeReference ;
3225import org .springframework .beans .factory .aot .BeanRegistrationAotContribution ;
3326import org .springframework .beans .factory .aot .BeanRegistrationAotProcessor ;
3427import org .springframework .beans .factory .aot .BeanRegistrationCode ;
3528import org .springframework .beans .factory .support .RegisteredBean ;
3629import org .springframework .core .annotation .AnnotatedElementUtils ;
37- import org .springframework .core .annotation .MergedAnnotation ;
38- import org .springframework .core .type .classreading .MetadataReader ;
39- import org .springframework .core .type .classreading .MetadataReaderFactory ;
40- import org .springframework .core .type .filter .TypeFilter ;
41- import org .springframework .hateoas .aot .AotUtils .FullTypeScanner ;
4230import org .springframework .hateoas .config .EnableHypermediaSupport ;
4331import org .springframework .hateoas .config .EnableHypermediaSupport .HypermediaType ;
4432import org .springframework .util .Assert ;
@@ -80,8 +68,6 @@ public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registe
8068
8169 static class MediaTypeReflectionAotContribution implements BeanRegistrationAotContribution {
8270
83- private static final Logger LOGGER = LoggerFactory .getLogger (MediaTypeReflectionAotContribution .class );
84-
8571 private final List <String > mediaTypePackage ;
8672 private final Set <String > packagesSeen ;
8773
@@ -105,8 +91,6 @@ public MediaTypeReflectionAotContribution(List<String> mediaTypePackage) {
10591 @ Override
10692 public void applyTo (GenerationContext generationContext , BeanRegistrationCode beanRegistrationCode ) {
10793
108- var reflection = generationContext .getRuntimeHints ().reflection ();
109-
11094 mediaTypePackage .forEach (it -> {
11195
11296 if (packagesSeen .contains (it )) {
@@ -115,97 +99,9 @@ public void applyTo(GenerationContext generationContext, BeanRegistrationCode be
11599
116100 packagesSeen .add (it );
117101
118- // Register RepresentationModel types for full reflection
119- FullTypeScanner provider = AotUtils .getScanner (it , //
120- new JacksonAnnotationPresentFilter (), //
121- new JacksonSuperTypeFilter ());
122-
123- LOGGER .info ("Registering Spring HATEOAS types in {} for reflection." , it );
124-
125- provider .findClasses ()
126- .sorted (Comparator .comparing (TypeReference ::getName ))
127- .peek (type -> LOGGER .debug ("> {}" , type .getName ()))
128- .forEach (reference -> reflection .registerType (reference , //
129- MemberCategory .INVOKE_DECLARED_CONSTRUCTORS , MemberCategory .INVOKE_DECLARED_METHODS ));
102+ new HypermediaTypesRuntimeHints (it ) //
103+ .registerHints (generationContext .getRuntimeHints (), getClass ().getClassLoader ());
130104 });
131105 }
132106 }
133-
134- static abstract class TraversingTypeFilter implements TypeFilter {
135-
136- /*
137- * (non-Javadoc)
138- * @see org.springframework.core.type.filter.TypeFilter#match(org.springframework.core.type.classreading.MetadataReader, org.springframework.core.type.classreading.MetadataReaderFactory)
139- */
140- @ Override
141- public boolean match (MetadataReader metadataReader , MetadataReaderFactory metadataReaderFactory )
142- throws IOException {
143-
144- if (doMatch (metadataReader , metadataReaderFactory )) {
145- return true ;
146- }
147-
148- var classMetadata = metadataReader .getClassMetadata ();
149-
150- String superClassName = classMetadata .getSuperClassName ();
151-
152- if (superClassName != null && !superClassName .startsWith ("java" )
153- && match (metadataReaderFactory .getMetadataReader (superClassName ), metadataReaderFactory )) {
154- return true ;
155- }
156-
157- for (String names : classMetadata .getInterfaceNames ()) {
158-
159- MetadataReader reader = metadataReaderFactory .getMetadataReader (names );
160-
161- if (match (reader , metadataReaderFactory )) {
162- return true ;
163- }
164- }
165-
166- return false ;
167- }
168-
169- protected abstract boolean doMatch (MetadataReader reader , MetadataReaderFactory factory );
170- }
171-
172- static class JacksonAnnotationPresentFilter extends TraversingTypeFilter {
173-
174- private static final Predicate <String > IS_JACKSON_ANNOTATION = it -> it .startsWith ("com.fasterxml.jackson" );
175-
176- /*
177- * (non-Javadoc)
178- * @see org.springframework.hateoas.aot.HateoasRuntimeHints.TraversingTypeFilter#doMatch(org.springframework.core.type.classreading.MetadataReader, org.springframework.core.type.classreading.MetadataReaderFactory)
179- */
180- @ Override
181- protected boolean doMatch (MetadataReader reader , MetadataReaderFactory factory ) {
182-
183- var annotationMetadata = reader .getAnnotationMetadata ();
184-
185- // Type annotations
186- return annotationMetadata
187- .getAnnotationTypes ()
188- .stream ()
189- .anyMatch (IS_JACKSON_ANNOTATION )
190-
191- // Method annotations
192- || annotationMetadata .getDeclaredMethods ().stream ()
193- .flatMap (it -> it .getAnnotations ().stream ())
194- .map (MergedAnnotation ::getType )
195- .map (Class ::getName )
196- .anyMatch (IS_JACKSON_ANNOTATION );
197- }
198- }
199-
200- static class JacksonSuperTypeFilter extends TraversingTypeFilter {
201-
202- /*
203- * (non-Javadoc)
204- * @see org.springframework.hateoas.aot.HateoasRuntimeHints.TraversingTypeFilter#doMatch(org.springframework.core.type.classreading.MetadataReader, org.springframework.core.type.classreading.MetadataReaderFactory)
205- */
206- @ Override
207- protected boolean doMatch (MetadataReader reader , MetadataReaderFactory factory ) {
208- return reader .getClassMetadata ().getClassName ().startsWith ("com.fasterxml.jackson" );
209- }
210- }
211107}
0 commit comments