1

I am new to these and I need a solution or idea how I can do it

I have the following code:

if (page.getId().equals("STEP_NAME_SUBMIT_OTP_FOR_TRANSACTION"))
    emRequestResponse.setTagElementList(agent.getHeader().getTagElementList().getSTEP_NAME_SUBMIT_OTP_FOR_TRANSACTION());
else if (page.getId().equals("STEP_NAME_SUBMIT_CAPTCHA_FOR_LOGIN"))
    emRequestResponse.setTagElementList(agent.getHeader().getTagElementList().getSTEP_NAME_SUBMIT_CAPTCHA_FOR_LOGIN());

I need a better way to call the method at run time based on the id, so if the name is STEP_NAME_SUBMIT_OTP_FOR_TRANSACTION I need to call the method getSTEP_NAME_SUBMIT_OTP_FOR_TRANSACTION and so on.

5
  • what's wrong with the approach ? Commented Feb 15, 2019 at 6:39
  • There can be like 10 to 15 id's at run time and i don't want if else 15 times or switch , i need a way in which i just get a id and i call a method based on that id Commented Feb 15, 2019 at 6:42
  • Do all these getXXX methods return the same type? If so, what is that type? Commented Feb 15, 2019 at 6:42
  • yes all return the same type Commented Feb 15, 2019 at 6:43
  • @ArvindCarpenter you can try with my answer . Commented Feb 15, 2019 at 6:48

3 Answers 3

2

You can call method like below code:

try {
Method method = emRequestResponse.getDeclaredMethod("method name", parameterTypes);
method.invoke(objectToInvokeOn, params);
} catch(Exception ex) {
System.out.println(ex.toString());
}
Sign up to request clarification or add additional context in comments.

3 Comments

in your case you have to pass below line to call getDeclaredMethod emRequestResponse.setTagElementList(agent.getHeader().getTagElementList().geDeclaredMethod();
so parameter type is the type of parameter we pass to that method right, and if so in my case there is no parameters to be passed then i will leave it as null or void
@ArvindCarpenter You can skip paramaters , only pass method name.
0

You may use switch over if-else, switch performs better than if-else.

Switch performance will be better as in case of switch there will be one time evaluation . Once it evaluated the switch it knows which case needs to be executed but in case of if else it has to go through all conditions in case of worst scenario.

In case of large number of conditions, better will be switch performance but for shorter list (just two conditions), it can be slower also

4 Comments

actually these id are generated at run time and corrosponding method is also generated at run time, so i don't know at starting position what will be the id's and hence i can not hard code these in switch case
in such case, if you know correlation between method name and parameter value you may use reflection api to invoke method
yes relation is if id is STEP_NAME_SUBMIT_OTP_FOR_TRANSACTION then the method name will be getSTEP_NAME_SUBMIT_OTP_FOR_TRANSACTION
i guess then you may use answer proposed by @Chetan Joshi below
0

The answer to your question is the REFLECTION API of java.

I would recommend using switch case for your strings, because, as per your requirement, I see that the Strings can be made final.

And using the Method class, you can invoke methods by populating the method name and required arguments.

The following block of code should do the trick for you, assuming the methods you want to call are defined in a class named TagElementsList.

private List invokeMethod(TagElementsList tagElementsList, String pageId) {
    Method method = TagElementsList.class.getMethod("get"+pageId);
    List returnList = (TagElementsList)method.invoke(tagElementsList);
    return returnList;
}

You can thus get the method working as per your needs by invoking a call to the invokeMethod method from the required location in your code by the following call, considering the above method is declared in the same class as the point from where it has to be invoked. You can of course put define it in a different class and invoke a call to the method via an instance of that class too.

List list = invokeMethod(agent.getHeader().getTagElementList(), page.getid());
 emRequestResponse.setTagElementList(list);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.