Results 1 to 4 of 4
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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.
    Get in the habit of using standard Java naming conventions!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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:

    Java Code:
    <?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>
    There will be many more message-type elements. From this, I want to generate several Java files:

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

    Java Code:
    // author of the request
    String user = "bob@example.com";
    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);
    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.
    Last edited by kjkrum; 05-24-2011 at 08:28 AM.
    Get in the habit of using standard Java naming conventions!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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...
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. I want to put the native library in library path in mac os x
    By Hussain Ali in forum Advanced Java
    Replies: 2
    Last Post: 02-24-2010, 08:55 AM
  2. War file generation
    By rummy in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 01:57 PM
  3. Report generation
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 12-14-2009, 01:26 PM
  4. Auto id generation
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 12:27 AM
  5. Tell me jar file for library library org.bouncycastle.cms
    By 82rathi.angara in forum New To Java
    Replies: 10
    Last Post: 09-09-2008, 06:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •