Skip to content

Commit

Permalink
Polish, inline logging recipes to use anonymous visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
aegershman committed Jan 7, 2022
1 parent 215928f commit df0f0d3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,23 @@ protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new MigrateLogRecordSetMillisToSetInstantVisitor();
}

private static class MigrateLogRecordSetMillisToSetInstantVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = method;
if (MATCHER.matches(m)) {
m = m.withName(m.getName().withSimpleName("setInstant")).withTemplate(
JavaTemplate.builder(this::getCursor, "Instant.ofEpochMilli(#{any(long)})")
.imports("java.time.Instant")
.build(),
m.getCoordinates().replaceArguments(),
m.getArguments().get(0)
);
maybeAddImport("java.time.Instant");
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = method;
if (MATCHER.matches(m)) {
m = m.withName(m.getName().withSimpleName("setInstant")).withTemplate(
JavaTemplate.builder(this::getCursor, "Instant.ofEpochMilli(#{any(long)})")
.imports("java.time.Instant")
.build(),
m.getCoordinates().replaceArguments(),
m.getArguments().get(0)
);
maybeAddImport("java.time.Instant");
}
return super.visitMethodInvocation(m, ctx);
}
return super.visitMethodInvocation(m, ctx);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,20 @@ protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new MigrateLoggerGlobalToGetGlobalVisitor();
}

private static class MigrateLoggerGlobalToGetGlobalVisitor extends JavaVisitor<ExecutionContext> {
@Override
public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
J j = super.visitFieldAccess(fieldAccess, ctx);
J.FieldAccess asFieldAccess = (J.FieldAccess) j;

if (TypeUtils.isOfClassType(asFieldAccess.getTarget().getType(), "java.util.logging.Logger") && asFieldAccess.getSimpleName().equals("global")) {
j = j.withTemplate(JavaTemplate.builder(this::getCursor, "Logger.getGlobal();").build(),
((J.FieldAccess) j).getCoordinates().replace());
return new JavaVisitor<ExecutionContext>() {
@Override
public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
J j = super.visitFieldAccess(fieldAccess, ctx);
J.FieldAccess asFieldAccess = (J.FieldAccess) j;

if (TypeUtils.isOfClassType(asFieldAccess.getTarget().getType(), "java.util.logging.Logger") && asFieldAccess.getSimpleName().equals("global")) {
j = j.withTemplate(JavaTemplate.builder(this::getCursor, "Logger.getGlobal();").build(),
((J.FieldAccess) j).getCoordinates().replace());
}

return j;
}

return j;
}
};


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,24 @@ protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new MigrateLoggerLogrbToUseResourceBundleVisitor();
}

private static class MigrateLoggerLogrbToUseResourceBundleVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = method;
if (MATCHER.matches(m)) {
m = m.withTemplate(
JavaTemplate.builder(this::getCursor, "#{any(java.util.logging.Level)}, #{any(String)}, #{any(String)}, ResourceBundle.getBundle(#{any(String)}), #{any(String)}" + (m.getArguments().size() == 6 ? ", #{any()}" : ""))
.imports("java.util.ResourceBundle")
.build(),
m.getCoordinates().replaceArguments(),
m.getArguments().toArray()
);
maybeAddImport("java.util.ResourceBundle");
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = method;
if (MATCHER.matches(m)) {
m = m.withTemplate(
JavaTemplate.builder(this::getCursor, "#{any(java.util.logging.Level)}, #{any(String)}, #{any(String)}, ResourceBundle.getBundle(#{any(String)}), #{any(String)}" + (m.getArguments().size() == 6 ? ", #{any()}" : ""))
.imports("java.util.ResourceBundle")
.build(),
m.getCoordinates().replaceArguments(),
m.getArguments().toArray()
);
maybeAddImport("java.util.ResourceBundle");
}
return super.visitMethodInvocation(m, ctx);
}
return super.visitMethodInvocation(m, ctx);
}

};
}

}

0 comments on commit df0f0d3

Please sign in to comment.