Search code examples
javacode-generationjtextareacode-editor

Sensing the changes done in code


I've created a standalone java app using netbeans 6.9. In my app if a user selects some options(values for some specific fields) then my app automatically generates its corresponding code.That code is displayed in the text area in my app itself.
For example: In my app I've a button for a new variable. If the user clicks on it,my app asks for the variable name. After the name has been entered my app automatically generates the corresponding code for that.
But I want an option for the user to edit that code.
Suppose the user enters that variable declaration code himself on the text area then my app should sense the presence of that code.
I mean in my app I also have a combobox which shows all the variables declared. So if the user enters that code and saves it then in my app that new variable for which the user has written code for, should be present.
Can anyone help me with this.
-Thanks in advance


Solution

  • While you can do what you're asking in Java, up to a point, Java probably isn't the language best suited to this task. A dynamic language would probably be better. There are a number of Dynamic languages for the JVM. Perhaps Groovy would be a good fit, since it has a Java-like syntax.

    Disclaimers aside, to do what you're doing in Java you'd want to:

    1. Generate a Java source file containing the contents of your text area. (This can actually be done in-memory by creating your own implementation of JavaFileManager and friends.)
    2. Invoke the Java compiler on said source.
    3. Load the generated bytecode with a ClassLoader.