-
I want to write migration that replaces access to field with method call: replace I created TemplateBuilder: private final JavaTemplate callMethodTemplate= JavaTemplate.builder(this::getCursor, "getMvc()")
.build(); and invoke it in my JavaVisitor like this: @Override
public J visitIdentifier(J.Identifier ident, ExecutionContext executionContext) {
J.Identifier i = (J.Identifier) super.visitIdentifier(ident, executionContext);
if (i.getFieldType() == null || !"mockMvc".equals(i.getFieldType().getName())) return i;
if (!TypeUtils.isOfClassType(i.getFieldType().getOwner(), "com.example.libs.MockMvcSupport;")) {
return i;
}
return i.withTemplate(callMethodTemplate, i.getCoordinates().replace()); //generates MethodInvocation
} The problem is rewrite test fails with the following error:
I understand that in template usage I don't provide return type information, but I don't know how to do for method that is missing at the start of migration. I also can't find any example for doing similar task. Question: How to properly provide type for this case? Just in case, here's the test that I'm running: rewriteRun(
java(
"""
import com.example.libs.MockMvcSupport;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
public class BaseAppControllerTest extends MockMvcSupport {
public void sample() {
Object a = this.mockMvc;
}
private void bar() {
MockMvc mockMvc = null;
System.out.println(mockMvc);
}
private void foo(Object o) {}
public void customMethod() throws Exception {
foo(mockMvc);
var asyncResult = result.andExpect(request().asyncStarted())
.andReturn();
result = mockMvc.perform(MockMvcRequestBuilders.asyncDispatch(asyncResult));
}
}
""",
"""
import com.example.libs.MockMvcSupport;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
public class BaseAppControllerTest extends MockMvcSupport {
public void sample() {
Object a = this.getMvc();
}
private void bar() {
MockMvc mockMvc = null;
System.out.println(mockMvc);
}
private void foo(Object o) {}
public void customMethod() throws Exception {
foo(getMvc());
var asyncResult = result.andExpect(request().asyncStarted())
.andReturn();
result = getMvc().perform(MockMvcRequestBuilders.asyncDispatch(asyncResult));
}
}
"""
)
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
hi @zeldigas ! Could you share with us how you configure the RecipeSpec in your test? That might help fill in some of the blanks with what you're already using or doing to find that What I'm looking for specifically is whether you're using anything like You can see some relevant links to example code in this section of the documentation. |
Beta Was this translation helpful? Give feedback.
Sounds like we're one step closer; did you already notice that
JavaTemplate.builder
also accepts and expects a classpath entry if you're referring to external classes, such as yourMockMvcSupport
? Adding that there ought to help resolve the missing type in the recipe as well.