Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 8f250b4

Browse files
christophstroblsdeleuze
authored andcommitted
Drop if necessary - Be smart on media type registration.
Use the (at) EnableHypermediaSupport annotation to only register configured media types, their configuration and jackson mappings which saves about 2.5 MB
1 parent 832991d commit 8f250b4

File tree

3 files changed

+103
-6
lines changed

3 files changed

+103
-6
lines changed

spring-aot/src/main/java/org/springframework/nativex/type/Type.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
import org.springframework.nativex.hint.TypeHint;
6868
import org.springframework.nativex.hint.TypeHints;
6969
import org.springframework.util.ClassUtils;
70+
import org.springframework.util.ObjectUtils;
71+
import org.springframework.util.StringUtils;
7072

7173
/**
7274
* @author Andy Clement
@@ -627,7 +629,34 @@ public void getAnnotationValuesInHierarchy(String lookingFor, List<String> seen,
627629
if (anno.desc.equals(lookingFor)) {
628630
List<Object> os = anno.values;
629631
for (int i = 0; i < os.size(); i += 2) {
630-
collector.put(os.get(i).toString(), os.get(i + 1).toString());
632+
if(os.get(i+1) instanceof List) {
633+
634+
List<String> resolvedAttributeValues = new ArrayList<>();
635+
636+
for(Object attributeValue : (List)os.get(i+1)) {
637+
638+
if(!ObjectUtils.isArray(attributeValue)) {
639+
resolvedAttributeValues.add(attributeValue.toString());
640+
continue;
641+
}
642+
643+
String attributeValueStringRepresentation = "";
644+
String[] args = (String[]) attributeValue;
645+
if(args[0].startsWith("L") && args[0].endsWith(";")) {
646+
Type resolve = typeSystem.Lresolve(args[0], true);
647+
if(resolve != null) {
648+
attributeValueStringRepresentation = resolve.getDottedName();
649+
}
650+
}
651+
if(args.length > 1) {
652+
attributeValueStringRepresentation += ("." + args[1]);
653+
}
654+
resolvedAttributeValues.add(attributeValueStringRepresentation);
655+
}
656+
collector.put(os.get(i).toString(), StringUtils.collectionToDelimitedString(resolvedAttributeValues, ";"));
657+
} else {
658+
collector.put(os.get(i).toString(), os.get(i + 1).toString());
659+
}
631660
}
632661
}
633662
try {

spring-native-configuration/src/main/java/org/springframework/hateoas/config/HateoasHints.java

+72-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.Set;
22+
import java.util.stream.Collectors;
23+
import java.util.stream.Stream;
2124

2225
import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration;
2326
import org.springframework.boot.autoconfigure.hateoas.HypermediaHttpMessageConverterConfiguration;
@@ -44,6 +47,7 @@
4447
import org.springframework.nativex.type.Type;
4548
import org.springframework.nativex.type.TypeProcessor;
4649
import org.springframework.nativex.type.TypeSystem;
50+
import org.springframework.util.StringUtils;
4751

4852

4953
@NativeHint(trigger = WebStackImportSelector.class, types = {
@@ -94,23 +98,64 @@ public class HateoasHints implements NativeConfiguration {
9498

9599
private static final String ENTITY_LINKS = "org/springframework/hateoas/server/EntityLinks";
96100
private static final String JACKSON_ANNOTATION = "Lcom/fasterxml/jackson/annotation/JacksonAnnotation;";
101+
private static final String ENABLE_HYPERMEDIA_SUPPORT = "Lorg/springframework/hateoas/config/EnableHypermediaSupport;";
97102

98103
@Override
99104
public List<HintDeclaration> computeHints(TypeSystem typeSystem) {
100105

106+
Set<String> hypermediaFormats = computeConfiguredHypermediaFormats(typeSystem);
107+
101108
List<HintDeclaration> hints = new ArrayList<>();
102109

103-
hints.addAll(computeAtConfigurationClasses(typeSystem));
110+
hints.addAll(computeAtConfigurationClasses(typeSystem, hypermediaFormats));
104111
hints.addAll(computeRepresentationModels(typeSystem));
105112
hints.addAll(computeEntityLinks(typeSystem));
106-
hints.addAll(computeJacksonMappings(typeSystem));
113+
hints.addAll(computeJacksonMappings(typeSystem, hypermediaFormats));
107114

108115
return hints;
109116
}
110117

111-
private List<HintDeclaration> computeAtConfigurationClasses(TypeSystem typeSystem) {
118+
private Set<String> computeConfiguredHypermediaFormats(TypeSystem typeSystem) {
119+
120+
return typeSystem.scanUserCodeDirectoriesAndSpringJars(type -> {
121+
try {
122+
return type.isAnnotated(ENABLE_HYPERMEDIA_SUPPORT);
123+
} catch (MissingTypeException e) {
124+
return false;
125+
}
126+
})
127+
.flatMap(type -> {
128+
try {
129+
String formats = type.getAnnotationValuesInHierarchy(ENABLE_HYPERMEDIA_SUPPORT).getOrDefault("type", "");
130+
return StringUtils.hasText(formats) ? Stream.of(formats.split(";")) : Stream.empty();
131+
} catch (MissingTypeException ex) {
132+
return Stream.empty();
133+
}
134+
})
135+
.map(it -> {
136+
String value = it.replaceAll(".*org\\.springframework\\.hateoas\\.config\\.EnableHypermediaSupport\\$HypermediaType\\.", "").toLowerCase();
137+
switch (value) {
138+
case "hal_forms":
139+
return "hal";
140+
case "collection_json":
141+
return "collectionjson";
142+
default:
143+
return value;
144+
}
145+
})
146+
.collect(Collectors.toSet());
147+
}
148+
149+
150+
private List<HintDeclaration> computeAtConfigurationClasses(TypeSystem typeSystem, Set<String> hypermediaFormats) {
112151

113152
return TypeProcessor.namedProcessor("HateoasHints - Configuration Classes")
153+
.skipTypesMatching(type -> {
154+
if (type.isPartOfDomain("org.springframework.hateoas.mediatype")) {
155+
return !isSupportedHypermediaFormat(type, hypermediaFormats);
156+
}
157+
return false;
158+
})
114159
.skipAnnotationInspection()
115160
.skipMethodInspection()
116161
.skipFieldInspection()
@@ -159,7 +204,7 @@ List<HintDeclaration> computeRepresentationModels(TypeSystem typeSystem) {
159204
});
160205
}
161206

162-
List<HintDeclaration> computeJacksonMappings(TypeSystem typeSystem) {
207+
List<HintDeclaration> computeJacksonMappings(TypeSystem typeSystem, Set<String> hypermediaFormats) {
163208

164209
return TypeProcessor.namedProcessor("HateoasHints - Jackson Mapping Candidates")
165210
.skipTypesMatching(type -> {
@@ -170,6 +215,7 @@ List<HintDeclaration> computeJacksonMappings(TypeSystem typeSystem) {
170215
if (!type.isPartOfDomain("org.springframework.hateoas.")) {
171216
return true;
172217
}
218+
return !isSupportedHypermediaFormat(type, hypermediaFormats);
173219
}
174220
return false;
175221
})
@@ -212,4 +258,26 @@ private boolean usesJackson(Type type) {
212258

213259
return false;
214260
}
261+
262+
private boolean isSupportedHypermediaFormat(Type type, Set<String> hypermediaFormats) {
263+
264+
if (hypermediaFormats.isEmpty()) {
265+
return true;
266+
}
267+
268+
if (!type.isPartOfDomain("org.springframework.hateoas.mediatype")) {
269+
return true;
270+
}
271+
272+
if (type.getPackageName().equals("org.springframework.hateoas.mediatype")) {
273+
return true;
274+
}
275+
276+
for (String hypermediaFormat : hypermediaFormats) {
277+
if (type.getPackageName().contains(hypermediaFormat)) {
278+
return true;
279+
}
280+
}
281+
return false;
282+
}
215283
}

spring-native-configuration/src/test/java/org/springframework/hateoas/config/HateoasHintsTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void computesModelAndModelProcessorCorrectly() {
6767
@Test
6868
public void jackson() {
6969

70-
List<HintDeclaration> hintDeclarations = hateoasHints.computeJacksonMappings(typeSystem);
70+
List<HintDeclaration> hintDeclarations = hateoasHints.computeJacksonMappings(typeSystem, Collections.emptySet());
7171
assertHints(hintDeclarations)
7272
// Assert only the ones using jackson
7373
.hasSize(7)

0 commit comments

Comments
 (0)