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
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
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("findThisF unction()" );
RunThisFunction2("TestObje ct.findThi sFunction( )");
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");
}
}
EXAMPLE (this is not real code):
FileOne
{
function main()
{
RunThisFunction("findThisF
RunThisFunction2("TestObje
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How would I use that to do what I need to do?
The examples show you how to call a Method of a Class
ASKER
Could you please putt the code into my example?
No time at the moment sorry
ASKER
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
That can't be the case - i've already partly answered your question
ASKER
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!
How about this who ever can show me HOW to do this (using my example above) well get 400 points!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So if I wanted to pass it prams - I would putt that where it says null??
FileOne.class.getMethod(fu nctionToRu n, null).invoke(this, new Object[] {"Pram1", "Pram2"}); // pram 1 and 2 are Strings
FileOne.class.getMethod(fu
ASKER
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.get Method(fun ctionToRun , null).invoke(this, new Object[] {"Pram1", "Pram2"}); // pram 1 and 2 are Strings
so say I was calling the string class... I would do this...
java.lang.String.class.get
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You put the params in the Object[] argument of the invoke() method.
________
radarsh
________
radarsh
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.St ring.class ", "concat", {"This", "and", "this"}, {String.class, String.class, String.class});
}
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.St
}
ASKER
oops
"Class classToUse = Class.getName(classname);" - SHOULD BE - C"lass classToUse = Class.getName(className);"
"Object result = classToUse" - SHOULD BE - "Object result = classToUse.getMethod"
"Class classToUse = Class.getName(classname);"
"Object result = classToUse" - SHOULD BE - "Object result = classToUse.getMethod"
ASKER
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});
}
void function main()
{
String test = (String) runThisClass("this", "concat", {"This", "and", "this"}, {String.class, String.class, String.class});
}
yep, that looks right.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.St ring.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});
}
{
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.St
// Method in this class/object
String test = (String) runThisClass(this.getClass
}
ASKER
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?
>> 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
You need to invoke an interpreter. Try Groovy or BeanShell
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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
actually the above is wrong, you also need to pass the object that you want to call the method on
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for the last one it depends on what language the code you want to run is in :)
ASKER
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, 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)
No you're not. You're going to have to *call* an interpreter (see my previous comments on this)
:-)
Please elaborate... give code examples. I didn't quite get you.
________
radarsh