Skip to content

Commit

Permalink
Call ShortenFullyQualifiedTypeReferences after visit (#18)
Browse files Browse the repository at this point in the history
* Call ShortenFullyQualifiedTypeReferences after visit

* Add missing doAfterVisit

* Strip leading whitespace
  • Loading branch information
timtebeek authored Aug 14, 2023
1 parent 30c6f24 commit c15a636
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
recipe.append(" maybeAddImport(\"" + import_.substring(0, dot) + "\", \"" + import_.substring(dot + 1) + "\");\n");
}
}
recipe.append(" doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());\n");
if (parameters.isEmpty()) {
recipe.append(" return " + after + ".apply(getCursor(), elem.getCoordinates().replace());\n");
} else {
Expand Down Expand Up @@ -343,6 +344,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
out.write("import org.openrewrite.TreeVisitor;\n");
out.write("import org.openrewrite.java.JavaTemplate;\n");
out.write("import org.openrewrite.java.JavaVisitor;\n");
out.write("import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;\n");
out.write("import org.openrewrite.java.template.Primitive;\n");
out.write("import org.openrewrite.java.tree.*;\n");
if (outerClassRequired) {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/recipes/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand Down Expand Up @@ -53,6 +54,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(0));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -84,6 +86,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace());
}
return super.visitBinary(elem, ctx);
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/recipes/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand Down Expand Up @@ -56,6 +57,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
maybeAddImport("java.util.Objects");
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -88,6 +90,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
maybeRemoveImport("java.util.Objects");
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand Down Expand Up @@ -53,6 +54,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down Expand Up @@ -84,6 +86,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/recipes/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand All @@ -30,6 +31,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down

0 comments on commit c15a636

Please sign in to comment.