1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .boot .env ;
18
18
19
+ import java .lang .invoke .MethodHandle ;
20
+ import java .lang .invoke .MethodHandles ;
21
+ import java .lang .invoke .MethodType ;
19
22
import java .util .ArrayList ;
20
23
import java .util .List ;
21
24
import java .util .Map ;
45
48
import org .springframework .boot .origin .TextResourceOrigin ;
46
49
import org .springframework .boot .origin .TextResourceOrigin .Location ;
47
50
import org .springframework .core .io .Resource ;
51
+ import org .springframework .util .ReflectionUtils ;
48
52
49
53
/**
50
54
* Class to load {@code .yml} files into a map of {@code String} to
@@ -73,8 +77,8 @@ protected Yaml createYaml() {
73
77
74
78
private Yaml createYaml (LoaderOptions loaderOptions ) {
75
79
BaseConstructor constructor = new OriginTrackingConstructor (loaderOptions );
76
- Representer representer = new Representer ();
77
80
DumperOptions dumperOptions = new DumperOptions ();
81
+ Representer representer = new Representer (dumperOptions );
78
82
LimitedResolver resolver = new LimitedResolver ();
79
83
return new Yaml (constructor , representer , dumperOptions , loaderOptions , resolver );
80
84
}
@@ -167,7 +171,12 @@ private static Node get(Node node) {
167
171
/**
168
172
* {@link Resolver} that limits {@link Tag#TIMESTAMP} tags.
169
173
*/
170
- private static class LimitedResolver extends Resolver {
174
+ static class LimitedResolver extends Resolver {
175
+
176
+ private static final MethodType SNAKE_YAML_2_ADD_IMPLICIT_RESOLVER_METHOD_TYPE = MethodType
177
+ .methodType (void .class , Tag .class , Pattern .class , String .class , int .class );
178
+
179
+ private volatile MethodHandle snakeYaml2ImplicitResolverMethod ;
171
180
172
181
@ Override
173
182
public void addImplicitResolver (Tag tag , Pattern regexp , String first ) {
@@ -177,6 +186,30 @@ public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
177
186
super .addImplicitResolver (tag , regexp , first );
178
187
}
179
188
189
+ public void addImplicitResolver (Tag tag , Pattern regexp , String first , int limit ) {
190
+ // Support for SnakeYAML 2.0
191
+ if (tag == Tag .TIMESTAMP ) {
192
+ return ;
193
+ }
194
+ try {
195
+ getSnakeYaml2ImplicitResolverMethod ().invoke (this , tag , regexp , first , limit );
196
+ }
197
+ catch (Throwable ex ) {
198
+ ReflectionUtils .rethrowRuntimeException (ex );
199
+ }
200
+ }
201
+
202
+ private MethodHandle getSnakeYaml2ImplicitResolverMethod ()
203
+ throws NoSuchMethodException , IllegalAccessException {
204
+ MethodHandle snakeYaml2ImplicitResolverMethod = this .snakeYaml2ImplicitResolverMethod ;
205
+ if (snakeYaml2ImplicitResolverMethod == null ) {
206
+ snakeYaml2ImplicitResolverMethod = MethodHandles .lookup ().findSpecial (Resolver .class ,
207
+ "addImplicitResolver" , SNAKE_YAML_2_ADD_IMPLICIT_RESOLVER_METHOD_TYPE , getClass ());
208
+ this .snakeYaml2ImplicitResolverMethod = snakeYaml2ImplicitResolverMethod ;
209
+ }
210
+ return snakeYaml2ImplicitResolverMethod ;
211
+ }
212
+
180
213
}
181
214
182
215
}
0 commit comments