Hi, I'm trying to add a third argument to a method call of an external library.
class A {
Q method1() {
Q q = makeQ().setMember(x1, y1).setMember(x2, y2); // method chaining
return q;
}
Q method2() {
return makeQ().setMember(x1, y1).setMember(x2, y2); // method chaining
}
Q method3() {
Q q = makeQ();
q.setMember(x1, y1);
q.setMember(x2, y2);
return q;
}
}
My goal is to add a third argument to setMember(x1, y1, z1) but only if it's a method of Q meaning all 3 cases presented in the example. However I don't want to change calls to setMember belonging to other classes.
In my mind, method1 and method2 might be covered similarly while a different approach is required for method3. What would be the recommended approach?
By resolving the method call, you can get the fully qualified name.
Is this enough of a pointer for you to get going with?
... and in case you haven't done so yet, the book here: https://javaparser.org/getting-started.html give a good foundation to work with the library.
@MysterAitch I understand from your comment that I should be using MethodCallExpr to get to setMember but I don't know how to get the fully qualified name. Does this involve the symbol solver library?
@matozoid I went over the free book but, at first look, the examples seemed to me too simple for my use case. It's my first contact with the library and I guess I still have to get used to the mindset and also put more effort into it. On the other hand, I can't lie that I was also half-hoping for more than RTFM 馃槃 after stumbling through some of your answers on other questions.
I think you are on the right track, I might consider inspecting the AST to become more familiar with what JP does (i.e. the "model" you are working with)...
https://javaparser.org/inspecting-an-ast/
I think @MysterAitch was pointing you towards the JavaSymbolSolver, and not necessarily to just the MethodCallExpr since that is the world of ResolvedNode
anyways to give you another hint, inspecting the AST will show you something special about MethodCallExpr nodes in this (chained methods) scenario (i.e. recursion).
We all support this project for fun, so some days we feel generous with our time, and other days we are busy, so hopefully that helps you with the direction to learn how to use it (having more people learn it is a net positive)
(speaking for myself I realize sometimes learning is frustrating but I wouldnt want to deprive you of the experience)
This is turning into a kind of treasure hunt :-D If you're tired of it you can give the Gitter channel a try, we're all on there, complaining about Java syntax.
Hi @azmau, it looks like the question has been answered and it has been over a month since the last reply, so I'm going to close this issue to help keep the issue tracker tidy.
Please do reply/follow up if there is anything outstanding!
Stay safe and take care :)
Most helpful comment
This is turning into a kind of treasure hunt :-D If you're tired of it you can give the Gitter channel a try, we're all on there, complaining about Java syntax.