views:

237

answers:

2

In Visual Studio land, I used to be able to define a structure in an XSD file and add a special attribute to it which would cause it to be dynamically compiled and available to use with intellisense in the other C# files in the application. I am not sure exactly what the term for this is, perhaps "dynamic code generation."

I am trying to accomplish the same in Java using Eclipse IDE. Basically what I am looking for is a tool that will allow me to specify some template and generate Java code from it in a "hot folder" that will allow me code complete in the other static Java files.

Does anyone know of a solution for this? I know it is possible in Visual Studio, but I can't seem to find anything for Eclipse.

Ok, here is exactly what I want to do.

Step 1. I create a folder called templates Step 2. I create a file called HelloWord.ibes Step 3. Code it automatically generated in my src folder HelloWorld.java

I want to be able to do this in eclipse easily.

+2  A: 

I'm not sure whether you have seen the code template option?

Preferences.Java, Code Style then Code Templates

Pool
Thats not exactly what I am looking for. Basically I want to have say an XML file and have it processes dynamically to generate Java source code.
John Sonmez
I believe this is only usable within one source file. If you read the question, it seems that John wants to generate multiple source files.
Hans Doggen
@John, OK. I wasn't sure whether you meant the base code formatting.
Pool
+2  A: 

You may create an ant build file that does the source generation for you. Then you are free to use any code generator you like. Ant support is part of the eclipse IDE. If you prefer maven, there's a nice eclipse pluging available (that's what I actually use for source code generation based on jaxb, javacc and xdoclet...).

Technically spoken, you just add another eclipse builder which is invoked anytime eclipse detects a change in your code base.

If you already have a code generator in mind, just 'ask' the internet if there's a plugin available.

Edit

On how to install a builder: This is done automatically. For maven, you just install the maven plugin (m2eclipse) and enable maven dependencies for a project. Then if you look at the projects properties pages (Builder section), you find a second entry in the list of builders.

It's similiar with ant, even easier, because ant is already integrated. "enable" ant for a project and the builder is added to the list of builders for the project. You can deselect it at any time if it kills performance or switch of automatic building (I don't know by heart how to enable ant builds for a project, but I remember that the eclipse help had sufficiant informations).

All about ant can be found here: Apache Ant

Creating a new builder is difficult, as it has to be coded in java and added to eclipse as a plugin. I bet you don't want to follow that track ;)

Andreas_D
This might work. How do you add an eclipse builder? I didn't see the options in preferences.
John Sonmez
Thanks, I was actually able to get this to work. I ended up writing my own generator class to parse my source files and generate java source from them, then I used the ant builder to automatically call my generator and output the files.
John Sonmez