Link to home
Start Free TrialLog in
Avatar of JoshWegener
JoshWegener

asked on

Execute Dynamic Code / Function

say we have a function called "RunThisCode" and "RunThisFunction"

When we pass the, RunThisFunction, a string it runs that funciton in the current class - ( how / can ) is this done?
When we pass the, RunThisFunction, a string it runs that funciton in a DIFFERENT class - ( how / can ) is this done?
When we pass the, RunThisCode, a string it runs that code - ( how / can ) is this done?

Thanks,
Josh Wegener
Avatar of radarsh
radarsh

Hi JoshWegener,

Please elaborate... give code examples. I didn't quite get you.

________
radarsh
Avatar of JoshWegener

ASKER

sorry about using the word "Class" I was thinking of C++ even though this will be in Java

EXAMPLE (this is not real code):

FileOne
{

   function main()
   {
      RunThisFunction("findThisFunction()");
      RunThisFunction2("TestObject.findThisFunction()");
      RunThisCode("int T = 0; T = T ++; msgBox(T);");
   }

   function RunThisCode(String Code)
   {
      // run code
   }

   function findThisFunction()
   {
      msgBox("it worked");
   }

   function RunThisFunction(String functionToRun)
   {
      // call functionToRun
   }

   function RunThisFunction2(String functionToRun)
   {
      // call functionToRun
   }

}

FileOne2 TestObject
{
   function findThisFunction()
   {
      msgBox("it worked");
   }

}
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
How would I use that to do what I need to do?
The examples show you how to call a Method of a Class
Could you please putt the code into my example?
No time at the moment sorry
Ok, well who ever can show me HOW to do this (using my example above) well get full points!
>>well get full points!

That can't be the case - i've already partly answered your question
Sir, I asked How to do somthing - I did not ask you to point me to a site that gave some examples that I have to figure out. I do not have the time right now to go and test the code - that I is why I asked for examples.

How about this who ever can show me HOW to do this (using my example above) well get 400 points!
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
So if I wanted to pass it prams - I would putt that where it says null??

FileOne.class.getMethod(functionToRun, null).invoke(this, new Object[] {"Pram1", "Pram2"}); // pram 1 and 2 are Strings
FileOne.class. = what ever the class is right? so for example "java.lang.String.class"

so say I was calling the string class... I would do this...

java.lang.String.class.getMethod(functionToRun, null).invoke(this, new Object[] {"Pram1", "Pram2"}); // pram 1 and 2 are Strings
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
You put the params in the Object[] argument of the invoke() method.

________
radarsh
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
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
There is one BIG problem here... and that is "java.lang.String.class" is ina STRING..... this is Dynamic... I can not hard code it...
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
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
This look right?

Object function runThisClass(String className, String methodToRun, Object pramsToSend[], Object pramsTypes[])
{
   Class classToUse = Class.getName(classname);

   Object result = classToUse(methodToRun, pramsTypes).invoke(this, pramsToSend);
}

void function main()
{
   String test = (String) runThisClass("java.lang.String.class", "concat", {"This", "and", "this"}, {String.class, String.class, String.class});
}
oops

"Class classToUse = Class.getName(classname);" -  SHOULD BE - C"lass classToUse = Class.getName(className);"
"Object result = classToUse" - SHOULD BE - "Object result = classToUse.getMethod"
and so if I want to call a method in the current class.... I would do this?

void function main()
{
   String test = (String) runThisClass("this", "concat", {"This", "and", "this"}, {String.class, String.class, String.class});
}
yep, that looks right.
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
Object function runThisClass(String className, String methodToRun, Object pramsToSend[], Object pramsTypes[])
{
   Class classToUse = Class.getName(classname);

   return classToUse(methodToRun, pramsTypes).invoke(this, pramsToSend);
}

Object function runThisClass(Class classToUse, String methodToRun, Object pramsToSend[], Object pramsTypes[])
{
   return classToUse(methodToRun, pramsTypes).invoke(this, pramsToSend);
}

void function main()
{
   // method out side this class/object
   String test = (String) runThisClass("java.lang.String.class", "concat", {"This", "and", "this"}, {String.class, String.class, String.class});

  // Method in this class/object
   String test = (String) runThisClass(this.getClass(), "concat", {"This", "and", "this"}, {String.class, String.class, String.class});
}
Now that we have that all done... that brings us to the last question...

> When we pass the, RunThisCode, a string it runs that code - ( how / can ) is this done?
>> When we pass the, RunThisCode, a string it runs that code - ( how / can ) is this done?

You need to invoke an interpreter. Try Groovy or BeanShell
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
> Object function runThisClass(String className, String methodToRun, Object pramsToSend[], Object pramsTypes[])

actually the above is wrong, you also need to pass the object that you want to call the method on
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
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
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
for the last one it depends on what language the code you want to run is in :)
Java
If its a complete class you could use the javac class to compile it and then run the resulting class.

otherwise you're going to need to write your own interpreter, which is no small task.
>>otherwise you're going to need to write your own interpreter

No you're not. You're going to have to *call* an interpreter (see my previous comments on this)
:-)