Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate java code in Eclipse?

Does anyone know what approach one can take to automatically generate Java source code, from for example an xml or json file, in eclipse?

One great example of what I am thinking of doing is what Google Android sdk does: they have an R class generated automatically from the resources.

Every time a resource file is saved in Eclipse R class is automatically regenerated.

UPDATE: Example: In the text (xml or json file) I have the following:

 <tags>
     <tag id="ALPHA">
         <description>The first alpha tag.</description>
         <value>231232</value>
     </tag>
     <tag id="BETA">
         <description>This is the beta tag.</description>
         <value>231232</value>
     </tag>

Then in my generated java class, say R I would have something like:

R.tags.ids.ALPHA //refers to an enum value for example
R.tags.values.ALPHA //refers to final int with avlue 231232
R.tags.descriptions.ALPHA //refers to the String with description

Thanks!

like image 915
Andriy Drozdyuk Avatar asked Jun 02 '10 17:06

Andriy Drozdyuk


People also ask

How do I create a new Java class in Eclipse?

You can use the toolbar item with the circled C icon, you can select File→ New→ Class, or you can right-click a project in the Package Explorer and select New→ Class in the context menu. All these methods open the New Java Class dialog.

How can I get source code from Eclipse?

Go to Windows->Preferences->Java->Installed JREs. Select Browse and navigate to the C:\Program Files\Java\jdk1. 6.0_21 directory. Eclipse will automatically find the source and associate it with the JDK classes.


2 Answers

The way I do it is that I have an XSLT file that simply transforms my xml-data (in my case a protocol specification) to java source code. This XSLT-transformation can easily be done in an ANT-task which could be included in the build-chain in eclipse.

Perhaps there is a plugin for this particular task.

Some useful links:

  • XSLT tutorial
  • Ant: Performing XSLT Tranformations
  • Using Ant to Auto-Build in Eclipse
like image 80
aioobe Avatar answered Sep 22 '22 13:09

aioobe


I'm adding another answer based on your comments and also because I don't really recommend doing this outside of Google Android Resource SDK. Google is basically using a hierarchy of static classes (singletons) for their resources. You need to make your XSLT generate static member variables instead of getters and setters.

I basically took my old answer and changed it to static for all member variables. You have to be very careful doing this because I have seen so many bugs with incorrect use of the "static" modifier.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/" priority="100">
    public class <xsl:value-of select="name(node())" /> {
        <xsl:apply-templates  select="child::node()" />
    }
    </xsl:template>
    <xsl:template match="/*/*">
        public static String <xsl:value-of select="name()" />;
        public static String get<xsl:value-of select="name()" /> {
            return <xsl:value-of select=" name()" />;
        }

        public void static set<xsl:value-of select="name()" />(String value) {
            <xsl:value-of select="name()" /> = value;
        }
    </xsl:template>
</xsl:stylesheet>

If you process with this example XML:

<?xml version="1.0" encoding="UTF-8"?>
<Human>
    <EyeColor>brown</EyeColor>
    <HairColor>brown</HairColor>
</Human>

You get something like: public class Human {

    public static String EyeColor;

    public static String getEyeColor {
        return EyeColor;
    }

    public static void setEyeColor(String value) {
        this.EyeColor = value;
    }


    public static String HairColor;
    public static String getHairColor {
        return HairColor;
    }

    public static void setHairColor(String value) {
        this.HairColor = value;
    }


}
like image 36
Adam Gent Avatar answered Sep 20 '22 13:09

Adam Gent