11/*
2- * Copyright 2012-2020 the original author or authors.
2+ * Copyright 2012-2023 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .boot .env ;
1818
19+ import java .lang .invoke .MethodHandle ;
20+ import java .lang .invoke .MethodHandles ;
21+ import java .lang .invoke .MethodType ;
1922import java .util .ArrayList ;
2023import java .util .List ;
2124import java .util .Map ;
4548import org .springframework .boot .origin .TextResourceOrigin ;
4649import org .springframework .boot .origin .TextResourceOrigin .Location ;
4750import org .springframework .core .io .Resource ;
51+ import org .springframework .util .ReflectionUtils ;
4852
4953/**
5054 * Class to load {@code .yml} files into a map of {@code String} to
@@ -73,8 +77,8 @@ protected Yaml createYaml() {
7377
7478 private Yaml createYaml (LoaderOptions loaderOptions ) {
7579 BaseConstructor constructor = new OriginTrackingConstructor (loaderOptions );
76- Representer representer = new Representer ();
7780 DumperOptions dumperOptions = new DumperOptions ();
81+ Representer representer = new Representer (dumperOptions );
7882 LimitedResolver resolver = new LimitedResolver ();
7983 return new Yaml (constructor , representer , dumperOptions , loaderOptions , resolver );
8084 }
@@ -167,7 +171,12 @@ private static Node get(Node node) {
167171 /**
168172 * {@link Resolver} that limits {@link Tag#TIMESTAMP} tags.
169173 */
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 ;
171180
172181 @ Override
173182 public void addImplicitResolver (Tag tag , Pattern regexp , String first ) {
@@ -177,6 +186,30 @@ public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
177186 super .addImplicitResolver (tag , regexp , first );
178187 }
179188
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+
180213 }
181214
182215}
0 commit comments