Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

Executing string as a statement

Hi Experts,

This might be a stupid question, but I will ask anyway. If I store numerous java statements as string in…string array for example, is there any way that I can execute them?

For example, if I have the following:

String [] TEST = {“this.getNum1()”, “this,getNum2()”, …};

Is there any way that I can execute TEST[0] and TEST[1], so I can use result of executing this.getNum1() and this.getNum2()?

thanks,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

With reflection, yes. Use Class.getMethod
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18
dkim18

ASKER

What if I need to use this in struts action like this:

++++++++++++++++++++++++++
  public ActionForward validate(ActionMapping mapping,// first entry
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)throws Exception {
...
...
          ProjectForm projectForm = (ProjectForm)form;  
...
...
++++++++++++++++++
So, I am trying to get values in projectForm bean by using getter methods in run time; furthermore, I am trying to make to make this generialize so that I can use this for many actionform. Now, I am thinking if I use reflection, I create new object then I won't be able to get user inputs from the form bean.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18

ASKER

kannan_ekanath,

thanks for your comment and it works. Now, I have additional question and I increased point. Is it possible to apply setter method here? Since setter methods take string as parameter, how do I get that method?

thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok,
You seem to have got the point. But reflection is really powerful. You can check the javadocs in JDK and also frameworks like Spring, Hibernate which have complete utility classes for these kind of stuff. Spring is a big library serving a variety of needs, and it is modular, so you can just pick up the spring-util library which by all probablity would have all these stuff.

Doing a setter would also be in similar lines. I dont see anything wrong with the code that objects has posted above. In particular whenever you are doing something like this, you need to zoom out and see if there are frameworks which can do stuff. Since you are doing web stuff and I guess you are using Struts, Why not give a shot at 'Webwork' a sensible replacement for struts (struts gurus kindly excuse me, but webworks is simply too good).

Webwork does all these essential plumbing, and a whole lot of features......check it out!!!
:-)