简体   繁体   English

在Eclipse中生成java代码?

[英]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? 有谁知道在eclipse中自动生成Java源代码的方法,例如xml或json文件?

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 . 我正在考虑做的一个很好的例子是Google Android sdk所做的事情:他们有一个从资源自动生成的R类。

Every time a resource file is saved in Eclipse R class is automatically regenerated. 每次在Eclipse中保存资源文件时,R类都会自动重新生成。

UPDATE: Example: In the text (xml or json file) I have the following: 更新:示例:在文本(xml或json文件)中,我有以下内容:

 <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 RI would have something like: 然后在我生成的java类中,说RI会有类似的东西:

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! 谢谢!

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. 我这样做的方式是我有一个XSLT文件,它只是将我的xml数据(在我的情况下是一个协议规范)转换为java源代码。 This XSLT-transformation can easily be done in an ANT-task which could be included in the build-chain in eclipse. 这个XSLT转换可以很容易地在一个ANT任务中完成,该任务可以包含在eclipse的构建链中。

Perhaps there is a plugin for this particular task. 也许这个特定任务有一个插件。

Some useful links: 一些有用的链接:

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 Android Resource SDK之外执行此操作。 Google is basically using a hierarchy of static classes (singletons) for their resources. Google基本上使用静态类(单例)的层次结构来获取其资源。 You need to make your XSLT generate static member variables instead of getters and setters. 您需要使XSLT生成静态成员变量而不是getter和setter。

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:

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

You get something like: public class Human { 你会得到类似的结果: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;
    }


}

Well Eclipse Modelling Framework (EMF) is meant for the application you are looking forward to. Eclipse Eclipse Modeling Framework(EMF)适用于您期待的应用程序。 I assume you have a model and you want to convert it into code. 我假设你有一个模型,你想将其转换为代码。 Very specific hint I can give is JET (Java Emitter template) you can refer here for more details. 我可以给出的非常具体的提示是JET (Java Emitter模板),您可以在这里参考更多细节。

Also the newer framework XPand introduced by eclipse, 也是由eclipse引入的更新的框架XP,

Revolving around the abstract syntax-development components are model-transformation tech- nologies. 围绕抽象语法开发组件的转换是模型转换技术。 1. model-to-text (Java Emitter Templates [JET] and Xpand) 2. model-to-model (QVT and ATL) 1.模型到文本(Java Emitter Templates [JET]和Xpand)2。模型到模型(QVT和ATL)

Here model refers to XML-XSLT / UML (Rational rose) model. 这里的模型是指XML-XSLT / UML(Rational rose)模型。

Take a look at XDoclet . 看看XDoclet Its an extensible open source code generation engine for Java. 它是一个可扩展的Java开源代码生成引擎。

EDIT: As bozho points out, XDoclet has mostly been replaced by annotations: Annotations vs. XDoclet 编辑:正如bozho指出的那样,XDoclet主要被注释取代: 注释与XDoclet

Do any java libraries use annotations for code generation? 是否有任何Java库使用注释来生成代码?

From XML to JAVA and vice Versa I highly recommend JAXB. 从XML到JAVA和副Versa我强烈推荐JAXB。

You can generate Java source code from Schema's using JAXB 2.0 or greater. 您可以使用JAXB 2.0或更高版本从Schema生成Java源代码。 Or you can generate Schemas from Java. 或者您可以从Java生成Schema。

You can also easily make JAXB generate/consume JSON using Jettison. 您还可以使用Jettison轻松地使JAXB生成/使用JSON。

You can also have JAXB generate POJO's in a Martin Fowler Fluent Style or a whole bunch of different ways using its plugin system. 您还可以让JAXB使用其插件系统以Martin Fowler Fluent Style或一系列不同方式生成POJO。

EDIT based on your comments: Have XSLT generate your JAXB POJO: 编辑基于您的评论:让XSLT生成您的JAXB POJO:

<?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">
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement(name="Human")
    public class <xsl:value-of select="name(node())" /> {
        <xsl:apply-templates  select="child::node()" />
    }
    </xsl:template>
    <xsl:template match="/*/*">
        private String <xsl:value-of select="name()" />;
        public String get<xsl:value-of select="name()" /> {
            return <xsl:value-of select=" name()" />;
        }

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

If you process with this example XML: 如果您使用此示例处理XML:

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

You get something like: 你会得到类似的东西:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Human")
public class Human {


    private String EyeColor;
    public String getEyeColor {
        return EyeColor;
    }

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


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

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


}

您可以查看Eclipse建模项目的模型到文本组件

是的,您可以使用xml文件来创建它。创建您自己的xml文件模板,然后使用preferences-java-code template-import选择该文件,您可以使用该模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM