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

Refactor SpringReference #4805

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
*/
package org.openrewrite.xml.trait;

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 @@ -31,55 +28,9 @@
import java.util.Set;
import java.util.regex.Pattern;

@Value
class SpringReference implements Reference {
Cursor cursor;
Kind kind;
public class SpringXmlReference {
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

@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<XmlReference> {
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 +39,30 @@ static class Matcher extends SimpleTraitMatcher<SpringReference> {
private final XPathMatcher tags = new XPathMatcher("//value");

@Override
protected @Nullable SpringReference test(Cursor cursor) {
protected @Nullable XmlReference 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 XmlReference(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 XmlReference(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 All @@ -121,7 +72,7 @@ public static class Provider implements Reference.Provider {
@Override
public Set<Reference> getReferences(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
new Matcher().asVisitor(reference -> {
new SpringXmlReference.Matcher().asVisitor(reference -> {
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
references.add(reference);
return reference.getTree();
}).visit(sourceFile, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 lombok.Value;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Tree;
import org.openrewrite.trait.Reference;
import org.openrewrite.xml.tree.Xml;

@Value
class XmlReference implements Reference {
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;
}

}
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
Loading