Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Code generation tool / language suggestions  (Read 6746 times)
0 Members and 1 Guest are viewing this topic.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Posted 2008-05-13 14:49:15 »

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

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline irrisor

Junior Member





« Reply #1 - Posted 2008-05-13 15:07:17 »

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

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2008-05-13 15:27:57 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline irrisor

Junior Member





« Reply #3 - Posted 2008-05-13 18:32:19 »

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...
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #4 - Posted 2008-05-13 20:41:02 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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.

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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Martin Strand

Junior Member





« Reply #5 - Posted 2008-05-13 22:18:45 »

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.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2008-05-13 22:47:21 »

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?
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?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline irrisor

Junior Member





« Reply #7 - Posted 2008-05-14 07:50:40 »

Performance because to do it with polymorphism would require a whole heap of virtual function calls *per vertex* which isn't acceptable.
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).

practicality
If you *would* use polymorphism, would it be practical then? (I think so, even after reading your statement about practicality)
Offline Don Kiddick

Junior Member





« Reply #8 - Posted 2008-05-14 08:09:52 »

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

Junior Member





« Reply #9 - Posted 2008-05-14 08:18:32 »

Use a templating engine like Freemarker? or Velocity?
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 Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #10 - Posted 2008-05-14 09:37:46 »

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).
If you *would* use polymorphism, would it be practical then? (I think so, even after reading your statement about practicality)

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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline cylab

JGO Ninja


Medals: 49



« Reply #11 - Posted 2008-05-14 09:47:35 »

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.

Mathias - I Know What [you] Did Last Summer!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #12 - Posted 2008-05-14 12:14:28 »

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.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 796
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2008-05-14 14:09:43 »

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)


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #14 - Posted 2008-05-15 18:48:02 »

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

Online Riven
« League of Dukes »

JGO Overlord


Medals: 796
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2008-05-15 19:00:00 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #16 - Posted 2008-05-15 19:53:26 »

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).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline jjmontes

Junior Newbie





« Reply #17 - Posted 2009-03-13 13:02:46 »


I would look into XDoclet and/or Velocity.

Both integrate well with Ant and Maven too.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (38 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!