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.
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:
There will be many more message-type elements. From this, I want to generate several Java files:
<?xml version="1.0" encoding="UTF-8"?>
<protocol xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/home/kevin/ZOMGXML/protocol.xsd">
<description>Messages from the client to the server</description>
<description>User's login name (normally their email address)</description>
<description>User's password in plaintext</description>
- 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:
So I lied -- I am looking for an XML to Java conversion or binding. But not the kind of direct element/member correspondence supported by (my limited understanding of) the libraries I'm finding.
// 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
Request rq = new Request(stringFromClient);
// 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);
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...