public class MethodCall extends Object implements CodeSegment
It's main purpose is to generate the code for the method call, parameters and chained methods invocations.
| Modifier and Type | Field and Description |
|---|---|
static String |
INDENT
Deprecated.
|
protected String |
methodName |
| Constructor and Description |
|---|
MethodCall(String methodName,
Class<?> returnType,
MethodParam... params) |
| Modifier and Type | Method and Description |
|---|---|
String |
buildAssignmentCode(String indent) |
String |
buildCode()
Generates the code for this method call and all associated parameters, children elements and
chained methods.
|
String |
buildCode(String indent) |
protected static UnsupportedOperationException |
buildNoMatchingMethodFoundException(String methodCondition,
MethodParam[] params) |
protected String |
buildParamsCode(String indent) |
static MethodCall |
buildUnsupported()
Allows to build a special method call used when some conversion is not supported.
|
MethodCall |
chain(MethodCall methodCall)
Allows to chain a method call in current method call.
|
MethodCall |
chain(String methodName,
MethodParam... params)
Allows chaining a method call to this call.
|
MethodCall |
chainComment(String comment)
Allows to add a comment as part of the chain of commands.
|
int |
chainSize()
Allows to check the number of method calls chained into current method call.
|
MethodCall |
child(MethodCall child)
Allows adding a child call to this call.
|
static MethodCall |
emptyCall()
Generates a method call that should be ignored (no code should be generated).
|
protected static Method |
findParamsMatchingMethod(Stream<Method> methods,
MethodParam[] params) |
static MethodCall |
forStaticMethod(Class<?> methodClass,
String methodName,
MethodParam... params)
Generates a new instance for a static method within a given class that is applicable to a given
set of parameters.
|
static MethodCall |
fromBuilderMethod(Method method,
MethodParam... params) |
Set<String> |
getImports() |
Map<String,MethodCall> |
getMethodDefinitions() |
Class<?> |
getReturnType() |
Set<String> |
getStaticImports() |
void |
headingComment(String comment)
Allow to add a heading comment to the method call.
|
boolean |
isCommented()
Allows to check if this method call is marked to be commented out.
|
void |
prependChild(MethodCall child)
Allows adding a child method at the beginning of children methods.
|
void |
reChain(MethodCall other)
Allows extracting from a given call the list of chained method calls and re assign them to this
call.
|
void |
replaceChild(MethodCall original,
MethodCall replacement)
Allows replacing a child method call with another.
|
void |
setCommented(boolean commented)
Marks or un-marks this method call as to be commented out.
|
void |
unchain(String methodName)
Allows to remove an existing chained method call.
|
@Deprecated public static final String INDENT
Indentation.INDENT instead.protected final String methodName
public MethodCall(String methodName, Class<?> returnType, MethodParam... params)
public static MethodCall fromBuilderMethod(Method method, MethodParam... params)
public static MethodCall forStaticMethod(Class<?> methodClass, String methodName, MethodParam... params)
This is usually used to get clas factory methods calls. Eg: Duration.ofSeconds.
methodClass - the class that contains the static method.methodName - the name of the method to search for in the given class.params - the parameters used to search the method in the given class and to associate
to the method call.public static MethodCall buildUnsupported()
public void setCommented(boolean commented)
This is mainly used when you want to provide users with an easy way to enable an existing part of a test plan that is currently not enabled or used.
commented - specifies to comment or uncomment this method call.public boolean isCommented()
public void headingComment(String comment)
This is helpful to add some note or comment on created element. Mainly comments that require users attention, like reviewing and/or changing a particular part of test plan.
comment - specifies the comment to add before the method call.public static MethodCall emptyCall()
This is helpful when some MethodCallBuilder supports a given test element conversion, but no associated generated DSL code should be included.
public Set<String> getStaticImports()
getStaticImports in interface CodeSegmentpublic Set<String> getImports()
getImports in interface CodeSegmentpublic Map<String,MethodCall> getMethodDefinitions()
getMethodDefinitions in interface CodeSegmentpublic Class<?> getReturnType()
public MethodCall child(MethodCall child)
This method should only be used in seldom scenarios where you need to manually add children calls. In most of the cases this is not necessary, since DSL framework automatically takes care of JMeter children conversion.
If the call defines a ChildrenParam parameter, then children are just added as
parameters of the call. Otherwise, a children method will be looked into the class retunrned by
this method, and if there is, then chained into this call and used to register provided child
element.
Warning: You should only use this method after applying any required chaining.
child - specifies the method call to be added as child call of this call.public void replaceChild(MethodCall original, MethodCall replacement)
This is useful when some element has to alter an already built method call, for example when replacing module controllers by test fragment method calls.
original - the method call to be replaced.replacement - the method call to be used instead of the original one.public void prependChild(MethodCall child)
This is mainly useful when in need to add configuration elements, that are usually added at the beginning of children calls.
child - the child method to add at the beginning of children methods.public MethodCall chain(String methodName, MethodParam... params)
This method is useful when adding property configuration methods (like
DslTestPlan.sequentialThreadGroups()) or other chained methods that further configure
the element (like
DslHttpSampler.post(String, ContentType).
This method abstracts some common logic regarding chaining. For example: if chained method only contains a parameter and its value is the default one, then method is not chained, since it is not necessary. It also takes care of handling boolean parameters which chained method may or may not include a boolean parameter.
methodName - is the name of the method contained in the returned instance of this method
call, which has to be chained to this method call.params - is the list of parameters used to find the method and associated to the
chained method call. Take into consideration that the exact same number and
type of parameters must be specified for the method to be found, otherwise an
exception will be generated.UnsupportedOperationException - when no method with given names and/or parameters can be
found to be chained in current method call.public MethodCall chain(MethodCall methodCall)
This method is handy when you want to chain a method that actually currently is not available. Mainly as a marker of a feature that could be implemented in the future but still isn't (like authentication methods still not implemented).
In general cases chain(String, MethodParam...) should be used instead.
methodCall - specifies the method call to chainprotected static Method findParamsMatchingMethod(Stream<Method> methods, MethodParam[] params)
public MethodCall chainComment(String comment)
This is useful to add notes to drive user attention to some particular chained method. For example, when parameters passed to a chained method need to be reviewed or changed.
comment - the comment to chain.protected static UnsupportedOperationException buildNoMatchingMethodFoundException(String methodCondition, MethodParam[] params)
public void reChain(MethodCall other)
This is usually helpful when you provide in a DSL element alias methods for children elements.
Eg: DslBaseHttpSampler.header(String, String).
other - is the call to extract the chained methods from.public void unchain(String methodName)
This is useful when you need to alter an already created method call, for example, when optimizing a conversion and removing settings that are already covered by some other configuration element (eg: httpDefaults).
methodName - specifies the name of the chained method to be removed. If there are multiple
methods chained with same name, then all of them will be removed.public int chainSize()
This is useful to check, for example, if a particular test element has any non default settings.
public String buildCode()
public String buildCode(String indent)
buildCode in interface CodeSegmentCopyright © 2024. All rights reserved.