Dynamically build java/scala method body at runtime and execute it -


suppose have following interface in java:

public interface dynamicmethod {     string doit(); } 

i build object during runtime conforms above interface such inject doit method body in , execute it? possible java reflection api, or other way? or in way in scala?

note doit body objects dynamic , not known priori. can assume in run-time array codearray[1..10] of strings provided , each entry of array holds code each doit method. appreciate if answer sample code.


the context: try explain context of problem; nonetheless, above question still remains independent context.

i have commands c1,c2, ...; each command has parameters. based on command , parameters system needs perform task (which expressible using java code.) need these commands stored future execution based on user demand (so codearray[1..10] in above holds list of java codes). example, user chooses command list (i.e., array) , demands execution.

my thought build engine based on user selection, loads corresponding command code array , executes it.

with context added, sounds me have interpreter..

for example, sql takes input "select * users", parses , builds tree of tokens interprets.

another example: java's regex interpreter. string "[abc]+" compiled tokens, , interpreted when executed. can see tokens (called nodes) uses in source code.

i'll try post simple example later, interpreter pattern doesn't use dynamically generated code. of tokens concrete classes. have define possible (valid) user input can make token execute however. sql , regex has defined syntax, need 1 also.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -