Skip to content

Commit 34d91bb

Browse files
authored
Inline nested visitor class into getVisitor() (#147)
Declare the visitor anonymously inside the `Preconditions.check(..)` call rather than as a named nested class.
1 parent 5e6fec0 commit 34d91bb

1 file changed

Lines changed: 62 additions & 64 deletions

File tree

src/main/java/org/openrewrite/apache/httpclient5/MigrateRequestConfig.java

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -65,78 +65,76 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6565
// Cheap check first, to avoid more expensive search
6666
new UsesMethod<>(MATCHER_STALE_CHECK_ENABLED),
6767
callsSetStaleCheckEnabledFalse()
68-
), new MigrateRequestConfigVisitor());
69-
}
68+
), new JavaIsoVisitor<ExecutionContext>() {
69+
70+
@Override
71+
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
72+
// setStaleConnectionCheckEnabled is only related to PoolingHttpClientConnectionManager
73+
boolean staleEnabled = callsSetStaleCheckEnabledFalse().visitNonNull(method, ctx, getCursor().getParentOrThrow()) != method;
74+
if (staleEnabled) {
75+
// Find or create a new PoolingHttpClientConnectionManager
76+
J.VariableDeclarations connectionManagerVD = findExistingConnectionPool(method);
77+
boolean needsNewConnectionManager = connectionManagerVD == null;
78+
if (needsNewConnectionManager) {
79+
maybeAddImport(FQN_POOL_CONN_MANAGER5);
80+
method = JavaTemplate.builder(
81+
"PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = " +
82+
"new PoolingHttpClientConnectionManager();")
83+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpclient5", "httpcore5"))
84+
.imports(FQN_POOL_CONN_MANAGER5)
85+
.build()
86+
.apply(getCursor(), method.getBody().getCoordinates().firstStatement());
87+
connectionManagerVD = (J.VariableDeclarations) method.getBody().getStatements().get(0);
88+
}
7089

71-
private static class MigrateRequestConfigVisitor extends JavaIsoVisitor<ExecutionContext> {
72-
73-
@Override
74-
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
75-
// setStaleConnectionCheckEnabled is only related to PoolingHttpClientConnectionManager
76-
boolean staleEnabled = callsSetStaleCheckEnabledFalse().visitNonNull(method, ctx, getCursor().getParentOrThrow()) != method;
77-
if (staleEnabled) {
78-
// Find or create a new PoolingHttpClientConnectionManager
79-
J.VariableDeclarations connectionManagerVD = findExistingConnectionPool(method);
80-
boolean needsNewConnectionManager = connectionManagerVD == null;
81-
if (needsNewConnectionManager) {
82-
maybeAddImport(FQN_POOL_CONN_MANAGER5);
83-
method = JavaTemplate.builder(
84-
"PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = " +
85-
"new PoolingHttpClientConnectionManager();")
90+
// Set `setValidateAfterInactivity(TimeValue.NEG_ONE_MILLISECOND)`
91+
J.Identifier connectionManagerIdentifier = connectionManagerVD.getVariables().get(0).getName();
92+
maybeAddImport(FQN_TIME_VALUE);
93+
method = JavaTemplate.builder("#{any(" + FQN_POOL_CONN_MANAGER5 + ")}.setValidateAfterInactivity(TimeValue.NEG_ONE_MILLISECOND);")
8694
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpclient5", "httpcore5"))
87-
.imports(FQN_POOL_CONN_MANAGER5)
95+
.imports(FQN_TIME_VALUE)
8896
.build()
89-
.apply(getCursor(), method.getBody().getCoordinates().firstStatement());
90-
connectionManagerVD = (J.VariableDeclarations) method.getBody().getStatements().get(0);
91-
}
97+
.apply(updateCursor(method), connectionManagerVD.getCoordinates().after(), connectionManagerIdentifier);
9298

93-
// Set `setValidateAfterInactivity(TimeValue.NEG_ONE_MILLISECOND)`
94-
J.Identifier connectionManagerIdentifier = connectionManagerVD.getVariables().get(0).getName();
95-
maybeAddImport(FQN_TIME_VALUE);
96-
method = JavaTemplate.builder("#{any(" + FQN_POOL_CONN_MANAGER5 + ")}.setValidateAfterInactivity(TimeValue.NEG_ONE_MILLISECOND);")
97-
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpclient5", "httpcore5"))
98-
.imports(FQN_TIME_VALUE)
99-
.build()
100-
.apply(updateCursor(method), connectionManagerVD.getCoordinates().after(), connectionManagerIdentifier);
101-
102-
// Make the connection manager available to set in the method invocation visit below
103-
if (needsNewConnectionManager) {
104-
getCursor().putMessage(KEY_POOL_CONN_MANAGER, connectionManagerIdentifier);
99+
// Make the connection manager available to set in the method invocation visit below
100+
if (needsNewConnectionManager) {
101+
getCursor().putMessage(KEY_POOL_CONN_MANAGER, connectionManagerIdentifier);
102+
}
105103
}
104+
return super.visitMethodDeclaration(method, ctx);
106105
}
107-
return super.visitMethodDeclaration(method, ctx);
108-
}
109-
110-
private J.@Nullable VariableDeclarations findExistingConnectionPool(J.MethodDeclaration method) {
111-
AtomicReference<J.VariableDeclarations> existingConnManager = new AtomicReference<>();
112-
new JavaIsoVisitor<AtomicReference<J.VariableDeclarations>>() {
113-
@Override
114-
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, AtomicReference<J.VariableDeclarations> ref) {
115-
J.VariableDeclarations vd = super.visitVariableDeclarations(multiVariable, ref);
116-
if (TypeUtils.isOfClassType(vd.getTypeAsFullyQualified(), FQN_POOL_CONN_MANAGER4)) {
117-
ref.set(vd);
106+
107+
private J.@Nullable VariableDeclarations findExistingConnectionPool(J.MethodDeclaration method) {
108+
AtomicReference<J.VariableDeclarations> existingConnManager = new AtomicReference<>();
109+
new JavaIsoVisitor<AtomicReference<J.VariableDeclarations>>() {
110+
@Override
111+
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, AtomicReference<J.VariableDeclarations> ref) {
112+
J.VariableDeclarations vd = super.visitVariableDeclarations(multiVariable, ref);
113+
if (TypeUtils.isOfClassType(vd.getTypeAsFullyQualified(), FQN_POOL_CONN_MANAGER4)) {
114+
ref.set(vd);
115+
}
116+
return vd;
117+
}
118+
}.visitNonNull(method, existingConnManager, getCursor().getParentOrThrow());
119+
return existingConnManager.get();
120+
}
121+
122+
@Override
123+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
124+
if (MATCHER_STALE_CHECK_ENABLED.matches(method)) {
125+
doAfterVisit(new RemoveMethodInvocationsVisitor(singletonList(PATTERN_STALE_CHECK_ENABLED)));
126+
} else if (MATCHER_REQUEST_CONFIG.matches(method)) {
127+
J.Identifier connectionManagerIdentifier = getCursor().pollNearestMessage(KEY_POOL_CONN_MANAGER);
128+
if (connectionManagerIdentifier != null) {
129+
method = JavaTemplate.builder("#{any()}.setConnectionManager(#{any()});")
130+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpclient5", "httpcore5"))
131+
.imports(FQN_POOL_CONN_MANAGER5)
132+
.build()
133+
.apply(getCursor(), method.getCoordinates().replace(), method, connectionManagerIdentifier);
118134
}
119-
return vd;
120-
}
121-
}.visitNonNull(method, existingConnManager, getCursor().getParentOrThrow());
122-
return existingConnManager.get();
123-
}
124-
125-
@Override
126-
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
127-
if (MATCHER_STALE_CHECK_ENABLED.matches(method)) {
128-
doAfterVisit(new RemoveMethodInvocationsVisitor(singletonList(PATTERN_STALE_CHECK_ENABLED)));
129-
} else if (MATCHER_REQUEST_CONFIG.matches(method)) {
130-
J.Identifier connectionManagerIdentifier = getCursor().pollNearestMessage(KEY_POOL_CONN_MANAGER);
131-
if (connectionManagerIdentifier != null) {
132-
method = JavaTemplate.builder("#{any()}.setConnectionManager(#{any()});")
133-
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpclient5", "httpcore5"))
134-
.imports(FQN_POOL_CONN_MANAGER5)
135-
.build()
136-
.apply(getCursor(), method.getCoordinates().replace(), method, connectionManagerIdentifier);
137135
}
136+
return super.visitMethodInvocation(method, ctx);
138137
}
139-
return super.visitMethodInvocation(method, ctx);
140-
}
138+
});
141139
}
142140
}

0 commit comments

Comments
 (0)