Skip to content

Commit

Permalink
Make matchers static singletons in the provider classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 24, 2024
1 parent 411a811 commit fae23b8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,26 @@ public Tree rename(Renamer renamer, Cursor cursor, ExecutionContext ctx) {
return tree;
}

@SuppressWarnings("unused")
public static class Provider extends AbstractProvider<PropertiesReference> {
private static final Predicate<String> applicationPropertiesMatcher = Pattern.compile("^application(-\\w+)?\\.properties$").asPredicate();
private static final SimpleTraitMatcher<PropertiesReference> matcher = new SimpleTraitMatcher<PropertiesReference>() {
private final Predicate<String> javaFullyQualifiedTypeMatcher = Pattern.compile(
"\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*").asPredicate();

@Override
protected @Nullable PropertiesReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Properties.Entry &&
javaFullyQualifiedTypeMatcher.test(((Properties.Entry) value).getValue().getText())) {
return new PropertiesReference(cursor, determineKind(((Properties.Entry) value).getValue().getText()));
}
return null;
}

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

@Override
public boolean isAcceptable(SourceFile sourceFile) {
Expand All @@ -73,24 +90,7 @@ public boolean isAcceptable(SourceFile sourceFile) {

@Override
public SimpleTraitMatcher<PropertiesReference> getMatcher() {
return new SimpleTraitMatcher<PropertiesReference>() {
private final Predicate<String> javaFullyQualifiedTypeMatcher = Pattern.compile(
"\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*").asPredicate();

@Override
protected @Nullable PropertiesReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Properties.Entry &&
javaFullyQualifiedTypeMatcher.test(((Properties.Entry) value).getValue().getText())) {
return new PropertiesReference(cursor, determineKind(((Properties.Entry) value).getValue().getText()));
}
return null;
}

private Kind determineKind(String value) {
return Character.isUpperCase(value.charAt(value.lastIndexOf('.') + 1)) ? Kind.TYPE : Kind.PACKAGE;
}
};
return matcher;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

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

@Value
Expand All @@ -42,43 +40,42 @@ public Kind getKind() {
return kind;
}

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");
private final XPathMatcher keyTypeXPath = new XPathMatcher("//@key-type");
private final XPathMatcher valueTypeXPath = new XPathMatcher("//@value-type");
private final XPathMatcher tags = new XPathMatcher("//value");
public static class Provider extends AbstractProvider<SpringXmlReference> {
private static final SimpleTraitMatcher<SpringXmlReference> matcher = new 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");
private final XPathMatcher keyTypeXPath = new XPathMatcher("//@key-type");
private final XPathMatcher valueTypeXPath = new XPathMatcher("//@value-type");
private final XPathMatcher tags = new XPathMatcher("//value");

@Override
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 SpringXmlReference(cursor, determineKind(stringVal));
@Override
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 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 SpringXmlReference(cursor, determineKind(stringVal.get()));
} 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 SpringXmlReference(cursor, determineKind(stringVal.get()));
}
}
}
return null;
}
return null;
}

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

public static class Provider extends AbstractProvider<SpringXmlReference> {
Reference.Kind determineKind(String value) {
return Character.isUpperCase(value.charAt(value.lastIndexOf('.') + 1)) ? Reference.Kind.TYPE : Reference.Kind.PACKAGE;
}
};

@Override
public boolean isAcceptable(SourceFile sourceFile) {
Expand All @@ -98,7 +95,7 @@ public boolean isAcceptable(SourceFile sourceFile) {

@Override
public SimpleTraitMatcher<SpringXmlReference> getMatcher() {
return new Matcher();
return matcher;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SpringXmlReferenceTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(RewriteTest.toRecipe(() -> new SpringXmlReference.Matcher()
spec.recipe(RewriteTest.toRecipe(() -> new SpringXmlReference.Provider().getMatcher()
.asVisitor(springJavaTypeReference -> SearchResult.found(springJavaTypeReference.getTree(), springJavaTypeReference.getValue()))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public class YamlApplicationConfigReference extends YamlReference {

public static class Provider extends YamlProvider {
private static final Predicate<String> applicationPropertiesMatcher = Pattern.compile("^application(-\\w+)?\\.(yaml|yml)$").asPredicate();
private static final SimpleTraitMatcher<YamlReference> matcher = new SimpleTraitMatcher<YamlReference>() {
private final Predicate<String> javaFullyQualifiedTypePattern = Pattern.compile(
"\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*").asPredicate();

@Override
protected @Nullable YamlReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Yaml.Scalar && javaFullyQualifiedTypePattern.test(((Yaml.Scalar) value).getValue())) {
return new YamlApplicationConfigReference(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;
}
};

@Override
public boolean isAcceptable(SourceFile sourceFile) {
Expand All @@ -44,24 +61,7 @@ public boolean isAcceptable(SourceFile sourceFile) {

@Override
public SimpleTraitMatcher<YamlReference> getMatcher() {
return new SimpleTraitMatcher<YamlReference>() {
private final Predicate<String> javaFullyQualifiedTypePattern = Pattern.compile(
"\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*")
.asPredicate();

@Override
protected @Nullable YamlReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Yaml.Scalar && javaFullyQualifiedTypePattern.test(((Yaml.Scalar) value).getValue())) {
return new YamlApplicationConfigReference(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;
}
};
return matcher;
}
}
}

0 comments on commit fae23b8

Please sign in to comment.