Results 1 to 4 of 4
Thread: Code generation library?
- 05-24-2011, 04:32 AM #1
Code generation library?
Actually, "code generation library" is probably a poor description of what I'm looking for. I'm not looking for a bytecode generator like gclib. I'm also not looking for some kind of XML to POJO data binding.
What I'm looking for is a library that makes it simple and easy to output well-formed Java code. For example, I could create an object representing a class, set its package, add members, add methods, and then call its toString() method and get compilable Java code.
It almost seems as if you want a very smart IDE. What IDE are you currently using? If this is for professional development, have you considered IntelliJ Idea? It's kind of pricey but from what I've heard it's one of the best. There's also a free community edition but of course it's not as full featured as the full-fledged program.
- 05-24-2011, 07:01 AM #3
I'm using Eclipse.
Perhaps an explanation of what I'm doing would suggest a solution. I've written a XML Schema, and I'm about to start writing a very large XML document, describing messages in a simple client-server protocol for a game. Here's a sample:
<?xml version="1.0" encoding="UTF-8"?> <protocol xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/home/kevin/ZOMGXML/protocol.xsd"> <name>Requests</name> <description>Messages from the client to the server</description> <message-class>krum.sectorzero.protocol.Request</message-class> <enum-class>krum.sectorzero.protocol.RequestType</enum-class> <helper-class>krum.sectorzero.protocol.RequestHelper</helper-class> <message-type> <name>LOGIN</name> <description>Authentication credentials</description> <param> <name>user</name> <description>User's login name (normally their email address)</description> <type>String</type> <constant>USER</constant> </param> <param> <name>pass</name> <description>User's password in plaintext</description> <type>String</type> <constant>PASSWORD</constant> </param> </message-type> </protocol>
- an enum, named by protocol/enum-class, whose elements are named by protocol/message-type/name
- a class, named by protocol/helper-class, containing String constants named by protocol/message-type/param/constant and whose values are protocol/message-type/param/name; also containing static methods whose names are derived from protocol/message-type/name, and whose arguments are derived from protocol/message-type/param/name and protocol/message-type/param/type
- an interface for the receiver of the messages, with method names derived similarly to the above
- a couple other interfaces and classes
...all with javadoc comments derived from the appropriate description elements. These generated classes will compose the bulk of the client-server protocol library. The resulting user code will look like this:
// author of the request String user = "firstname.lastname@example.org"; String pass = "ihaterabbits"; Request rq = RequestHelper.login(user, pass); // send rq.toString() to server // receiver of the request RequestListener listener; // generated interface RequestDispatcher dispatcher = new RequestDispatcher(); // generated class dispatcher.addListener(listener); ... Request rq = new Request(stringFromClient); dispatcher.dispatch(rq); // dispatcher would call a method in the registered RequestListener in which something like this would happen String user, pass; user = rq.getString(RequestHelper.USER); pass = rq.getString(RequestHelper.PASSWORD);
Last edited by kjkrum; 05-24-2011 at 07:28 AM.
- 05-25-2011, 01:48 AM #4
I found some possible solutions here: eclipse - A Java API to generate Java source files - Stack Overflow
It helped when I thought to include the word "source" in my searches. I stopped using the phrase "source code" years ago, because it's redundant...
- By Hussain Ali in forum Advanced JavaReplies: 2Last Post: 02-24-2010, 08:55 AM
- By rummy in forum New To JavaReplies: 1Last Post: 02-08-2010, 01:57 PM
- By anilkumar_vist in forum Advanced JavaReplies: 1Last Post: 12-14-2009, 01:26 PM
- By jboy in forum New To JavaReplies: 2Last Post: 08-31-2009, 11:27 PM
- By 82rathi.angara in forum New To JavaReplies: 10Last Post: 09-09-2008, 05:11 AM