Code generation tool / language suggestions

I need to generate a lot of near-identical classes by splicing together code snippets based on some kind of high-level template. The generated code will be read-only, so any changes will mean changing the snippets and regenerating them. Does anyone have any suggestions of code generating tools for Java code? I could just hack together something custom with python, but I’m wondering if there’s something more specific and better already out there. What are LWJGL and Jogl using for generating code?

Cheers

What do you want to use as input? What data do you have? A list of classes? Or a class diagram?

The input will probably be text only, consisting of code fragments (ie. several lines of Java code, possibly with variable names to be changed later) and templates indicating how the fragments will be stitched together. Possibly something like:


fragment BasicVertexOutput
data.put(x).put(y).put(z);

fragment AdvancedVertexOutput
data.put(x).put(y).put(z);
data.put(r).put(g).put(b).put(a);

template Sprite
public class Sprite
{
  public void draw(Buffer data)
  {
    // common code
   // ..

  #fragment VertexOutput
  }
}

output Sprite<BasicVertexOutput> BasicSprite
output Sprite<AdvancedVertexOutput> AdvancedSprite

Which would output code for a BasicSprite, usng the Sprite template code and substituting in the BasicVertexOutput code and similarly for AdvancedSprite.

uhu… why don’t you do that with polymorphism and method overriding? is it just performance? or do you have other use cases which forbid that?

it might be feasable to use an aspect weaver as well - if you like those :wink:

another option could be interpreting your file with e.g. velocity and put your parameters (BasicVertexOutput, BasicVertexOutput) as template names into the context…

That looks almost like something you could do with a (ick) preprocessor?

If a preprocessor or an ant script isn’t enough, you could use Javassist.
Javassist makes it easy to generate such templated classes at runtime - you create a subclass of Sprite, get the code for draw(…) as a String somehow, insert it in the subclass, compile it, and your new class is ready to use.

Performance and practicality. Performance because to do it with polymorphism would require a whole heap of virtual function calls per vertex which isn’t acceptable. Practicallity because there’s three different parts (vertex format, texture unit usage, shader attributes) each which might have a handful of different sections of code, and can be used in any combination. Writing them all out by hand isn’t practical.

Abuse: A preprocesser could probably be convinced to do the job, but I don’t know of any for Java, do you know of any good ones (preferably ones which work nicely with Eclipse?

I don’t think that this is a problem with recent hotspot versions. If you make the subclasses final it should not be a real performance hit after some invocations (they’re inlined).

[quote=“Orangy Tang,post:7,topic:31795”]
If you would use polymorphism, would it be practical then? (I think so, even after reading your statement about practicality)

Use a templating engine like Freemarker? or Velocity?
I’ve used Freemarker before with good outcomes.
http://freemarker.sourceforge.net/

that is what I meant with “interpreting your file with e.g. velocity”. Of course the question remains, if it smoothly integrates with eclipse, keeping java completion etc. while being a proper template… as this could get nasty one should not do it if it’s not necessary :slight_smile:

Regardless of how good or not hotspot is, it’s still doing work which can be completely eliminated at compile time. Even if it manages to inline the calls (which I doubt it would be able to) it’s still adding overhead which is unneeded.

I suspect using polymorphism would work for about half of the cases, but probably not for the more tricky ones where there’s multiple extra tex coord and vertex shader attributes. Those could concievably be done via some convoluted Map of String -> arrays of VertexAttrib interface, but that would be far, far too much overhead and would make the calling code ugly as sin.

I fear when using a template engine you will loose all java code assistent in an IDE. Maybe you could write aspects and let them weave in at compiletime using aspectj. I personally don’t use eclipse, but I heard it has support for writing aspects with aspectj.

I don’t know the details, but LWJGL uses templates and annotations to generate code. Go take a look at it. If you read the build.xml first you can see where to start looking for the code generation.

Why not parse it yourself?

Using a slightly easier format (more ‘pronounced’ delimiters) would make parsing and processing quite easy.

Just for ‘fun’, I’m giving it a try, to see how hard it actually is… (to make it reliable)

Maybe it’s doable doing it in runtime (or rather init-time) using Janino?

Indeed, but Janino has it’s own quirks, like generating bytecode that triggers the JVM validator - especially wrong types in the stack.

Further, you’d have to call all your classes through some interface (otherwise you’d need reflection). Basically every generated class implements the Sprite interface or extends the Sprite class, after which HotSpot turns every method-call into a massive jumptable in native code (as with all polymorphism). Performance really drops in both client and server VM.

So even if you’d use some tool to generate the sourcecode, you shouldn’t make all implementations implement the same interface.

I think it’s looking like the best approach will be to write something in Python to generate these classes, as Riven said with suitably pronounced deliminators it should be reasonably easy - I’ll probably just pinch the C preprocessor style syntax with #begin and #end blocks.

It’s not really possible to do it at runtime because they certainly won’t all implement the same interface (or rather, they will implement a common interface, but each type will have additional functionality as well as that which requires additional methods).

I would look into XDoclet and/or Velocity.

Both integrate well with Ant and Maven too.