0

I have a string variable storing a method call in my Spring boot application.
ex.

public String foo(Result result) {
    String methodCall = "result.getStudenMaster().getStudentName()";
    return methodCall;
}

Its storing result.getStudenMaster().getStudentName() as a value in the variable methodcall instead of a value returned by result.getStudenMaster().getStudentName()

ajay rathod
  • 3
  • 1
  • 3
  • Try without double quotes – the_tech_maddy Mar 30 '19 at 12:51
  • actually that method name is coming from the database – ajay rathod Mar 30 '19 at 12:52
  • i have stored list of method calls to be made in the database. and according to the user input the code will identify which method to be called. here that method is result.getStudenMaster().getStudentName() and i am trying to store value returned from result.getStudenMaster().getStudentName() method into the methodCall variable – ajay rathod Mar 30 '19 at 12:55
  • ya exactly, the string is coming from database. – ajay rathod Mar 30 '19 at 12:57
  • 1
    `if (methodCall.equals("result.getStudenMaster().getStudentName()")) { return result.getStudenMaster().getStudentName(); }`. That may sound silly, but what is really silly is to store something like that in the database. Your data shouldn't be dependant on implementation details of the application that happens to be using this data now. Change your design. – JB Nizet Mar 30 '19 at 13:31
  • Basically what I am doing here is, I'm allowing user to make a dynamic mail template in which he'll be using predefined fields like **student name** in our example. and with that field i am also storing the place where it should look for the value of the keyword . i.e. **result.getStudenMaster().getStudentName()**. – ajay rathod Mar 30 '19 at 13:40
  • Even in that case you should perhaps do an if tree, map or some pattern, not execution through Strings. That is really unstable, also it opens up security vulnerabilities and even if that is put aside it looks like an anti-pattern. But all that aside, if you are 100% sure you it's the best solution, check this SO https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string – Tarmo Mar 30 '19 at 13:56
  • Possible duplicate of [How do I invoke a Java method when given the method name as a string?](https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string) – Tarmo Mar 30 '19 at 14:07

0 Answers0