getVisitor() {
- return new ChangePropertyKeyVisitor<>();
- }
-
- private class ChangePropertyKeyVisitor extends YamlIsoVisitor
{
- @Override
- public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, P p) {
- Yaml.Mapping.Entry e = super.visitMappingEntry(entry, p);
- if (getCursor().firstEnclosing(Yaml.Sequence.class) != null) {
- return e;
- }
- Deque propertyEntries = getCursor().getPathAsStream()
- .filter(Yaml.Mapping.Entry.class::isInstance)
- .map(Yaml.Mapping.Entry.class::cast)
- .collect(Collectors.toCollection(ArrayDeque::new));
-
- String prop = stream(spliteratorUnknownSize(propertyEntries.descendingIterator(), 0), false)
- .map(e2 -> e2.getKey().getValue())
- .collect(Collectors.joining("."));
-
- String propertyToTest = newPropertyKey;
- if (!Boolean.FALSE.equals(relaxedBinding) ?
- NameCaseConvention.matchesRelaxedBinding(prop, oldPropertyKey) :
- StringUtils.matchesGlob(prop, oldPropertyKey)) {
- Iterator propertyEntriesLeftToRight = propertyEntries.descendingIterator();
- while (propertyEntriesLeftToRight.hasNext()) {
- Yaml.Mapping.Entry propertyEntry = propertyEntriesLeftToRight.next();
- String value = propertyEntry.getKey().getValue() + ".";
-
- if (!propertyToTest.startsWith(value ) || (propertyToTest.startsWith(value) && !propertyEntriesLeftToRight.hasNext())) {
- doAfterVisit(new InsertSubpropertyVisitor<>(
- propertyEntry,
- propertyToTest,
- entry
- ));
- break;
- }
- propertyToTest = propertyToTest.substring(value.length());
- }
- }
-
- return e;
- }
- }
-
- private static class InsertSubpropertyVisitor extends YamlIsoVisitor
{
- private final Yaml.Mapping.Entry scope;
- private final String subproperty;
- private final Yaml.Mapping.Entry entryToReplace;
-
- private InsertSubpropertyVisitor(Yaml.Mapping.Entry scope, String subproperty, Yaml.Mapping.Entry entryToReplace) {
- this.scope = scope;
- this.subproperty = subproperty;
- this.entryToReplace = entryToReplace;
- }
-
- @Override
- public Yaml.Mapping visitMapping(Yaml.Mapping mapping, P p) {
- Yaml.Mapping m = super.visitMapping(mapping, p);
- if (m.getEntries().contains(scope)) {
- String newEntryPrefix = scope.getPrefix();
- Yaml.Mapping.Entry newEntry = new Yaml.Mapping.Entry(randomId(),
- newEntryPrefix,
- Markers.EMPTY,
- new Yaml.Scalar(randomId(), "", Markers.EMPTY,
- Yaml.Scalar.Style.PLAIN, null, subproperty),
- scope.getBeforeMappingValueIndicator(),
- entryToReplace.getValue().copyPaste());
-
- if (m.getEntries().contains(entryToReplace)) {
- m = m.withEntries(ListUtils.map(m.getEntries(), e -> {
- if (e.equals(entryToReplace)) {
- return newEntry.withPrefix(e.getPrefix());
- }
- return e;
- }));
- } else {
- m = (Yaml.Mapping) new DeletePropertyVisitor<>(entryToReplace).visitNonNull(m, p);
- m = maybeAutoFormat(m, m.withEntries(ListUtils.concat(m.getEntries(), newEntry)), p, getCursor().getParentOrThrow());
- }
- }
-
- return m;
- }
- }
- private static class DeletePropertyVisitor
extends YamlIsoVisitor
{
- private final Yaml.Mapping.Entry scope;
-
- private DeletePropertyVisitor(Yaml.Mapping.Entry scope) {
- this.scope = scope;
- }
-
- @Override
- public Yaml.Mapping visitMapping(Yaml.Mapping mapping, P p) {
- Yaml.Mapping m = super.visitMapping(mapping, p);
-
- boolean changed = false;
- List entries = new ArrayList<>();
- for (Yaml.Mapping.Entry entry : m.getEntries()) {
- if (entry == scope || (entry.getValue() instanceof Yaml.Mapping && ((Yaml.Mapping) entry.getValue()).getEntries().isEmpty())) {
- changed = true;
- } else {
- entries.add(entry);
- }
- }
-
- if (entries.size() == 1) {
- entries = ListUtils.map(entries, e -> e.withPrefix(""));
- }
-
- if (changed) {
- m = m.withEntries(entries);
-
- if (getCursor().getParentOrThrow().getValue() instanceof Yaml.Document) {
- Yaml.Document document = getCursor().getParentOrThrow().getValue();
- if (!document.isExplicit()) {
- m = m.withEntries(m.getEntries());
- }
- }
- }
- return m;
- }
- }
-}
diff --git a/spring-boot-upgrade-30/src/test/java/org/openrewrite/java/spring/boot3/UpdateMicrometerPackageTest.java b/spring-boot-upgrade-30/src/test/java/org/openrewrite/java/spring/boot3/UpdateMicrometerPackageTest.java
deleted file mode 100644
index fbbc4720d..000000000
--- a/spring-boot-upgrade-30/src/test/java/org/openrewrite/java/spring/boot3/UpdateMicrometerPackageTest.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Copyright 2021 - 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.openrewrite.java.spring.boot3;
-
-import org.junit.jupiter.api.Test;
-import org.openrewrite.Result;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.test.RewriteTest;
-
-import java.util.List;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class UpdateMicrometerPackageTest {
-
- @Test
- void shouldUpdatePackage() {
- JavaParser javaParser = JavaParser.fromJavaVersion().build();
- String javaDependsOn = """
- package io.micrometer.core.instrument.binder;
- public class Abc {
- }""".stripIndent();
- String javaCode = """
- package a;
- import io.micrometer.core.instrument.binder.*;
- class A {
- Abc method() {
- return null;
- }
- }""".stripIndent();
- List parse = javaParser.parse(javaDependsOn, javaCode);
- String recipeName = "org.openrewrite.java.spring.boot3.Micrometer_3_0";
- List results = RewriteTest.fromRuntimeClasspath(recipeName)
- .run(parse);
- assertThat(results).hasSize(2);
-
- assertThat(results.get(1).getAfter().printAll()).isEqualTo("""
- package a;
- import io.micrometer.binder.*;
- class A {
- Abc method() {
- return null;
- }
- }""".stripIndent());
- }
-}