Xtext - Generating object - generate more data
-
14-07-2021 - |
Pregunta
I have xtext file similar to the xtext describe in here: (www.ebpml.org/blog2/index.php/2010/11/05/mde-xtext-and-json)
JSONDataSource: root = Object ;
Object: '{' firstObject=TerminalObject (',' objects+=TerminalObject)* '}';
Array: '['firstItem=ObjectValue (',' items+=ObjectValue)* ']';
EmptyObject: isEmpty?= '{}' ;
EmptyArray: isEmpty?= '[]' ;
ObjectValue: value=STRING | Object | Array | EmptyObject | EmptyArray ;
TerminalObject: element=STRING ':' (content = ObjectValue) ;
So after generating the code I have all the objects generated in java. now in I want to have more methods in those generated java files.
what is the best way to do so?
thanks :)
Solución
Well, there are no easy ways to customize these classes. You can do basically two things:
- Create your own EMF (Ecore) model, and create your grammar in a way that reuses your Ecore model. This way, you can define new operations, and create their implementations manually.
- You could also set up a postprocessor for the Ecore model generator that fills out the contents. See http://christiandietrich.wordpress.com/2011/07/22/customizing-xtext-metamodel-inference-using-xtend2/ for details - but be vary, as the model inference cannot directly output custom code; there are some EMF genmodel-annotation based way to create the bodies of generated code (see the final part of http://jevopisdeveloperblog.blogspot.hu/2011/03/implement-tostring-with-xtexts.html for details).
Alltogether, customizing the generated EMF models is quite hard, and I do not recommend doing it. Although it seems unnecessary to introduce new helper classes, technologically it is much easier to do.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow