0

I am writing framework which required to store methods in database. The thing is I have written all the method in java file, now I have UI where user will select the method and when user click on save button, system needs to copy selected method from java file and save in database, the reason doing this is when running application system will call id which will be mapped with stored method in database and will execute that method to achieve goal. Is this possible? and how to copy method from java class file and store in db?

Is there any other suggestion or input will be more appreciated.

Thanks,

Karim Narsindani
  • 434
  • 3
  • 14
  • 38
  • search for reflection api java – vipul mittal Jun 26 '14 at 10:32
  • While technically possible, this sounds like a really bad idea. Rather than storing actual code in a database, you should just store commands and interpret them when reading them out. – chrylis -cautiouslyoptimistic- Jun 26 '14 at 10:54
  • @Chrylis, Thanks for your inputs. However, I have thought the same thing, but the requirement is user should be able to modify method in UI. That is when user select method 'test()', system will display test method code in text area where user will have right to modify this method to achieve user goal. Hence, we decided that we can store method in db and while executing code, it will be called from db. – Karim Narsindani Jun 26 '14 at 11:03
  • Then use Groovy instead. – chrylis -cautiouslyoptimistic- Jun 26 '14 at 12:02
  • @chrylis, we have change are implementation based on your suggestion. Now we are going to store only commands in database. I need your inputs on creating java source code based on selected method. Just want to clear here that, class name and method will be provided by user and based on user we need to create java class file. Could you please help me? Is there any api or tool which we can implement in our application to generate source code dynamically? – Karim Narsindani Jul 03 '14 at 13:07
  • I really, really, recommend looking at embedding Groovy as a scripting option. – chrylis -cautiouslyoptimistic- Jul 03 '14 at 13:31

1 Answers1

1

Getting a source code for method would be difficult as the original code is not available to a running program.

Know more here

But you can do the following:

Using java reflection api(javaassist) you can add a method to existing class. And start from storing first definition of the method in DB fetch it and put it in a class. So you can, at any time, get it and display as well as change.

Community
  • 1
  • 1
vipul mittal
  • 17,343
  • 3
  • 41
  • 44