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 in application properties file #4697

Merged
merged 12 commits into from
Nov 25, 2024
Merged
Next Next commit
Add initial support for java references in properties file
nielsdebruin committed Nov 21, 2024
commit 490ccd9d20920e57e594bb1d626215000f6820e6
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.properties.Assertions.properties;
import static org.openrewrite.xml.Assertions.xml;

@SuppressWarnings("ConstantConditions")
@@ -1725,4 +1726,22 @@ void changePackageInSpringXml() {
);

}

@Test
void changeTypeInPropertiesFile() {
rewriteRun(
spec -> spec.recipe(new ChangePackage("java.lang", "java.cool", true)),
properties(
"""
a.property=java.lang.String
b.property=java.lang.test.String
c.property=String
""",
"""
a.property=java.cool.String
b.property=java.cool.test.String
c.property=String
""")
);
}
}
Original file line number Diff line number Diff line change
@@ -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.properties.Assertions.properties;

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

@Test
void changeTypeInPropertiesFile() {
rewriteRun(
spec -> spec.recipe(new ChangeType("java.lang.String", "java.lang.Integer", true)),
properties(
"""
a.property=java.lang.String
b.property=String
""",
"""
a.property=java.lang.Integer
b.property=String
""")
);
}
}
1 change: 1 addition & 0 deletions rewrite-java/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ dependencies {
api(project(":rewrite-core"))
api(project(":rewrite-yaml"))
api(project(":rewrite-xml"))
api(project(":rewrite-properties"))

api("io.micrometer:micrometer-core:1.9.+")
api("org.jetbrains:annotations:latest.release")
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.properties.tree.Properties;
import org.openrewrite.trait.Reference;
import org.openrewrite.xml.tree.Xml;

@@ -58,6 +59,10 @@ public TreeVisitor<Tree, ExecutionContext> rename(String newValue) {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (StringUtils.isNotEmpty(newValue)) {
if (tree instanceof Properties.Entry) {
Properties.Entry entry = (Properties.Entry) tree;
return entry.withValue(entry.getValue().withText(getReplacement(entry.getValue().getText(), targetPackage, newValue)));
}
if (tree instanceof Xml.Attribute) {
return ((Xml.Attribute) tree).withValue(((Xml.Attribute) tree).getValue().withValue(getReplacement(((Xml.Attribute) tree).getValueAsString(), targetPackage, newValue)));
}
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.trait.Reference;
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.properties.tree.Properties;

import java.util.regex.Pattern;

@@ -126,6 +127,9 @@ public TreeVisitor<Tree, ExecutionContext> rename(String newValue) {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (StringUtils.isNotEmpty(newValue)) {
if (tree instanceof Properties.Entry) {
return ((Properties.Entry) tree).withValue(((Properties.Entry) tree).getValue().withText(newValue));
}
if (tree instanceof Xml.Attribute) {
return ((Xml.Attribute) tree).withValue(((Xml.Attribute) tree).getValue().withValue(newValue));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.properties.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.properties.tree.Properties;
import org.openrewrite.trait.Reference;
import org.openrewrite.trait.SimpleTraitMatcher;

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

@Value
public class PropertiesReference implements Reference {
Cursor cursor;
Kind kind;

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

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

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

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

@Override
protected @Nullable PropertiesReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Properties.Entry && javaFullyQualifiedTypePattern.matcher(((Properties.Entry) value).getValue().getText()).matches()) {
if (determineKind(((Properties.Entry) value).getValue().getText()) == Kind.PACKAGE) {
return new PropertiesReference(cursor, Kind.PACKAGE);
} else if (determineKind(((Properties.Entry) value).getValue().getText()) == Kind.TYPE) {
return new PropertiesReference(cursor, Kind.TYPE);
}
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

@Override
public boolean isAcceptable(SourceFile sourceFile) {
return sourceFile instanceof Properties.File;
}
}
}
Original file line number Diff line number Diff line change
@@ -15,16 +15,16 @@
*/
package org.openrewrite.properties.tree;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.With;
import lombok.*;
import lombok.experimental.NonFinal;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.marker.Markers;
import org.openrewrite.properties.PropertiesVisitor;
import org.openrewrite.properties.internal.PropertiesPrinter;

import java.beans.Transient;
import java.lang.ref.SoftReference;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
@@ -55,7 +55,9 @@ default <P> boolean isAcceptable(TreeVisitor<?, P> v, P p) {

@lombok.Value
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
class File implements Properties, SourceFile {
@RequiredArgsConstructor
@AllArgsConstructor
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
class File implements Properties, SourceFileWithReferences {
@With
@EqualsAndHashCode.Include
UUID id;
@@ -110,6 +112,27 @@ public <P> Properties acceptProperties(PropertiesVisitor<P> v, P p) {
public <P> TreeVisitor<?, PrintOutputCapture<P>> printer(Cursor cursor) {
return new PropertiesPrinter<>();
}

@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;
}
}

interface Content extends Properties {
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.openrewrite.properties.trait.PropertiesReference$Provider
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.properties.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.properties.Assertions.properties;

class PropertiesReferenceTest implements RewriteTest {
@Test
void testFindJavaReferences() {
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() -> new PropertiesReference.Matcher()
.asVisitor(ref -> SearchResult.found(ref.getTree(), ref.getValue())))),
properties(
"""
a.fqt=java.lang.String
b.package=java.lang
c.type=Integer
""",
"""
~~(java.lang.String)~~>a.fqt=java.lang.String
~~(java.lang)~~>b.package=java.lang
c.type=Integer
""",
spec -> spec.afterRecipe(doc -> {
assertThat(doc.getReferences().getReferences().stream().filter(typeRef -> typeRef.getValue().equals("java.lang.String") && typeRef.getKind().equals(Reference.Kind.TYPE)).count()).isEqualTo(1);
assertThat(doc.getReferences().getReferences().stream().filter(typeRef -> typeRef.getValue().equals("java.lang") && typeRef.getKind().equals(Reference.Kind.PACKAGE)).count()).isEqualTo(1);
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
assertThat(doc.getReferences().getReferences().stream().anyMatch(typeRef -> typeRef.getValue().equals("Integer"))).isFalse();
})
));
}
}
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved