I'm using Codemodel library for java-class generation. Is there a way to generate a generic method invocation which looks like this:
clazz.<String>get(value)
There is certainly a way of just casting a return result to a correct type using the following expression:
JExpr.cast(stringType, clazz.invoke("get").arg(value))
which results in
(String) clazz.get(value)
but the preferred way of casting is the first one, as this code is generating templates for further manual editing by developers.
With the existing JCodeModel API, there is no pre-built way to handle this. You can, however, define your own
JStatementtype to generate the generic declaration like this:Which generates:
This is by no mean a complete solution (It's missing method call arguments for instance). Take a look at the implementation of the
generate()method inJInvocationfor the details that are required.