Class MethodCall
- java.lang.Object
-
- us.abstracta.jmeter.javadsl.codegeneration.MethodCall
-
- All Implemented Interfaces:
CodeSegment
- Direct Known Subclasses:
DslRecordingController.OnlyChildrenMethodCall,FragmentMethodCall
public class MethodCall extends Object implements CodeSegment
Represents a method call, it's parameters and chained invocations.It's main purpose is to generate the code for the method call, parameters and chained methods invocations.
- Since:
- 0.45
-
-
Field Summary
Fields Modifier and Type Field Description static StringINDENTDeprecated.protected StringmethodName
-
Constructor Summary
Constructors Constructor Description MethodCall(String methodName, Class<?> returnType, MethodParam... params)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringbuildAssignmentCode(String indent)StringbuildCode()Generates the code for this method call and all associated parameters, children elements and chained methods.StringbuildCode(String indent)protected static UnsupportedOperationExceptionbuildNoMatchingMethodFoundException(String methodCondition, MethodParam[] params)protected StringbuildParamsCode(String indent)static MethodCallbuildUnsupported()Allows to build a special method call used when some conversion is not supported.MethodCallchain(String methodName, MethodParam... params)Allows chaining a method call to this call.MethodCallchain(MethodCall methodCall)Allows to chain a method call in current method call.MethodCallchainComment(String comment)Allows to add a comment as part of the chain of commands.intchainSize()Allows to check the number of method calls chained into current method call.MethodCallchild(MethodCall child)Allows adding a child call to this call.static MethodCallemptyCall()Generates a method call that should be ignored (no code should be generated).protected static MethodfindParamsMatchingMethod(Stream<Method> methods, MethodParam[] params)static MethodCallforStaticMethod(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 MethodCallfromBuilderMethod(Method method, MethodParam... params)Set<String>getImports()Map<String,MethodCall>getMethodDefinitions()Class<?>getReturnType()Set<String>getStaticImports()voidheadingComment(String comment)Allow to add a heading comment to the method call.booleanisCommented()Allows to check if this method call is marked to be commented out.voidprependChild(MethodCall child)Allows adding a child method at the beginning of children methods.voidreChain(MethodCall other)Allows extracting from a given call the list of chained method calls and re assign them to this call.voidreplaceChild(MethodCall original, MethodCall replacement)Allows replacing a child method call with another.voidsetCommented(boolean commented)Marks or un-marks this method call as to be commented out.voidunchain(String methodName)Allows to remove an existing chained method call.
-
-
-
Field Detail
-
INDENT
@Deprecated public static final String INDENT
Deprecated.As of 1.3 useIndentation.INDENTinstead.- See Also:
- Constant Field Values
-
methodName
protected final String methodName
-
-
Constructor Detail
-
MethodCall
public MethodCall(String methodName, Class<?> returnType, MethodParam... params)
-
-
Method Detail
-
fromBuilderMethod
public static MethodCall fromBuilderMethod(Method method, MethodParam... params)
-
forStaticMethod
public 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.This is usually used to get clas factory methods calls. Eg: Duration.ofSeconds.
- Parameters:
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.- Returns:
- the newly created instance
-
buildUnsupported
public static MethodCall buildUnsupported()
Allows to build a special method call used when some conversion is not supported.- Returns:
- the special method call to include as child of other method calls.
-
setCommented
public void setCommented(boolean commented)
Marks or un-marks this method call as to be commented out.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.
- Parameters:
commented- specifies to comment or uncomment this method call.- Since:
- 1.3
-
isCommented
public boolean isCommented()
Allows to check if this method call is marked to be commented out.- Returns:
- true if the method call is marked to be commented out, false otherwise.
- Since:
- 1.3
-
headingComment
public void headingComment(String comment)
Allow to add a heading comment to the method call.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.
- Parameters:
comment- specifies the comment to add before the method call.- Since:
- 1.8
-
emptyCall
public static MethodCall emptyCall()
Generates a method call that should be ignored (no code should be generated).This is helpful when some MethodCallBuilder supports a given test element conversion, but no associated generated DSL code should be included.
- Returns:
- the empty method call.
-
getStaticImports
public Set<String> getStaticImports()
- Specified by:
getStaticImportsin interfaceCodeSegment
-
getImports
public Set<String> getImports()
- Specified by:
getImportsin interfaceCodeSegment
-
getMethodDefinitions
public Map<String,MethodCall> getMethodDefinitions()
- Specified by:
getMethodDefinitionsin interfaceCodeSegment
-
getReturnType
public Class<?> getReturnType()
-
child
public MethodCall child(MethodCall child)
Allows adding a child call to this call.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
ChildrenParamparameter, 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.
- Parameters:
child- specifies the method call to be added as child call of this call.- Returns:
- the current call instance for further configuration.
-
replaceChild
public void replaceChild(MethodCall original, MethodCall replacement)
Allows replacing a child method call with another.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.
- Parameters:
original- the method call to be replaced.replacement- the method call to be used instead of the original one.- Since:
- 1.3
-
prependChild
public void prependChild(MethodCall child)
Allows adding a child method at the beginning of children methods.This is mainly useful when in need to add configuration elements, that are usually added at the beginning of children calls.
- Parameters:
child- the child method to add at the beginning of children methods.- Since:
- 1.8
-
chain
public MethodCall chain(String methodName, MethodParam... params)
Allows chaining a method call to this call.This method is useful when adding property configuration methods (like
DslTestPlan.sequentialThreadGroups()) or other chained methods that further configure the element (likeDslHttpSampler.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.
- Parameters:
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.- Returns:
- this call instance for further chaining or addition of children elements.
- Throws:
UnsupportedOperationException- when no method with given names and/or parameters can be found to be chained in current method call.
-
chain
public MethodCall chain(MethodCall methodCall)
Allows to chain a method call in current method call.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.- Parameters:
methodCall- specifies the method call to chain- Returns:
- current method call for further usage.
- Since:
- 1.5
-
findParamsMatchingMethod
protected static Method findParamsMatchingMethod(Stream<Method> methods, MethodParam[] params)
-
chainComment
public MethodCall chainComment(String comment)
Allows to add a comment as part of the chain of commands.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.
- Parameters:
comment- the comment to chain.- Returns:
- the method call for further usage.
- Since:
- 1.5
-
buildNoMatchingMethodFoundException
protected static UnsupportedOperationException buildNoMatchingMethodFoundException(String methodCondition, MethodParam[] params)
-
reChain
public void reChain(MethodCall other)
Allows extracting from a given call the list of chained method calls and re assign them to this call.This is usually helpful when you provide in a DSL element alias methods for children elements. Eg:
DslBaseHttpSampler.header(String, String).- Parameters:
other- is the call to extract the chained methods from.
-
unchain
public void unchain(String methodName)
Allows to remove an existing chained method call.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).
- Parameters:
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.- Since:
- 1.8
-
chainSize
public int chainSize()
Allows to check the number of method calls chained into current method call.This is useful to check, for example, if a particular test element has any non default settings.
- Returns:
- the number chained method calls.
- Since:
- 1.8
-
buildCode
public String buildCode()
Generates the code for this method call and all associated parameters, children elements and chained methods.- Returns:
- the generated code.
-
buildCode
public String buildCode(String indent)
- Specified by:
buildCodein interfaceCodeSegment
-
-