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,
Java EEJava

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

With reflection, yes. Use Class.getMethod
SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mick Barry

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Kannan Ekanath

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!!!
CEHJ

:-)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.