8. Generating methods #
See the YouTube playlist for all videos.
By default EMF generates getter and setter for every class. You can also add Operations or for example overwrite methods, e.g., the toString()
method. For Article the following toString method was generated in “ArticleImpl”
/*
* @generated
*/
@Override
public String toString() {
if (eIsProxy()) return super.toString();
StringBuffer result = new StringBuffer(super.toString());
result.append(" (name: ");
result.append(name);
result.append(", created: ");
result.append(created);
result.append(')');
return result.toString();
}
To overwrite this, add a "EOperation" to your model with the name toString. Maintain in the properties "EType" EString as return type.
Add an annotation with the source "http://www.eclipse.org/emf/2002/GenModel" and maintain an entry with the key "body", the value is the code that will be generated in to the method, you find it listed below.
if (eIsProxy()) return super.toString();
StringBuffer result = new StringBuffer(super.toString());
result.append("Article: ");
result.append(name);
return result.toString();
You can also generate methods with input parameter, just add parameter with their type to your EOperation.