Skip to content

Commit

Permalink
Refactor SpringReference (#4805)
Browse files Browse the repository at this point in the history
* Separating and clearer naming

* Add license header

* Review feedback
  • Loading branch information
Laurens-W authored Dec 20, 2024
1 parent 2631a7e commit 697ae15
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<SpringReference> {
static class Matcher extends SimpleTraitMatcher<SpringXmlReference> {
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");
Expand All @@ -88,30 +49,30 @@ static class Matcher extends SimpleTraitMatcher<SpringReference> {
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) {
Xml.Tag tag = (Xml.Tag) value;
if (tags.matches(cursor)) {
Optional<String> 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.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;
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.openrewrite.xml.trait.SpringReference$Provider
org.openrewrite.xml.trait.SpringXmlReference$Provider
Original file line number Diff line number Diff line change
Expand Up @@ -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()))));
}

Expand Down

0 comments on commit 697ae15

Please sign in to comment.