[英]How can I modify my ecore XML file to generate methods other than getters and setters Java/EMF
I am using EMF to generate Java classes from an ecore XML file. 我正在使用EMF从ecore XML文件生成Java类。 I am trying to, in this generated code, override the equals() and hashCode() methods, but I have not found any good guides online on how to generate anything beyond basic get and set methods.
我试图在此生成的代码中重写equals()和hashCode()方法,但是我没有找到关于如何生成除基本get和set方法之外的任何内容的在线指南。 My sample.ecore file looks something like this:
我的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>
I am trying to generate code that looks something like the below basic code: 我正在尝试生成看起来像以下基本代码的代码:
@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;
}
Is there some kind of additional tag I can write inside of eClassifiers to generate this, or some kind of property that I need to change in the code generation? 我可以在eClassifiers中编写某种附加标签来生成此标记,还是在代码生成中需要更改某种属性?
This should be possible by annotating an EOperation
with the body
keyword and the source code. 这可以通过使用
body
关键字和源代码注释EOperation
来实现。 The " http://www.eclipse.org/emf/2002/GenModel " annotations are picked up by the generator model. 生成器模型会选择“ 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;
 result = 31 * result + code.hashCode();
 return result;"/>
</eAnnotations>
</eOperations>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.