簡體   English   中英

如何修改ecore XML文件以生成除getter和setters Java / EMF之外的方法

[英]How can I modify my ecore XML file to generate methods other than getters and setters Java/EMF

我正在使用EMF從ecore XML文件生成Java類。 我試圖在此生成的代碼中重寫equals()和hashCode()方法,但是我沒有找到關於如何生成除基本get和set方法之外的任何內容的在線指南。 我的sample.ecore文件看起來像這樣:

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="sample" nsURI="jewel:/sample" nsPrefix="sample">
    <eSubpackages name="models" nsURI="jewel:/sample.models" nsPrefix="models">
        <eClassifiers xsi:type="ecore:EClass" name="Foo" abstract="true" eSuperTypes="models.ecore#//Foo">
            <eStructuralFeatures xsi:type="ecore:EAttribute" name="code" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
            <eStructuralFeatures xsi:type="ecore:EAttribute" name="anotherAttribute" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
            <eStructuralFeatures xsi:type="ecore:EAttribute" name="aDateAttribute" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EDate"/>
            <eStructuralFeatures xsi:type="ecore:EAttribute" name="aBooleanAttribute" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean"/>
        </eClassifiers>
    </eSubpackages>
</ecore:EPackage>

我正在嘗試生成看起來像以下基本代碼的代碼:

@Override
public boolean equals(Object o) {
    if(o == this) return true;
    if(!(o instanceof Foo))
        return false;
    Foo foo = (Foo) o;
    return foo.code=code;
}

@Override
public int hashCode() {
    int result = 17;
    result = 31 * result + code.hashCode();
    return result;
}

我可以在eClassifiers中編寫某種附加標簽來生成此標記,還是在代碼生成中需要更改某種屬性?

這可以通過使用body關鍵字和源代碼注釋EOperation來實現。 生成器模型會選擇“ http://www.eclipse.org/emf/2002/GenModel ”注釋。

<eOperations name="hash" eType="#//EInt"> 
      <eAnnotations source=" http://www.eclipse.org/emf/2002/GenModel "> 
        <details key=" body " value="int result = 17;&#xA;    result = 31 * result + code.hashCode();&#xA;    return result;"/> 
      </eAnnotations> 
 </eOperations> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM