I have a java class say Class A with some methods already present, I am generating a class using code model say classB and while generating using code model I am trying to call one the method of that classA.
i tried below
method
.body()
.invoke(JExpr.ref(helper), "display")
.arg("hello");
but it is not working, I will really appreciate if someone knows how to do the same
I want to generate a method like:
public void method() {
Helper helper = new Helper();
helper.display("hello")
}
I am also interested in how I can generate the following method:
@Test
public void method() {
Assert.fail("message")
}
Let's start with:
Assuming you already have a
JMethod method, you first need to create ahelperinstance:Then just invoke the desired method on it:
No this one:
It's even easier, you only have to do a static invocation. Something along the lines:
If you're interested in the annotation:
Note that in the invocations above I could pass strings to the
argmethod directly (arg("message")). This is just a convenience method for strings. If you want to use other types like primitve types, you will need to do something likeJExpr.lit(12.34).