From e5d3fce8e915d1637684583f4ced1cda452c36ee Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Fri, 20 Dec 2024 15:33:17 +0100 Subject: [PATCH] Refactor SpringReference (#4805) * Separating and clearer naming * Add license header * Review feedback --- ...Reference.java => SpringXmlReference.java} | 55 +++------------- .../openrewrite/xml/trait/XmlReference.java | 64 +++++++++++++++++++ .../org.openrewrite.trait.Reference$Provider | 2 +- ...eTest.java => SpringXmlReferenceTest.java} | 4 +- 4 files changed, 75 insertions(+), 50 deletions(-) rename rewrite-xml/src/main/java/org/openrewrite/xml/trait/{SpringReference.java => SpringXmlReference.java} (67%) create mode 100644 rewrite-xml/src/main/java/org/openrewrite/xml/trait/XmlReference.java rename rewrite-xml/src/test/java/org/openrewrite/xml/trait/{SpringReferenceTest.java => SpringXmlReferenceTest.java} (96%) diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringReference.java b/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringXmlReference.java similarity index 67% rename from rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringReference.java rename to rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringXmlReference.java index a2a78481461..41523a89b8a 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringReference.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringXmlReference.java @@ -18,9 +18,7 @@ import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.Cursor; -import org.openrewrite.ExecutionContext; import org.openrewrite.SourceFile; -import org.openrewrite.Tree; import org.openrewrite.trait.Reference; import org.openrewrite.trait.SimpleTraitMatcher; import org.openrewrite.xml.XPathMatcher; @@ -32,54 +30,17 @@ import java.util.regex.Pattern; @Value -class SpringReference implements Reference { +public class SpringXmlReference extends XmlReference { + Cursor cursor; Kind kind; - @Override - public Tree getTree() { - return Reference.super.getTree(); - } - @Override public Kind getKind() { return kind; } - @Override - public String getValue() { - if (getTree() instanceof Xml.Attribute) { - Xml.Attribute attribute = (Xml.Attribute) getTree(); - return attribute.getValueAsString(); - } else if (getTree() instanceof Xml.Tag) { - Xml.Tag tag = (Xml.Tag) getTree(); - if (tag.getValue().isPresent()) { - return tag.getValue().get(); - } - } - throw new IllegalArgumentException("getTree() must be an Xml.Attribute or Xml.Tag: " + getTree().getClass()); - } - - @Override - public boolean supportsRename() { - return true; - } - - @Override - public Tree rename(Renamer renamer, Cursor cursor, ExecutionContext ctx) { - Tree tree = cursor.getValue(); - if (tree instanceof Xml.Attribute) { - Xml.Attribute attribute = (Xml.Attribute) tree; - String renamed = renamer.rename(this); - return attribute.withValue(attribute.getValue().withValue(renamed)); - } else if (tree instanceof Xml.Tag && ((Xml.Tag) tree).getValue().isPresent()) { - String renamed = renamer.rename(this); - return ((Xml.Tag) tree).withValue(renamed); - } - return tree; - } - - static class Matcher extends SimpleTraitMatcher { + static class Matcher extends SimpleTraitMatcher { private final Pattern referencePattern = Pattern.compile("\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*"); private final XPathMatcher classXPath = new XPathMatcher("//@class"); private final XPathMatcher typeXPath = new XPathMatcher("//@type"); @@ -88,14 +49,14 @@ static class Matcher extends SimpleTraitMatcher { private final XPathMatcher tags = new XPathMatcher("//value"); @Override - protected @Nullable SpringReference test(Cursor cursor) { + protected @Nullable SpringXmlReference test(Cursor cursor) { Object value = cursor.getValue(); if (value instanceof Xml.Attribute) { Xml.Attribute attrib = (Xml.Attribute) value; if (classXPath.matches(cursor) || typeXPath.matches(cursor) || keyTypeXPath.matches(cursor) || valueTypeXPath.matches(cursor)) { String stringVal = attrib.getValueAsString(); if (referencePattern.matcher(stringVal).matches()) { - return new SpringReference(cursor, determineKind(stringVal)); + return new SpringXmlReference(cursor, determineKind(stringVal)); } } } else if (value instanceof Xml.Tag) { @@ -103,15 +64,15 @@ static class Matcher extends SimpleTraitMatcher { if (tags.matches(cursor)) { Optional stringVal = tag.getValue(); if (stringVal.isPresent() && referencePattern.matcher(stringVal.get()).matches()) { - return new SpringReference(cursor, determineKind(stringVal.get())); + return new SpringXmlReference(cursor, determineKind(stringVal.get())); } } } return null; } - Kind determineKind(String value) { - return Character.isUpperCase(value.charAt(value.lastIndexOf('.') + 1)) ? Kind.TYPE : Kind.PACKAGE; + Reference.Kind determineKind(String value) { + return Character.isUpperCase(value.charAt(value.lastIndexOf('.') + 1)) ? Reference.Kind.TYPE : Reference.Kind.PACKAGE; } } diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/trait/XmlReference.java b/rewrite-xml/src/main/java/org/openrewrite/xml/trait/XmlReference.java new file mode 100644 index 00000000000..25b87149d2d --- /dev/null +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/trait/XmlReference.java @@ -0,0 +1,64 @@ +/* + * Copyright 2024 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.xml.trait; + +import org.openrewrite.Cursor; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Tree; +import org.openrewrite.trait.Reference; +import org.openrewrite.xml.tree.Xml; + +public abstract class XmlReference implements Reference { + + @Override + public Tree getTree() { + return Reference.super.getTree(); + } + + @Override + public String getValue() { + if (getTree() instanceof Xml.Attribute) { + Xml.Attribute attribute = (Xml.Attribute) getTree(); + return attribute.getValueAsString(); + } else if (getTree() instanceof Xml.Tag) { + Xml.Tag tag = (Xml.Tag) getTree(); + if (tag.getValue().isPresent()) { + return tag.getValue().get(); + } + } + throw new IllegalArgumentException("getTree() must be an Xml.Attribute or Xml.Tag: " + getTree().getClass()); + } + + @Override + public boolean supportsRename() { + return true; + } + + @Override + public Tree rename(Renamer renamer, Cursor cursor, ExecutionContext ctx) { + Tree tree = cursor.getValue(); + if (tree instanceof Xml.Attribute) { + Xml.Attribute attribute = (Xml.Attribute) tree; + String renamed = renamer.rename(this); + return attribute.withValue(attribute.getValue().withValue(renamed)); + } else if (tree instanceof Xml.Tag && ((Xml.Tag) tree).getValue().isPresent()) { + String renamed = renamer.rename(this); + return ((Xml.Tag) tree).withValue(renamed); + } + return tree; + } + +} diff --git a/rewrite-xml/src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider b/rewrite-xml/src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider index 72d76d75ee4..e8a9ab1dc42 100644 --- a/rewrite-xml/src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider +++ b/rewrite-xml/src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider @@ -1 +1 @@ -org.openrewrite.xml.trait.SpringReference$Provider \ No newline at end of file +org.openrewrite.xml.trait.SpringXmlReference$Provider \ No newline at end of file diff --git a/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringReferenceTest.java b/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringXmlReferenceTest.java similarity index 96% rename from rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringReferenceTest.java rename to rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringXmlReferenceTest.java index d60f5a2e8a9..000d242b16e 100644 --- a/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringReferenceTest.java +++ b/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringXmlReferenceTest.java @@ -23,11 +23,11 @@ import static org.openrewrite.xml.Assertions.xml; -class SpringReferenceTest implements RewriteTest { +class SpringXmlReferenceTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(RewriteTest.toRecipe(() -> new SpringReference.Matcher() + spec.recipe(RewriteTest.toRecipe(() -> new SpringXmlReference.Matcher() .asVisitor(springJavaTypeReference -> SearchResult.found(springJavaTypeReference.getTree(), springJavaTypeReference.getValue())))); }