Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for java references for yaml #4698

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.xml.Assertions.xml;
import static org.openrewrite.yaml.Assertions.yaml;

@SuppressWarnings("ConstantConditions")
class ChangePackageTest implements RewriteTest {
Expand Down Expand Up @@ -1723,6 +1724,24 @@ void changePackageInSpringXml() {
"""
)
);
}

@Test
void changePackageInYaml() {
rewriteRun(
spec -> spec.recipe(new ChangePackage("java.lang", "java.cool", true)),
yaml(
"""
root:
a: java.lang.String
b: java.lang.test.String
c: String
""", """
root:
a: java.cool.String
b: java.cool.test.String
c: String
"""
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.xml.Assertions.xml;
import static org.openrewrite.yaml.Assertions.yaml;

@SuppressWarnings("ConstantConditions")
class ChangeTypeTest implements RewriteTest {
Expand Down Expand Up @@ -2062,4 +2063,23 @@ public class HelloClass {}
}))
);
}

@Test
void changeTypeInYaml() {
rewriteRun(
spec -> spec.recipe(new ChangeType("java.lang.String", "java.lang.Integer", true)),
yaml(
"""
root:
a: java.lang.String
b: java.lang.test.String
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
c: String
""", """
root:
a: java.lang.Integer
b: java.lang.test.String
c: String
"""
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.internal.StringUtils;
import org.openrewrite.trait.Reference;
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.yaml.tree.Yaml;

@Getter
public class PackageMatcher implements Reference.Renamer, Reference.Matcher {
Expand Down Expand Up @@ -66,6 +67,9 @@ public TreeVisitor<Tree, ExecutionContext> rename(String newValue) {
return ((Xml.Tag) tree).withValue(getReplacement(((Xml.Tag) tree).getValue().get(), targetPackage, newValue));
}
}
if (tree instanceof Yaml.Scalar) {
return ((Yaml.Scalar) tree).withValue(getReplacement(((Yaml.Scalar) tree).getValue(), targetPackage, newValue));
}
}
return super.visit(tree, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.trait.Reference;
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.yaml.tree.Yaml;

import java.util.regex.Pattern;

Expand Down Expand Up @@ -132,6 +133,9 @@ public TreeVisitor<Tree, ExecutionContext> rename(String newValue) {
if (tree instanceof Xml.Tag) {
return ((Xml.Tag) tree).withValue(newValue);
}
if (tree instanceof Yaml.Scalar) {
return ((Yaml.Scalar) tree).withValue(newValue);
}
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
}
return super.visit(tree, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.yaml.trait;

import lombok.Value;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.SourceFile;
import org.openrewrite.trait.Reference;
import org.openrewrite.trait.SimpleTraitMatcher;
import org.openrewrite.yaml.tree.Yaml;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

@Value
class YamlReference implements Reference {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT this will now try to match any YAML scalar values against the regexp we have for Java identifiers. This seems wrong to me. I don't think there is any need to add this class here. Instead I would expect "domain-specific" implementations like SpringReference which look for type references in specific YAML documents (matching some given file name or similar).

AFAIK Spring also allows config to be in YAML, so maybe we could provide an implementation for that.

Cursor cursor;
Kind kind;

@Override
public @NonNull Kind getKind() {
return kind;
}

@Override
public @NonNull String getValue() {
if (getTree() instanceof Yaml.Scalar) {
return ((Yaml.Scalar) getTree()).getValue();
}
throw new IllegalArgumentException("getTree() must be an Yaml.Document: " + getTree().getClass());
}

@Override
public boolean supportsRename() {
return true;
}

public static class Matcher extends SimpleTraitMatcher<YamlReference> {
private static final Pattern javaFullyQualifiedTypePattern = Pattern.compile("\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*");

@Override
protected @Nullable YamlReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Yaml.Scalar && javaFullyQualifiedTypePattern.matcher(((Yaml.Scalar) value).getValue()).matches()) {
return new YamlReference(cursor, determineKind(((Yaml.Scalar) value).getValue()));
}
return null;
}

private Kind determineKind(String value) {
return Character.isUpperCase(value.charAt(value.lastIndexOf('.') + 1)) ? Kind.TYPE : Kind.PACKAGE;
}
}

@SuppressWarnings("unused")
public static class Provider implements Reference.Provider {

@Override
public @NonNull Set<Reference> getReferences(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
new Matcher().asVisitor(reference -> {
references.add(reference);
return reference.getTree();
}).visit(sourceFile, 0);
return references;
}

@Override
public boolean isAcceptable(SourceFile sourceFile) {
return sourceFile instanceof Yaml.Documents;
}
}
}
28 changes: 27 additions & 1 deletion rewrite-yaml/src/main/java/org/openrewrite/yaml/tree/Yaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.marker.Markers;
import org.openrewrite.yaml.YamlVisitor;
import org.openrewrite.yaml.internal.YamlPrinter;

import java.beans.Transient;
import java.lang.ref.SoftReference;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand Down Expand Up @@ -60,8 +63,10 @@ default <P> boolean isAcceptable(TreeVisitor<?, P> v, P p) {

@Value
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@With
class Documents implements Yaml, SourceFile {
class Documents implements Yaml, SourceFileWithReferences {
@EqualsAndHashCode.Include
UUID id;

Expand Down Expand Up @@ -125,6 +130,27 @@ public Documents withPrefix(String prefix) {
public <P> TreeVisitor<?, PrintOutputCapture<P>> printer(Cursor cursor) {
return new YamlPrinter<>();
}

@Nullable
@NonFinal
transient SoftReference<References> references;

@Transient
@Override
public References getReferences() {
References cache;
if (this.references == null) {
cache = References.build(this);
this.references = new SoftReference<>(cache);
} else {
cache = this.references.get();
if (cache == null || cache.getSourceFile() != this) {
cache = References.build(this);
this.references = new SoftReference<>(cache);
}
}
return cache;
}
}

@Value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.openrewrite.yaml.trait.YamlReference$Provider
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.yaml.trait;

import org.junit.jupiter.api.Test;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.trait.Reference;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.yaml.Assertions.yaml;

class YamlReferenceTest implements RewriteTest {

@Test
void findJavaReferences() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() -> new YamlReference.Matcher()
.asVisitor(yamlJavaReference -> SearchResult.found(yamlJavaReference.getTree(), yamlJavaReference.getValue())))),
yaml(
"""
root:
a: java.lang.String
b: java.lang
c: String
recipelist:
- org.openrewrite.java.DoSomething:
option: 'org.foo.Bar'
""", """
root:
a: ~~(java.lang.String)~~>java.lang.String
b: ~~(java.lang)~~>java.lang
c: String
recipelist:
- ~~(org.openrewrite.java.DoSomething)~~>org.openrewrite.java.DoSomething:
option: ~~(org.foo.Bar)~~>'org.foo.Bar'
""",
spec -> spec.afterRecipe(doc -> {
assertThat(doc.getReferences().getReferences()).satisfiesExactlyInAnyOrder(
ref -> {
assertThat(ref.getKind()).isEqualTo(Reference.Kind.TYPE);
assertThat(ref.getValue()).isEqualTo("java.lang.String");
},
ref -> {
assertThat(ref.getKind()).isEqualTo(Reference.Kind.PACKAGE);
assertThat(ref.getValue()).isEqualTo("java.lang");
},
ref -> {
assertThat(ref.getKind()).isEqualTo(Reference.Kind.TYPE);
assertThat(ref.getValue()).isEqualTo("org.openrewrite.java.DoSomething");
},
ref -> {
assertThat(ref.getKind()).isEqualTo(Reference.Kind.TYPE);
assertThat(ref.getValue()).isEqualTo("org.foo.Bar");
});
}))
);
}
}